Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow apps to determine if a Navigator is ready for navigation #72

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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