Tidy up top level destination handling

Change-Id: I385b6f49ba37801e40bb90254abe96e2350d48e8
pull/1413/head
Don Turner 4 months ago
parent fc58680c06
commit e19c31570b

@ -16,6 +16,7 @@
package com.google.samples.apps.nowinandroid.navigation
import androidx.annotation.StringRes
import androidx.compose.ui.graphics.vector.ImageVector
import com.google.samples.apps.nowinandroid.R
import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons
@ -35,8 +36,8 @@ import com.google.samples.apps.nowinandroid.feature.search.R as searchR
enum class TopLevelDestination(
val selectedIcon: ImageVector,
val unselectedIcon: ImageVector,
val iconTextId: Int,
val titleTextId: Int,
@StringRes val iconTextId: Int,
@StringRes val titleTextId: Int,
val route: KClass<*>,
) {
FOR_YOU(

@ -73,6 +73,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.theme.LocalGradien
import com.google.samples.apps.nowinandroid.feature.settings.SettingsDialog
import com.google.samples.apps.nowinandroid.navigation.NiaNavHost
import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination
import kotlin.reflect.KClass
import com.google.samples.apps.nowinandroid.feature.settings.R as settingsR
@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@ -151,7 +152,7 @@ internal fun NiaApp(
appState.topLevelDestinations.forEach { destination ->
val hasUnread = unreadDestinations.contains(destination)
val selected = currentDestination
.isTopLevelDestinationInHierarchy(destination)
.isRouteInHierarchy(destination.route)
item(
selected = selected,
onClick = { appState.navigateToTopLevelDestination(destination) },
@ -199,8 +200,10 @@ internal fun NiaApp(
) {
// Show the top app bar on top level destinations.
val destination = appState.currentTopLevelDestination
val shouldShowTopAppBar = destination != null
var shouldShowTopAppBar = false
if (destination != null) {
shouldShowTopAppBar = true
NiaTopAppBar(
titleRes = destination.titleTextId,
navigationIcon = NiaIcons.Search,
@ -267,7 +270,7 @@ private fun Modifier.notificationDot(): Modifier =
}
}
private fun NavDestination?.isTopLevelDestinationInHierarchy(destination: TopLevelDestination) =
private fun NavDestination?.isRouteInHierarchy(route: KClass<*>) =
this?.hierarchy?.any {
it.hasRoute(destination.route)
it.hasRoute(route)
} ?: false

@ -92,12 +92,9 @@ class NiaAppState(
val currentTopLevelDestination: TopLevelDestination?
@Composable get() {
with(currentDestination) {
if (this?.hasRoute<ForYouRoute>() == true) return FOR_YOU
if (this?.hasRoute<BookmarksRoute>() == true) return BOOKMARKS
if (this?.hasRoute<InterestsRoute>() == true) return INTERESTS
return TopLevelDestination.entries.firstOrNull { topLevelDestination ->
currentDestination?.hasRoute(route = topLevelDestination.route) ?: false
}
return null
}
val isOffline = networkMonitor.isOnline

@ -20,7 +20,6 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.navigation.toRoute
import com.google.samples.apps.nowinandroid.feature.interests.navigation.InterestsRoute
import com.google.samples.apps.nowinandroid.feature.interests.navigation.TOPIC_ID_KEY
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject

Loading…
Cancel
Save