Skip to content

Commit

Permalink
Improve remembers related to the local navigation context/navigation …
Browse files Browse the repository at this point in the history
…handle so that the correct instance is returned in situations where it may have changed (i.e. when the LifecycleOwner or View has also changed)
  • Loading branch information
isaac-udy committed Jun 19, 2023
1 parent b374882 commit 26f3ee8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
13 changes: 12 additions & 1 deletion enro-core/src/main/java/dev/enro/core/NavigationContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.LocalView
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
Expand Down Expand Up @@ -243,7 +246,15 @@ public val containerManager: NavigationContainerManager
@Composable
get() {
val viewModelStoreOwner = LocalViewModelStoreOwner.current!!
return remember {

val context = LocalContext.current
val view = LocalView.current
val lifecycleOwner = LocalLifecycleOwner.current

// The navigation context attached to a NavigationHandle may change when the Context, View,
// or LifecycleOwner changes, so we're going to re-query the navigation context whenever
// any of these change, to ensure the container always has an up-to-date NavigationContext
return remember(context, view, lifecycleOwner) {
viewModelStoreOwner
.navigationContext!!
.containerManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import dev.enro.core.*
@Composable
public inline fun <reified T : NavigationKey> navigationHandle(): TypedNavigationHandle<T> {
val navigationHandle = navigationHandle()
return remember {
return remember(navigationHandle) {
navigationHandle.asTyped()
}
}
Expand All @@ -19,7 +19,7 @@ public fun navigationHandle(): NavigationHandle {
val localNavigationHandle = LocalNavigationHandle.current
val localViewModelStoreOwner = LocalViewModelStoreOwner.current

return remember {
return remember(localNavigationHandle, localViewModelStoreOwner) {
localNavigationHandle ?: localViewModelStoreOwner!!.getNavigationHandle()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.LocalView
import dev.enro.core.*
import dev.enro.core.compose.container.ComposableNavigationContainer
import dev.enro.core.compose.container.ContainerRegistrationStrategy
Expand Down Expand Up @@ -67,7 +69,13 @@ public fun rememberNavigationContainer(
): ComposableNavigationContainer {
val localNavigationHandle = navigationHandle()
val context = LocalContext.current
val localNavigationContext = remember(context) {
val view = LocalView.current
val lifecycleOwner = LocalLifecycleOwner.current

// The navigation context attached to a NavigationHandle may change when the Context, View,
// or LifecycleOwner changes, so we're going to re-query the navigation context whenever
// any of these change, to ensure the container always has an up-to-date NavigationContext
val localNavigationContext = remember(context, view, lifecycleOwner) {
localNavigationHandle.requireNavigationContext()
}
val navigationContainer = remember(localNavigationContext.containerManager) {
Expand Down

0 comments on commit 26f3ee8

Please sign in to comment.