From 7a8e73542e346389979aca33e681d96af2b10a4c Mon Sep 17 00:00:00 2001 From: James Rich <2199651+jamesarich@users.noreply.github.com> Date: Sun, 20 Jul 2025 09:13:48 -0500 Subject: [PATCH] refactor(Main): Simplify top-level destination check (#2458) Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com> --- .../main/java/com/geeksville/mesh/ui/Main.kt | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/ui/Main.kt b/app/src/main/java/com/geeksville/mesh/ui/Main.kt index 41516370d..95626e009 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/Main.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/Main.kt @@ -111,7 +111,13 @@ enum class TopLevelDestination(@StringRes val label: Int, val icon: ImageVector, ; companion object { - fun NavDestination.isTopLevel(): Boolean = entries.any { hasRoute(it.route::class) } + fun NavDestination.isTopLevel(): Boolean = listOf( + NodesRoutes.Nodes, + ContactsRoutes.Contacts, + MapRoutes.Map, + ChannelsRoutes.Channels, + ConnectionsRoutes.Connections, + ).any { this.hasRoute(it::class) } fun fromNavDestination(destination: NavDestination?): TopLevelDestination? = entries .find { dest -> destination?.hierarchy?.any { it.hasRoute(dest.route::class) } == true } @@ -378,7 +384,6 @@ private fun MainAppBar( val backStackEntry by navController.currentBackStackEntryAsState() val currentDestination = backStackEntry?.destination val canNavigateBack = navController.previousBackStackEntry != null - val isTopLevelRoute = currentDestination?.isTopLevel() == true val navigateUp: () -> Unit = navController::navigateUp if (currentDestination?.hasRoute() == true) { return @@ -389,7 +394,7 @@ private fun MainAppBar( TopAppBar( title = { val title = when { - currentDestination == null || isTopLevelRoute -> stringResource(id = R.string.app_name) + currentDestination == null || currentDestination.isTopLevel() -> stringResource(id = R.string.app_name) currentDestination.hasRoute() -> stringResource(id = R.string.debug_panel) @@ -420,7 +425,7 @@ private fun MainAppBar( } }, modifier = modifier, - navigationIcon = if (canNavigateBack && !isTopLevelRoute) { + navigationIcon = if (canNavigateBack && currentDestination?.isTopLevel() == false) { { IconButton(onClick = navigateUp) { Icon( @@ -447,7 +452,6 @@ private fun MainAppBar( viewModel = viewModel, currentDestination = currentDestination, isManaged = isManaged, - isTopLevelRoute = isTopLevelRoute, onAction = onAction ) }, @@ -459,7 +463,6 @@ private fun TopBarActions( viewModel: UIViewModel = hiltViewModel(), currentDestination: NavDestination?, isManaged: Boolean, - isTopLevelRoute: Boolean, onAction: (Any?) -> Unit ) { val ourNode by viewModel.ourNodeInfo.collectAsStateWithLifecycle() @@ -474,17 +477,19 @@ private fun TopBarActions( ) } } - when { - currentDestination == null || isTopLevelRoute -> - MainMenuActions(isManaged, onAction) + currentDestination?.let { + when { + it.isTopLevel() -> + MainMenuActions(isManaged, onAction) - currentDestination.hasRoute() -> - DebugMenuActions() + currentDestination.hasRoute() -> + DebugMenuActions() - currentDestination.hasRoute() -> - RadioConfigMenuActions(viewModel = viewModel) + currentDestination.hasRoute() -> + RadioConfigMenuActions(viewModel = viewModel) - else -> {} + else -> {} + } } }