From ef132e07e3f0be4b0af2f0992ce156583c170706 Mon Sep 17 00:00:00 2001 From: Don Turner Date: Fri, 1 Nov 2024 19:24:51 +0000 Subject: [PATCH] Fix the top app bar logic for top level destinations --- .../navigation/TopLevelDestination.kt | 20 +++++++++++++++---- .../samples/apps/nowinandroid/ui/NiaApp.kt | 2 +- .../apps/nowinandroid/ui/NiaAppState.kt | 2 +- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/navigation/TopLevelDestination.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/navigation/TopLevelDestination.kt index 5b286e111..429e626ff 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/navigation/TopLevelDestination.kt +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/navigation/TopLevelDestination.kt @@ -22,6 +22,7 @@ import com.google.samples.apps.nowinandroid.R import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons import com.google.samples.apps.nowinandroid.feature.bookmarks.navigation.BookmarksRoute import com.google.samples.apps.nowinandroid.feature.foryou.navigation.ForYouBaseRoute +import com.google.samples.apps.nowinandroid.feature.foryou.navigation.ForYouRoute import com.google.samples.apps.nowinandroid.feature.interests.navigation.InterestsRoute import kotlin.reflect.KClass import com.google.samples.apps.nowinandroid.feature.bookmarks.R as bookmarksR @@ -29,9 +30,18 @@ import com.google.samples.apps.nowinandroid.feature.foryou.R as forYouR import com.google.samples.apps.nowinandroid.feature.search.R as searchR /** - * Type for the top level destinations in the application. Each of these destinations - * can contain one or more screens (based on the window size). Navigation from one screen to the - * next within a single destination will be handled directly in composables. + * Type for the top level destinations in the application. Contains metadata about the destination + * that is used in the top app bar and common navigation UI. + * + * @param selectedIcon The icon to be displayed in the navigation UI when this destination is + * selected. + * @param unselectedIcon The icon to be displayed in the navigation UI when this destination is + * not selected. + * @param iconTextId Text that to be displayed in the navigation UI. + * @param titleTextId Text that is displayed on the top app bar. + * @param route The route to use when navigating to this destination. + * @param baseRoute The highest ancestor of this destination. Defaults to [route], meaning that + * there is a single destination in that section of the app (no nested destinations). */ enum class TopLevelDestination( val selectedIcon: ImageVector, @@ -39,13 +49,15 @@ enum class TopLevelDestination( @StringRes val iconTextId: Int, @StringRes val titleTextId: Int, val route: KClass<*>, + val baseRoute: KClass<*> = route, ) { FOR_YOU( selectedIcon = NiaIcons.Upcoming, unselectedIcon = NiaIcons.UpcomingBorder, iconTextId = forYouR.string.feature_foryou_title, titleTextId = R.string.app_name, - route = ForYouBaseRoute::class, + route = ForYouRoute::class, + baseRoute = ForYouBaseRoute::class, ), BOOKMARKS( selectedIcon = NiaIcons.Bookmarks, diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt index 6cdc32bb0..640b22e83 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt @@ -152,7 +152,7 @@ internal fun NiaApp( appState.topLevelDestinations.forEach { destination -> val hasUnread = unreadDestinations.contains(destination) val selected = currentDestination - .isRouteInHierarchy(destination.route) + .isRouteInHierarchy(destination.baseRoute) item( selected = selected, onClick = { appState.navigateToTopLevelDestination(destination) }, diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt index 75a294c01..249f07590 100644 --- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt +++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt @@ -90,7 +90,7 @@ class NiaAppState( val currentTopLevelDestination: TopLevelDestination? @Composable get() { return TopLevelDestination.entries.firstOrNull { topLevelDestination -> - currentDestination?.hasRoute(route = topLevelDestination.route) ?: false + currentDestination?.hasRoute(route = topLevelDestination.route) == true } }