From 0aa116ac701d43212b7f786ae1664ee230d5c838 Mon Sep 17 00:00:00 2001 From: hristogochev Date: Thu, 17 Oct 2024 14:00:13 +0300 Subject: [PATCH] Fix System WindowInsets Not Consumed on iOS Targets When using Nested Navigators --- .../adriel/voyager/navigator/Navigator.kt | 19 ++++++++++++++++--- .../internal/NavigatorSaverInternal.kt | 7 ++++--- 2 files changed, 20 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..0ecd9c26 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,9 @@ 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 +22,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)