Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP RetainedStateHolder #1168

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ allprojects {
"**/FilterList.kt",
"**/Remove.kt",
"**/Pets.kt",
"**/RetainedStateHolder*.kt",
"**/RetainedStateRestorationTester.kt",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,7 @@ public fun CircuitContent(
unavailableContent: (@Composable (screen: Screen, modifier: Modifier) -> Unit) =
circuit.onUnavailableContent,
) {
val navigator =
remember(onNavEvent) {
object : Navigator {
override fun goTo(screen: Screen) {
onNavEvent(NavEvent.GoTo(screen))
}

override fun resetRoot(
newRoot: Screen,
saveState: Boolean,
restoreState: Boolean,
): List<Screen> {
onNavEvent(NavEvent.ResetRoot(newRoot, saveState, restoreState))
return emptyList()
}

override fun pop(): Screen? {
onNavEvent(NavEvent.Pop)
return null
}

override fun peek(): Screen = screen

override fun peekBackStack(): List<Screen> = listOf(screen)
}
}
val navigator = remember(onNavEvent) { Navigator.navEventNavigator(screen, onNavEvent) }
CircuitContent(screen, navigator, modifier, circuit, unavailableContent)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,35 @@ public fun Navigator.onNavEvent(event: NavEvent) {
}
}

public fun Navigator.Companion.navEventNavigator(
screen: Screen,
onNavEvent: (event: NavEvent) -> Unit,
): Navigator {
return object : Navigator {
override fun goTo(screen: Screen) {
onNavEvent(NavEvent.GoTo(screen))
}

override fun resetRoot(
newRoot: Screen,
saveState: Boolean,
restoreState: Boolean,
): List<Screen> {
onNavEvent(NavEvent.ResetRoot(newRoot, saveState, restoreState))
return emptyList()
}

override fun pop(): Screen? {
onNavEvent(NavEvent.Pop)
return null
}

override fun peek(): Screen = screen

override fun peekBackStack(): List<Screen> = listOf(screen)
}
}

/** A sealed navigation interface intended to be used when making a navigation callback. */
public sealed interface NavEvent : CircuitUiEvent {
/** Corresponds to [Navigator.pop]. */
Expand Down
Loading
Loading