Skip to content

Commit

Permalink
fix: save state using screen.key instead of the screen itself
Browse files Browse the repository at this point in the history
  • Loading branch information
adrielcafe committed Aug 8, 2021
1 parent d66a98d commit c9f8fc4
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ data class BasicNavigationScreen(
val wrapContent: Boolean = false
) : Screen {

override val key = "BasicNavigationScreen #$index"

@Composable
override fun Content() {
LifecycleEffect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down

0 comments on commit c9f8fc4

Please sign in to comment.