From 00b875b15de874778b09de2dfd167f47311d5d33 Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Sat, 24 Feb 2024 09:07:16 -0500 Subject: [PATCH] Properly handle navigate back/up from a bottom sheet dialog fragment by cancelling the dialog instead of popping the backstack --- .../hotwire/turbo/delegates/TurboFragmentDelegate.kt | 5 +---- .../kotlin/dev/hotwire/turbo/nav/TurboNavigator.kt | 12 ++++++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboFragmentDelegate.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboFragmentDelegate.kt index b48bbe3b..13bf24e4 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboFragmentDelegate.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/delegates/TurboFragmentDelegate.kt @@ -97,10 +97,7 @@ class TurboFragmentDelegate(private val navDestination: TurboNavDestination) { navDestination.toolbarForNavigation()?.let { NavigationUI.setupWithNavController(it, fragment.findNavController()) it.setNavigationOnClickListener { - when (fragment) { - is DialogFragment -> fragment.requireDialog().cancel() - else -> navDestination.navigateUp() - } + navDestination.navigateUp() } } } diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavigator.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavigator.kt index 5a814aba..bdf6eb79 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavigator.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavigator.kt @@ -22,13 +22,21 @@ internal class TurboNavigator(private val navDestination: TurboNavDestination) { fun navigateUp() { onNavigationVisit { - currentController().navigateUp() + if (fragment is DialogFragment) { + fragment.requireDialog().cancel() + } else { + currentController().navigateUp() + } } } fun navigateBack() { onNavigationVisit { - currentController().popBackStack() + if (fragment is DialogFragment) { + fragment.requireDialog().cancel() + } else { + currentController().popBackStack() + } } }