From a98f261d2bbfe7cade6423967e89869221edf5ca Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Tue, 26 Nov 2024 10:26:51 -0500 Subject: [PATCH 1/2] Allow an app to check whether a navigator or its host are ready for navigation --- .../activities/HotwireActivityDelegate.kt | 15 ++++++++------- .../fragments/HotwireWebBottomSheetFragment.kt | 2 +- .../navigation/fragments/HotwireWebFragment.kt | 2 +- .../dev/hotwire/navigation/navigator/Navigator.kt | 12 ++++++++++++ .../hotwire/navigation/navigator/NavigatorHost.kt | 4 ++++ 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/activities/HotwireActivityDelegate.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/activities/HotwireActivityDelegate.kt index 281178f..1240cc1 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/activities/HotwireActivityDelegate.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/activities/HotwireActivityDelegate.kt @@ -44,16 +44,17 @@ class HotwireActivityDelegate(val activity: HotwireActivity) { /** * Get the Activity's currently active [Navigator]. + * + * Returns null if the navigator is not ready for navigation. */ val currentNavigator: Navigator? - get() { - return if (currentNavigatorHost.isAdded && !currentNavigatorHost.isDetached) { - currentNavigatorHost.navigator - } else { - null - } + get() = if (currentNavigatorHost.isReady()) { + currentNavigatorHost.navigator + } else { + null } + /** * Sets the currently active navigator in your Activity. If you use multiple * [NavigatorHost] instances in your app (such as for bottom tabs), @@ -107,7 +108,7 @@ class HotwireActivityDelegate(val activity: HotwireActivity) { } private fun updateOnBackPressedCallback(navController: NavController) { - if (navController == currentNavigatorHost.navController) { + if (navController == currentNavigatorHost.navController) { onBackPressedCallback.isEnabled = navController.previousBackStackEntry != null } } diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebBottomSheetFragment.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebBottomSheetFragment.kt index e4e8f79..ac330d8 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebBottomSheetFragment.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebBottomSheetFragment.kt @@ -108,7 +108,7 @@ open class HotwireWebBottomSheetFragment : HotwireBottomSheetFragment(), Hotwire /** * Gets the HotwireView instance in the Fragment's view - * with resource ID R.id.turbo_view. + * with resource ID R.id.hotwire_view. */ final override val hotwireView: HotwireView? get() = view?.findViewById(R.id.hotwire_view) diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt index 220fd16..de0c9a5 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt @@ -126,7 +126,7 @@ open class HotwireWebFragment : HotwireFragment(), HotwireWebFragmentCallback { /** * Gets the HotwireView instance in the Fragment's view - * with resource ID R.id.turbo_view. + * with resource ID R.id.hotwire_view. */ final override val hotwireView: HotwireView? get() = view?.findViewById(R.id.hotwire_view) diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/Navigator.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/Navigator.kt index f153e87..daa7f66 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/Navigator.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/Navigator.kt @@ -64,6 +64,18 @@ class Navigator( } } + /** + * Returns whether the navigator and its host are ready for navigation. It is not + * ready for navigation if the host view is not attached or the start destination + * has not been created yet. + */ + fun isReady(): Boolean { + return host.isReady() + } + + /** + * Returns whether the current destination is the only backstack entry. + */ fun isAtStartDestination(): Boolean { return navController.previousBackStackEntry == null } diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/NavigatorHost.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/NavigatorHost.kt index b5a2273..6958b7f 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/NavigatorHost.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/NavigatorHost.kt @@ -27,6 +27,10 @@ open class NavigatorHost : NavHostFragment() { activity.delegate.unregisterNavigatorHost(this) } + fun isReady(): Boolean { + return isAdded && !isDetached && childFragmentManager.primaryNavigationFragment != null + } + internal fun initControllerGraph() { navController.apply { graph = NavigatorGraphBuilder( From 9816065a6d4a4cf55c853498f460cfc887939bc1 Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Tue, 26 Nov 2024 10:30:54 -0500 Subject: [PATCH 2/2] Add API doc --- .../java/dev/hotwire/navigation/navigator/NavigatorHost.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/NavigatorHost.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/NavigatorHost.kt index 6958b7f..782f8b1 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/NavigatorHost.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/NavigatorHost.kt @@ -27,6 +27,11 @@ open class NavigatorHost : NavHostFragment() { activity.delegate.unregisterNavigatorHost(this) } + /** + * Returns whether the navigation host is ready for navigation. It is not + * ready for navigation if the view is not attached or the start destination + * has not been created yet. + */ fun isReady(): Boolean { return isAdded && !isDetached && childFragmentManager.primaryNavigationFragment != null }