fix: 隐藏首页动态入口失效

This commit is contained in:
HuanCheng65 2023-07-23 22:23:47 +08:00
parent fed1b0c284
commit 9cc4d7ec8d
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
1 changed files with 59 additions and 51 deletions

View File

@ -122,65 +122,73 @@ fun MainPage(
} }
} }
val pagerState = rememberPagerState()
val hideExplore by rememberPreferenceAsState( val hideExplore by rememberPreferenceAsState(
key = booleanPreferencesKey("hideExplore"), key = booleanPreferencesKey("hideExplore"),
defaultValue = false defaultValue = LocalContext.current.appPreferences.hideExplore
) )
val pagerState = rememberPagerState() LaunchedEffect(hideExplore) {
if (pagerState.currentPage == 3 && hideExplore) {
pagerState.scrollToPage(2)
}
}
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val themeColors = ExtendedTheme.colors val themeColors = ExtendedTheme.colors
val navigationItems = remember(messageCount) { val navigationItems by remember {
listOfNotNull( derivedStateOf {
NavigationItem( listOfNotNull(
id = "home", NavigationItem(
icon = { if (it) Icons.Rounded.Inventory2 else Icons.Outlined.Inventory2 }, id = "home",
title = { stringResource(id = R.string.title_main) }, icon = { if (it) Icons.Rounded.Inventory2 else Icons.Outlined.Inventory2 },
content = { title = { stringResource(id = R.string.title_main) },
HomePage( content = {
canOpenExplore = !LocalContext.current.appPreferences.hideExplore HomePage(
) { canOpenExplore = !LocalContext.current.appPreferences.hideExplore
coroutineScope.launch { ) {
pagerState.scrollToPage(1) coroutineScope.launch {
pagerState.scrollToPage(1)
}
} }
} }
} ),
), if (hideExplore) null
if (hideExplore) null else NavigationItem(
else NavigationItem( id = "explore",
id = "explore", icon = {
icon = { if (it) ImageVector.vectorResource(id = R.drawable.ic_round_toys)
if (it) ImageVector.vectorResource(id = R.drawable.ic_round_toys) else ImageVector.vectorResource(id = R.drawable.ic_outline_toys)
else ImageVector.vectorResource(id = R.drawable.ic_outline_toys) },
}, title = { stringResource(id = R.string.title_explore) },
title = { stringResource(id = R.string.title_explore) }, content = {
content = { ExplorePage()
ExplorePage() }
} ),
), NavigationItem(
NavigationItem( id = "notification",
id = "notification", icon = { if (it) Icons.Rounded.Notifications else Icons.Outlined.Notifications },
icon = { if (it) Icons.Rounded.Notifications else Icons.Outlined.Notifications }, title = { stringResource(id = R.string.title_notifications) },
title = { stringResource(id = R.string.title_notifications) }, badge = messageCount > 0,
badge = messageCount > 0, badgeText = "$messageCount",
badgeText = "$messageCount", onClick = {
onClick = { viewModel.send(MainUiIntent.NewMessage.Clear)
viewModel.send(MainUiIntent.NewMessage.Clear) },
}, content = {
content = { NotificationsPage()
NotificationsPage() }
} ),
), NavigationItem(
NavigationItem( id = "user",
id = "user", icon = { if (it) Icons.Rounded.AccountCircle else Icons.Outlined.AccountCircle },
icon = { if (it) Icons.Rounded.AccountCircle else Icons.Outlined.AccountCircle }, title = { stringResource(id = R.string.title_user) },
title = { stringResource(id = R.string.title_user) }, content = {
content = { UserPage()
UserPage() }
} ),
), ).toImmutableList()
).toImmutableList() }
} }
val pageCount by remember { derivedStateOf { navigationItems.size } }
val navigationType by remember { val navigationType by remember {
derivedStateOf { derivedStateOf {
@ -262,7 +270,7 @@ fun MainPage(
) { paddingValues -> ) { paddingValues ->
LazyLoadHorizontalPager( LazyLoadHorizontalPager(
contentPadding = paddingValues, contentPadding = paddingValues,
pageCount = navigationItems.size, pageCount = pageCount,
state = pagerState, state = pagerState,
key = { navigationItems[it].id }, key = { navigationItems[it].id },
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),