From c9f8fc4d1c783ea24e04603f02df513dbac8ffb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adriel=20Caf=C3=A9?= Date: Sun, 8 Aug 2021 17:47:59 -0300 Subject: [PATCH] fix: save state using screen.key instead of the screen itself --- gradle.properties | 2 +- .../voyager/sample/basicNavigation/BasicNavigationScreen.kt | 2 ++ .../voyager/sample/tabNavigation/TabNavigationActivity.kt | 2 +- .../adriel/voyager/sample/tabNavigation/tabs/TabContent.kt | 2 +- .../src/main/java/cafe/adriel/voyager/core/screen/Screen.kt | 5 ++++- .../main/java/cafe/adriel/voyager/navigator/Navigator.kt | 6 +++--- .../src/main/java/cafe/adriel/voyager/navigator/tab/Tab.kt | 2 +- .../cafe/adriel/voyager/transitions/ScreenTransition.kt | 2 +- 8 files changed, 14 insertions(+), 9 deletions(-) diff --git a/gradle.properties b/gradle.properties index 1ed9bcc5..d34f574f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ kotlin.code.style=official # Maven GROUP=cafe.adriel.voyager -VERSION_NAME=1.0.0-beta03 +VERSION_NAME=1.0.0-beta04 POM_DESCRIPTION=A pragmatic navigation library for Jetpack Compose POM_INCEPTION_YEAR=2021 diff --git a/sample/src/main/java/cafe/adriel/voyager/sample/basicNavigation/BasicNavigationScreen.kt b/sample/src/main/java/cafe/adriel/voyager/sample/basicNavigation/BasicNavigationScreen.kt index e2d4a5cb..c295cc18 100644 --- a/sample/src/main/java/cafe/adriel/voyager/sample/basicNavigation/BasicNavigationScreen.kt +++ b/sample/src/main/java/cafe/adriel/voyager/sample/basicNavigation/BasicNavigationScreen.kt @@ -27,6 +27,8 @@ data class BasicNavigationScreen( val wrapContent: Boolean = false ) : Screen { + override val key = "BasicNavigationScreen #$index" + @Composable override fun Content() { LifecycleEffect( diff --git a/sample/src/main/java/cafe/adriel/voyager/sample/tabNavigation/TabNavigationActivity.kt b/sample/src/main/java/cafe/adriel/voyager/sample/tabNavigation/TabNavigationActivity.kt index da462487..4bcf0b47 100644 --- a/sample/src/main/java/cafe/adriel/voyager/sample/tabNavigation/TabNavigationActivity.kt +++ b/sample/src/main/java/cafe/adriel/voyager/sample/tabNavigation/TabNavigationActivity.kt @@ -57,7 +57,7 @@ class TabNavigationActivity : ComponentActivity() { val tabNavigator = LocalTabNavigator.current BottomNavigationItem( - selected = tabNavigator.current == tab, + selected = tabNavigator.current.key == tab.key, onClick = { tabNavigator.current = tab }, icon = { Icon(painter = tab.options.icon!!, contentDescription = tab.options.title) } ) diff --git a/sample/src/main/java/cafe/adriel/voyager/sample/tabNavigation/tabs/TabContent.kt b/sample/src/main/java/cafe/adriel/voyager/sample/tabNavigation/tabs/TabContent.kt index 1eb7d241..514c3698 100644 --- a/sample/src/main/java/cafe/adriel/voyager/sample/tabNavigation/tabs/TabContent.kt +++ b/sample/src/main/java/cafe/adriel/voyager/sample/tabNavigation/tabs/TabContent.kt @@ -64,7 +64,7 @@ private fun RowScope.TabNavigationButton( val tabNavigator = LocalTabNavigator.current Button( - enabled = tabNavigator.current != tab, + enabled = tabNavigator.current.key != tab.key, onClick = { tabNavigator.current = tab }, modifier = Modifier.weight(1f) ) { diff --git a/voyager-core/src/main/java/cafe/adriel/voyager/core/screen/Screen.kt b/voyager-core/src/main/java/cafe/adriel/voyager/core/screen/Screen.kt index ecf6d7f0..e5bf7df4 100644 --- a/voyager-core/src/main/java/cafe/adriel/voyager/core/screen/Screen.kt +++ b/voyager-core/src/main/java/cafe/adriel/voyager/core/screen/Screen.kt @@ -15,7 +15,10 @@ public fun Screen.LifecycleEffect( } } -public fun interface Screen : Serializable { +public interface Screen : Serializable { + + public val key: String + get() = this::class.qualifiedName ?: error("Default key not found, please override it and set your own key") @Composable public fun Content() diff --git a/voyager-navigator/src/main/java/cafe/adriel/voyager/navigator/Navigator.kt b/voyager-navigator/src/main/java/cafe/adriel/voyager/navigator/Navigator.kt index 673e68d5..53680abe 100644 --- a/voyager-navigator/src/main/java/cafe/adriel/voyager/navigator/Navigator.kt +++ b/voyager-navigator/src/main/java/cafe/adriel/voyager/navigator/Navigator.kt @@ -34,7 +34,7 @@ public fun CurrentScreen() { val navigator = LocalNavigator.currentOrThrow val currentScreen = navigator.lastItem - navigator.stateHolder.SaveableStateProvider(currentScreen) { + navigator.stateHolder.SaveableStateProvider(currentScreen.key) { currentScreen.Content() } } @@ -74,10 +74,10 @@ public fun Navigator( NavigatorBackHandler(navigator, onBackPressed) - DisposableEffect(currentScreen) { + DisposableEffect(currentScreen.key) { onDispose { if (navigator.lastEvent in disposableEvents) { - navigator.stateHolder.removeState(currentScreen) + navigator.stateHolder.removeState(currentScreen.key) } } } diff --git a/voyager-tab-navigator/src/main/java/cafe/adriel/voyager/navigator/tab/Tab.kt b/voyager-tab-navigator/src/main/java/cafe/adriel/voyager/navigator/tab/Tab.kt index 21768e5d..8d893fbc 100644 --- a/voyager-tab-navigator/src/main/java/cafe/adriel/voyager/navigator/tab/Tab.kt +++ b/voyager-tab-navigator/src/main/java/cafe/adriel/voyager/navigator/tab/Tab.kt @@ -9,7 +9,7 @@ public fun CurrentTab() { val tabNavigator = LocalTabNavigator.current val currentTab = tabNavigator.current - tabNavigator.stateHolder.SaveableStateProvider(currentTab) { + tabNavigator.stateHolder.SaveableStateProvider(currentTab.key) { currentTab.Content() } } diff --git a/voyager-transitions/src/main/java/cafe/adriel/voyager/transitions/ScreenTransition.kt b/voyager-transitions/src/main/java/cafe/adriel/voyager/transitions/ScreenTransition.kt index 5a40001e..c895e2a3 100644 --- a/voyager-transitions/src/main/java/cafe/adriel/voyager/transitions/ScreenTransition.kt +++ b/voyager-transitions/src/main/java/cafe/adriel/voyager/transitions/ScreenTransition.kt @@ -58,7 +58,7 @@ public fun ScreenTransition( Box(modifier) { items.forEach { key(it.screen) { - navigator.stateHolder.SaveableStateProvider(it.screen) { + navigator.stateHolder.SaveableStateProvider(it.screen.key) { it.content() } }