Skip to content

Commit

Permalink
修复rememberSaveable中存储List的错误用法
Browse files Browse the repository at this point in the history
  • Loading branch information
guozhiqiang123 committed Nov 13, 2022
1 parent ca9b426 commit fea789d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
51 changes: 25 additions & 26 deletions app/src/main/java/com/gzq/wanandroid/features/main/MainPage.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gzq.wanandroid.features.main

import androidx.annotation.StringRes
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.expandHorizontally
import androidx.compose.animation.fadeIn
Expand Down Expand Up @@ -60,11 +61,32 @@ val LocalSnackbarHostState = compositionLocalOf { SnackbarHostState() }

data class BottomNavigationModel(
val tag: HomeNav,
val label: String,
@StringRes val labelId: Int,
val selectIcon: ImageVector,
val unSelectIcon: ImageVector,
)

val bottomModels = listOf(
BottomNavigationModel(
tag = HomeNav.HOME,
labelId = R.string.nav_home,
selectIcon = Icons.Default.Home,
unSelectIcon = Icons.Outlined.Home,
),
BottomNavigationModel(
tag = HomeNav.PROJECT,
labelId = R.string.nav_project,
selectIcon = Icons.Default.Build,
unSelectIcon = Icons.Outlined.Build,
),
BottomNavigationModel(
tag = HomeNav.PROFILE,
labelId = R.string.nav_profile,
selectIcon = Icons.Default.Person,
unSelectIcon = Icons.Outlined.Person,
),
)

@Composable
fun MainPage(
windowSizeClass: WindowSizeClass,
Expand All @@ -86,29 +108,6 @@ fun MainPage(
val snackbarHostState = remember { SnackbarHostState() }

LogCompositions(msg = "MainPage")
val ctx = LocalContext.current
val bottomModels = rememberSaveable(ctx) {
listOf(
BottomNavigationModel(
tag = HomeNav.HOME,
label = with(ctx) { resources.getString(R.string.nav_home) },
selectIcon = Icons.Default.Home,
unSelectIcon = Icons.Outlined.Home,
),
BottomNavigationModel(
tag = HomeNav.PROJECT,
label = with(ctx) { resources.getString(R.string.nav_project) },
selectIcon = Icons.Default.Build,
unSelectIcon = Icons.Outlined.Build,
),
BottomNavigationModel(
tag = HomeNav.PROFILE,
label = with(ctx) { resources.getString(R.string.nav_profile) },
selectIcon = Icons.Default.Person,
unSelectIcon = Icons.Outlined.Person,
),
)
}

AndroidTemplateTheme {
androidx.compose.material.Scaffold(
Expand All @@ -123,7 +122,7 @@ fun MainPage(
},
drawerContent = if (!appState.shouldShowBottomBar && showNavigationBar) {
{
DrawableContentC(bottomModels,isSelect = { model ->
DrawableContentC(isSelect = { model ->
selectNav.value == model.tag
}) { model ->
if (scaffoldState.drawerState.isOpen) {
Expand Down Expand Up @@ -153,7 +152,7 @@ fun MainPage(
enter = expandHorizontally() + fadeIn(),
exit = shrinkVertically() + fadeOut()
) {
BottomNavigationC(bottomModels, isSelect = { model ->
BottomNavigationC(isSelect = { model ->
selectNav.value == model.tag
}) { model ->
selectNav.value = model.tag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.gzq.wanandroid.features.main.BottomNavigationModel
import com.gzq.wanandroid.features.main.bottomModels

@Composable
fun BottomNavigationC(
bottomModels: List<BottomNavigationModel>,
isSelect: (BottomNavigationModel) -> Boolean,
onClick: (BottomNavigationModel) -> Unit
) {
Expand All @@ -34,7 +35,7 @@ fun BottomNavigationC(
bottomModels.forEach { model ->
BottomTab(
isSelect = isSelect(model),
label = model.label,
label = stringResource(id = model.labelId),
selectIcon = model.selectIcon,
unSelectIcon = model.unSelectIcon,
onClick = { onClick(model) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.gzq.wanandroid.features.main.BottomNavigationModel
import com.gzq.wanandroid.features.main.bottomModels

@Composable
fun ColumnScope.DrawableContentC(
bottomModels: List<BottomNavigationModel>,
isSelect: (BottomNavigationModel) -> Boolean,
onClick: (BottomNavigationModel) -> Unit
) {
bottomModels.forEach { model ->
DrawerTab(
isSelect = isSelect(model),
label = model.label,
label = stringResource(id = model.labelId),
selectIcon = model.selectIcon,
unSelectIcon = model.unSelectIcon,
onClick = { onClick(model) }
Expand Down

0 comments on commit fea789d

Please sign in to comment.