feat: Refactor back navigation in adaptive screens (#4085)

Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
James Rich 2025-12-28 12:51:31 -06:00 committed by GitHub
parent b3aa2b63d2
commit cce41f6671
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 7 deletions

View file

@ -69,13 +69,16 @@ fun AdaptiveContactsScreen(
val scope = rememberCoroutineScope()
val backNavigationBehavior = BackNavigationBehavior.PopUntilScaffoldValueChange
BackHandler(enabled = navigator.currentDestination?.pane == ListDetailPaneScaffoldRole.Detail) {
val handleBack: () -> Unit = {
val currentEntry = navController.currentBackStackEntry
val isContactsRoute = currentEntry?.destination?.hasRoute<ContactsRoutes.Contacts>() == true
// Check if we navigated here from another screen (e.g., from Nodes or Map)
val previousEntry = navController.previousBackStackEntry
val isFromDifferentGraph = previousEntry?.destination?.hasRoute<ContactsRoutes.ContactsGraph>() == false
if (isFromDifferentGraph) {
// Navigate back via NavController to return to the previous screen
if (isFromDifferentGraph && !isContactsRoute) {
// Navigate back via NavController to return to the previous screen (e.g. Node Details)
navController.navigateUp()
} else {
// Close the detail pane within the adaptive scaffold
@ -83,6 +86,8 @@ fun AdaptiveContactsScreen(
}
}
BackHandler(enabled = navigator.currentDestination?.pane == ListDetailPaneScaffoldRole.Detail) { handleBack() }
LaunchedEffect(initialContactKey) {
if (initialContactKey != null) {
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, initialContactKey)
@ -135,7 +140,7 @@ fun AdaptiveContactsScreen(
message = if (contactKey == initialContactKey) initialMessage else "",
navigateToNodeDetails = { navController.navigate(NodesRoutes.NodeDetailGraph(it)) },
navigateToQuickChatOptions = { navController.navigate(ContactsRoutes.QuickChat) },
onNavigateBack = { scope.launch { navigator.navigateBack(backNavigationBehavior) } },
onNavigateBack = handleBack,
)
}
} ?: PlaceholderScreen()

View file

@ -69,12 +69,15 @@ fun AdaptiveNodeListScreen(
val scope = rememberCoroutineScope()
val backNavigationBehavior = BackNavigationBehavior.PopUntilScaffoldValueChange
BackHandler(enabled = navigator.currentDestination?.pane == ListDetailPaneScaffoldRole.Detail) {
val handleBack: () -> Unit = {
val currentEntry = navController.currentBackStackEntry
val isNodesRoute = currentEntry?.destination?.hasRoute<NodesRoutes.Nodes>() == true
// Check if we navigated here from another screen (e.g., from Messages or Map)
val previousEntry = navController.previousBackStackEntry
val isFromDifferentGraph = previousEntry?.destination?.hasRoute<NodesRoutes.NodesGraph>() == false
if (isFromDifferentGraph) {
if (isFromDifferentGraph && !isNodesRoute) {
// Navigate back via NavController to return to the previous screen
navController.navigateUp()
} else {
@ -83,6 +86,8 @@ fun AdaptiveNodeListScreen(
}
}
BackHandler(enabled = navigator.currentDestination?.pane == ListDetailPaneScaffoldRole.Detail) { handleBack() }
LaunchedEffect(initialNodeId) {
if (initialNodeId != null) {
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, initialNodeId)
@ -132,7 +137,7 @@ fun AdaptiveNodeListScreen(
nodeId = nodeId,
navigateToMessages = onNavigateToMessages,
onNavigate = { route -> navController.navigate(route) },
onNavigateUp = { scope.launch { navigator.navigateBack(backNavigationBehavior) } },
onNavigateUp = handleBack,
)
}
} ?: PlaceholderScreen()