Skip to content

Commit

Permalink
Merge pull request #72 from hotwired/navigator-ready
Browse files Browse the repository at this point in the history
Allow apps to determine if a `Navigator` is ready for navigation
  • Loading branch information
jayohms authored Nov 26, 2024
2 parents 52c5ee6 + 9816065 commit 235f364
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ 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
}

internal fun initControllerGraph() {
navController.apply {
graph = NavigatorGraphBuilder(
Expand Down

0 comments on commit 235f364

Please sign in to comment.