diff --git a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt index 5b6ce0c55..41a9e7756 100644 --- a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt +++ b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt @@ -145,7 +145,7 @@ fun NiaApp( destinations = appState.topLevelDestinations, destinationsWithUnreadResources = unreadDestinations, onNavigateToDestination = appState::navigateToTopLevelDestination, - currentDestination = appState.currentBackStackEntry?.destination, + currentDestination = appState.currentDestination, modifier = Modifier.testTag("NiaBottomBar"), ) } @@ -167,7 +167,7 @@ fun NiaApp( destinations = appState.topLevelDestinations, destinationsWithUnreadResources = unreadDestinations, onNavigateToDestination = appState::navigateToTopLevelDestination, - currentDestination = appState.currentBackStackEntry?.destination, + currentDestination = appState.currentDestination, modifier = Modifier .testTag("NiaNavRail") .safeDrawingPadding(), @@ -176,10 +176,10 @@ fun NiaApp( Column(Modifier.fillMaxSize()) { // Show the top app bar on top level destinations. - val destination = appState.currentTopLevelDestination + val destination = appState.topBarTitle if (destination != null) { NiaTopAppBar( - titleRes = destination.titleTextId, + titleRes = destination, navigationIcon = NiaIcons.Search, navigationIconContentDescription = stringResource( id = settingsR.string.top_app_bar_navigation_icon_description, diff --git a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt index 443664157..ff8deba79 100644 --- a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt +++ b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt @@ -21,10 +21,12 @@ import androidx.compose.material3.windowsizeclass.WindowSizeClass import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass import androidx.compose.runtime.Composable import androidx.compose.runtime.Stable +import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.navigation.NavBackStackEntry import androidx.navigation.NavController +import androidx.navigation.NavDestination import androidx.navigation.NavGraph.Companion.findStartDestination import androidx.navigation.NavHostController import androidx.navigation.compose.currentBackStackEntryAsState @@ -86,24 +88,34 @@ class NiaAppState( networkMonitor: NetworkMonitor, userNewsResourceRepository: UserNewsResourceRepository, ) { - val currentBackStackEntry: NavBackStackEntry? + val currentDestination: NavDestination? @Composable get() = navController - .currentBackStackEntryAsState().value + .currentBackStackEntryAsState().value?.destination - val currentTopLevelDestination: TopLevelDestination? + val topBarTitle: Int? @Composable get() { - val route: String? = currentBackStackEntry?.destination?.route - val arguments: Bundle? = currentBackStackEntry?.arguments + val backStackEntry: NavBackStackEntry? by navController.currentBackStackEntryAsState() + val route: String? = backStackEntry?.destination?.route + val arguments: Bundle? = backStackEntry?.arguments return when { - route == forYouNavigationRoute -> FOR_YOU - route == bookmarksRoute -> BOOKMARKS + route == forYouNavigationRoute -> FOR_YOU.titleTextId + route == bookmarksRoute -> BOOKMARKS.titleTextId route == interestsRoute && - (arguments?.getString(topicIdArg) == null || shouldShowTwoPane) -> INTERESTS + (arguments?.getString(topicIdArg) == null || shouldShowTwoPane) -> INTERESTS.titleTextId else -> null } } + + val currentTopLevelDestination: TopLevelDestination? + @Composable get() = when (currentDestination?.route) { + forYouNavigationRoute -> FOR_YOU + bookmarksRoute -> BOOKMARKS + interestsRoute -> INTERESTS + else -> null + } + val shouldShowGradientBackground: Boolean - @Composable get() = currentBackStackEntry?.destination?.route == forYouNavigationRoute + @Composable get() = currentTopLevelDestination == FOR_YOU val shouldShowBottomBar: Boolean get() = windowSizeClass.widthSizeClass == WindowWidthSizeClass.Compact diff --git a/feature/interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/TopicScreen.kt b/feature/interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/TopicScreen.kt index e3409b46c..b8bf15b29 100644 --- a/feature/interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/TopicScreen.kt +++ b/feature/interests/src/main/java/com/google/samples/apps/nowinandroid/feature/interests/TopicScreen.kt @@ -30,6 +30,7 @@ import androidx.compose.foundation.layout.windowInsetsBottomHeight import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.GridItemSpan import androidx.compose.foundation.lazy.grid.LazyGridScope +import androidx.compose.foundation.lazy.grid.LazyGridState import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.lazy.grid.rememberLazyGridState import androidx.compose.foundation.selection.selectableGroup @@ -59,9 +60,6 @@ import com.google.samples.apps.nowinandroid.core.ui.TrackScrollJank import com.google.samples.apps.nowinandroid.core.ui.UserNewsResourcePreviewParameterProvider import com.google.samples.apps.nowinandroid.core.ui.newsFeed import com.google.samples.apps.nowinandroid.feature.interests.R.string -import com.google.samples.apps.nowinandroid.feature.interests.TopicUiState.Error -import com.google.samples.apps.nowinandroid.feature.interests.TopicUiState.Loading -import com.google.samples.apps.nowinandroid.feature.interests.TopicUiState.Success @Composable internal fun TopicScreen( @@ -72,13 +70,13 @@ internal fun TopicScreen( onBookmarkChanged: (String, Boolean) -> Unit, onNewsResourceViewed: (String) -> Unit, modifier: Modifier = Modifier, + gridState: LazyGridState = rememberLazyGridState(), ) { - val state = rememberLazyGridState() - TrackScrollJank(scrollableState = state, stateName = "topic:screen") + TrackScrollJank(scrollableState = gridState, stateName = "topic:screen") LazyVerticalGrid( columns = GridCells.Adaptive(300.dp), - state = state, + state = gridState, verticalArrangement = Arrangement.spacedBy(16.dp), modifier = modifier .selectableGroup() @@ -93,13 +91,13 @@ internal fun TopicScreen( ) } - Error -> { + TopicUiState.Error -> { item { Text(text = stringResource(id = string.topic_error)) } } - is Success -> { + is TopicUiState.Success -> { item(span = { GridItemSpan(maxLineSpan) }) { TopicToolbar( onBackClick = onBackClick, @@ -238,7 +236,7 @@ fun TopicScreenPopulated( NiaTheme { NiaBackground { TopicScreen( - topicUiState = Success( + topicUiState = TopicUiState.Success( followableTopic = userNewsResources[0].followableTopics[0], newsResources = userNewsResources, ), @@ -258,7 +256,7 @@ fun TopicScreenLoading() { NiaTheme { NiaBackground { TopicScreen( - topicUiState = Loading, + topicUiState = TopicUiState.Loading, onBackClick = {}, onFollowClick = { _, _ -> }, onBookmarkChanged = { _, _ -> },