Skip to content

Commit

Permalink
Navigator - Pass PopResult to onRootPop (#1256)
Browse files Browse the repository at this point in the history
For some cases (mostly interop) it's useful to pass the `PopResult` to
the default `onRootPop` lambda.
  • Loading branch information
stagg authored Mar 5, 2024
1 parent 93cdbe3 commit 11b3246
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.runtime.Composable
import com.slack.circuit.backstack.BackStack
import com.slack.circuit.backstack.BackStack.Record
import com.slack.circuit.runtime.Navigator
import com.slack.circuit.runtime.screen.PopResult

/**
* Returns a new [Navigator] for navigating within [CircuitContents][CircuitContent]. Delegates
Expand All @@ -34,11 +35,11 @@ public fun rememberCircuitNavigator(
}

@Composable
private fun backDispatcherRootPop(): () -> Unit {
private fun backDispatcherRootPop(): (result: PopResult?) -> Unit {
val onBackPressedDispatcher =
LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher
?: error("No OnBackPressedDispatcherOwner found, unable to handle root navigation pops.")
return onBackPressedDispatcher::onBackPressed
return { onBackPressedDispatcher.onBackPressed() }
}

private fun onBack(backStack: BackStack<out Record>, navigator: Navigator): () -> Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import kotlinx.collections.immutable.persistentListOf
@Composable
public fun rememberCircuitNavigator(
backStack: BackStack<out Record>,
onRootPop: () -> Unit,
onRootPop: (result: PopResult?) -> Unit,
): Navigator {
return remember { Navigator(backStack, onRootPop) }
}
Expand All @@ -40,12 +40,14 @@ public fun rememberCircuitNavigator(
* button.
* @see NavigableCircuitContent
*/
public fun Navigator(backStack: BackStack<out Record>, onRootPop: () -> Unit): Navigator =
NavigatorImpl(backStack, onRootPop)
public fun Navigator(
backStack: BackStack<out Record>,
onRootPop: (result: PopResult?) -> Unit,
): Navigator = NavigatorImpl(backStack, onRootPop)

internal class NavigatorImpl(
private val backStack: BackStack<out Record>,
private val onRootPop: () -> Unit,
private val onRootPop: (result: PopResult?) -> Unit,
) : Navigator {

init {
Expand All @@ -58,7 +60,7 @@ internal class NavigatorImpl(

override fun pop(result: PopResult?): Screen? {
if (backStack.isAtRoot) {
onRootPop()
onRootPop(result)
return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fun main() = application {
) {
val initialBackStack = persistentListOf<Screen>(DesktopCounterScreen)
val backStack = rememberSaveableBackStack(initialBackStack)
val navigator = rememberCircuitNavigator(backStack, ::exitApplication)
val navigator = rememberCircuitNavigator(backStack) { exitApplication() }

val circuit: Circuit =
Circuit.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fun main() {
application {
val initialBackStack = persistentListOf<Screen>(HomeScreen)
val backStack = rememberSaveableBackStack(initialBackStack)
val circuitNavigator = rememberCircuitNavigator(backStack, ::exitApplication)
val circuitNavigator = rememberCircuitNavigator(backStack) { exitApplication() }
val navigator =
remember(circuitNavigator) {
object : Navigator by circuitNavigator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fun main() {
Window(title = "Tutorial", onCloseRequest = ::exitApplication) {
MaterialTheme {
val backStack = rememberSaveableBackStack(InboxScreen)
val navigator = rememberCircuitNavigator(backStack, ::exitApplication)
val navigator = rememberCircuitNavigator(backStack) { exitApplication() }
CircuitCompositionLocals(circuit) {
NavigableCircuitContent(navigator = navigator, backStack = backStack)
}
Expand Down

0 comments on commit 11b3246

Please sign in to comment.