From fea789d285288185a66928ce1e5e6341d6c582e5 Mon Sep 17 00:00:00 2001 From: gzq <774550196@qq.com> Date: Sun, 13 Nov 2022 17:59:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DrememberSaveable=E4=B8=AD?= =?UTF-8?q?=E5=AD=98=E5=82=A8List=E7=9A=84=E9=94=99=E8=AF=AF=E7=94=A8?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gzq/wanandroid/features/main/MainPage.kt | 51 +++++++++---------- .../main/components/BottomNavigationC.kt | 5 +- .../main/components/DrawerContentC.kt | 5 +- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/gzq/wanandroid/features/main/MainPage.kt b/app/src/main/java/com/gzq/wanandroid/features/main/MainPage.kt index 6a02602..9ba0280 100644 --- a/app/src/main/java/com/gzq/wanandroid/features/main/MainPage.kt +++ b/app/src/main/java/com/gzq/wanandroid/features/main/MainPage.kt @@ -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 @@ -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, @@ -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( @@ -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) { @@ -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 diff --git a/app/src/main/java/com/gzq/wanandroid/features/main/components/BottomNavigationC.kt b/app/src/main/java/com/gzq/wanandroid/features/main/components/BottomNavigationC.kt index 6dc8caf..00c19db 100644 --- a/app/src/main/java/com/gzq/wanandroid/features/main/components/BottomNavigationC.kt +++ b/app/src/main/java/com/gzq/wanandroid/features/main/components/BottomNavigationC.kt @@ -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, isSelect: (BottomNavigationModel) -> Boolean, onClick: (BottomNavigationModel) -> Unit ) { @@ -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) } diff --git a/app/src/main/java/com/gzq/wanandroid/features/main/components/DrawerContentC.kt b/app/src/main/java/com/gzq/wanandroid/features/main/components/DrawerContentC.kt index 4af0faa..8e071db 100644 --- a/app/src/main/java/com/gzq/wanandroid/features/main/components/DrawerContentC.kt +++ b/app/src/main/java/com/gzq/wanandroid/features/main/components/DrawerContentC.kt @@ -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, 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) }