diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/main/MainPage.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/main/MainPage.kt index fc086fb8..078f177d 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/main/MainPage.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/main/MainPage.kt @@ -122,65 +122,73 @@ fun MainPage( } } + val pagerState = rememberPagerState() val hideExplore by rememberPreferenceAsState( 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 themeColors = ExtendedTheme.colors - val navigationItems = remember(messageCount) { - listOfNotNull( - NavigationItem( - id = "home", - icon = { if (it) Icons.Rounded.Inventory2 else Icons.Outlined.Inventory2 }, - title = { stringResource(id = R.string.title_main) }, - content = { - HomePage( - canOpenExplore = !LocalContext.current.appPreferences.hideExplore - ) { - coroutineScope.launch { - pagerState.scrollToPage(1) + val navigationItems by remember { + derivedStateOf { + listOfNotNull( + NavigationItem( + id = "home", + icon = { if (it) Icons.Rounded.Inventory2 else Icons.Outlined.Inventory2 }, + title = { stringResource(id = R.string.title_main) }, + content = { + HomePage( + canOpenExplore = !LocalContext.current.appPreferences.hideExplore + ) { + coroutineScope.launch { + pagerState.scrollToPage(1) + } } } - } - ), - if (hideExplore) null - else NavigationItem( - id = "explore", - icon = { - if (it) ImageVector.vectorResource(id = R.drawable.ic_round_toys) - else ImageVector.vectorResource(id = R.drawable.ic_outline_toys) - }, - title = { stringResource(id = R.string.title_explore) }, - content = { - ExplorePage() - } - ), - NavigationItem( - id = "notification", - icon = { if (it) Icons.Rounded.Notifications else Icons.Outlined.Notifications }, - title = { stringResource(id = R.string.title_notifications) }, - badge = messageCount > 0, - badgeText = "$messageCount", - onClick = { - viewModel.send(MainUiIntent.NewMessage.Clear) - }, - content = { - NotificationsPage() - } - ), - NavigationItem( - id = "user", - icon = { if (it) Icons.Rounded.AccountCircle else Icons.Outlined.AccountCircle }, - title = { stringResource(id = R.string.title_user) }, - content = { - UserPage() - } - ), - ).toImmutableList() + ), + if (hideExplore) null + else NavigationItem( + id = "explore", + icon = { + if (it) ImageVector.vectorResource(id = R.drawable.ic_round_toys) + else ImageVector.vectorResource(id = R.drawable.ic_outline_toys) + }, + title = { stringResource(id = R.string.title_explore) }, + content = { + ExplorePage() + } + ), + NavigationItem( + id = "notification", + icon = { if (it) Icons.Rounded.Notifications else Icons.Outlined.Notifications }, + title = { stringResource(id = R.string.title_notifications) }, + badge = messageCount > 0, + badgeText = "$messageCount", + onClick = { + viewModel.send(MainUiIntent.NewMessage.Clear) + }, + content = { + NotificationsPage() + } + ), + NavigationItem( + id = "user", + icon = { if (it) Icons.Rounded.AccountCircle else Icons.Outlined.AccountCircle }, + title = { stringResource(id = R.string.title_user) }, + content = { + UserPage() + } + ), + ).toImmutableList() + } } + val pageCount by remember { derivedStateOf { navigationItems.size } } val navigationType by remember { derivedStateOf { @@ -262,7 +270,7 @@ fun MainPage( ) { paddingValues -> LazyLoadHorizontalPager( contentPadding = paddingValues, - pageCount = navigationItems.size, + pageCount = pageCount, state = pagerState, key = { navigationItems[it].id }, modifier = Modifier.fillMaxSize(),