From cfd3bbd35d27db35438b6e6909406379b3f3f7ba Mon Sep 17 00:00:00 2001 From: hristogochev Date: Fri, 18 Oct 2024 15:49:51 +0300 Subject: [PATCH] Replace providesDefault in Navigator with provides and an if-check --- .../adriel/voyager/navigator/Navigator.kt | 19 ++++++++++++++++--- .../internal/NavigatorSaverInternal.kt | 6 +++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/voyager-navigator/src/commonMain/kotlin/cafe/adriel/voyager/navigator/Navigator.kt b/voyager-navigator/src/commonMain/kotlin/cafe/adriel/voyager/navigator/Navigator.kt index f9239a71..bc6b0400 100644 --- a/voyager-navigator/src/commonMain/kotlin/cafe/adriel/voyager/navigator/Navigator.kt +++ b/voyager-navigator/src/commonMain/kotlin/cafe/adriel/voyager/navigator/Navigator.kt @@ -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) { @@ -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() + } +} diff --git a/voyager-navigator/src/commonMain/kotlin/cafe/adriel/voyager/navigator/internal/NavigatorSaverInternal.kt b/voyager-navigator/src/commonMain/kotlin/cafe/adriel/voyager/navigator/internal/NavigatorSaverInternal.kt index 4411f0d2..7e5aa260 100644 --- a/voyager-navigator/src/commonMain/kotlin/cafe/adriel/voyager/navigator/internal/NavigatorSaverInternal.kt +++ b/voyager-navigator/src/commonMain/kotlin/cafe/adriel/voyager/navigator/internal/NavigatorSaverInternal.kt @@ -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 = - staticCompositionLocalOf { error("LocalNavigatorStateHolder not initialized") } +internal val LocalNavigatorStateHolder: ProvidableCompositionLocal = + staticCompositionLocalOf { null } @Composable internal fun rememberNavigator( @@ -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)