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

Replace providesDefault in Navigator with provides and an if check #500

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -77,9 +77,7 @@ public fun Navigator(
require(screens.isNotEmpty()) { "Navigator must have at least one screen" }
require(key.isNotEmpty()) { "Navigator key can't be empty" }

CompositionLocalProvider(
LocalNavigatorStateHolder providesDefault rememberSaveableStateHolder()
) {
ConditionalLocalNavigationStateHolderCompositionLocalProvider {
val navigator = rememberNavigator(screens, key, disposeBehavior, LocalNavigator.current)

if (navigator.parent?.disposeBehavior?.disposeNestedNavigators != false) {
Expand Down Expand Up @@ -190,3 +188,18 @@ public data class NavigatorDisposeBehavior(
public fun compositionUniqueId(): String = currentCompositeKeyHash.toString(MaxSupportedRadix)

private val MaxSupportedRadix = 36

@Composable
private fun ConditionalLocalNavigationStateHolderCompositionLocalProvider(content: @Composable () -> Unit) {
val navigatorStateHolder = LocalNavigatorStateHolder.current

if (navigatorStateHolder == null) {
CompositionLocalProvider(
LocalNavigatorStateHolder provides rememberSaveableStateHolder(),
) {
content()
}
} else {
content()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import cafe.adriel.voyager.navigator.LocalNavigatorSaver
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.NavigatorDisposeBehavior

internal val LocalNavigatorStateHolder: ProvidableCompositionLocal<SaveableStateHolder> =
staticCompositionLocalOf { error("LocalNavigatorStateHolder not initialized") }
internal val LocalNavigatorStateHolder: ProvidableCompositionLocal<SaveableStateHolder?> =
staticCompositionLocalOf { null }

@Composable
internal fun rememberNavigator(
Expand All @@ -21,7 +21,7 @@ internal fun rememberNavigator(
disposeBehavior: NavigatorDisposeBehavior,
parent: Navigator?
): Navigator {
val stateHolder = LocalNavigatorStateHolder.current
val stateHolder = LocalNavigatorStateHolder.current ?: error("LocalNavigatorStateHolder not initialized")
val navigatorSaver = LocalNavigatorSaver.current
val saver = remember(navigatorSaver, stateHolder, parent, disposeBehavior) {
navigatorSaver.saver(screens, key, stateHolder, disposeBehavior, parent)
Expand Down