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",
"**/SystemUiController.kt",
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,7 @@ public fun CircuitContent(
circuit.onUnavailableContent,
key: Any? = screen,
) {
val navigator =
remember(onNavEvent) {
object : Navigator {
override fun goTo(screen: Screen) {
onNavEvent(NavEvent.GoTo(screen))
}

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

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

override fun peek(): Screen = screen

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

<application>
<activity android:name="androidx.activity.ComponentActivity"/>
<activity android:name="com.slack.circuit.retained.android.RetainedStateHolderTest$Activity"/>
</application>
</manifest>
Loading
Loading