Skip to content

Instantly share code, notes, and snippets.

@arkivanov
Last active August 4, 2023 12:29
Show Gist options
  • Select an option

  • Save arkivanov/c19fbc0dac4b5c296cc4a888426fa17e to your computer and use it in GitHub Desktop.

Select an option

Save arkivanov/c19fbc0dac4b5c296cc4a888426fa17e to your computer and use it in GitHub Desktop.

Revisions

  1. arkivanov revised this gist Dec 7, 2020. 1 changed file with 1 addition and 7 deletions.
    8 changes: 1 addition & 7 deletions Navigator.kt
    Original file line number Diff line number Diff line change
    @@ -7,13 +7,7 @@ fun <T : Parcelable> Navigator(
    val lifecycle = getLifecycle()
    val stateKeeper = getStateKeeper()

    val context =
    remember {
    DefaultComponentContext(
    lifecycle = lifecycle,
    stateKeeper = stateKeeper
    )
    }
    val context = remember { DefaultComponentContext(lifecycle = lifecycle, stateKeeper = stateKeeper) }

    val router =
    remember {
  2. arkivanov revised this gist Dec 7, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Navigator.kt
    Original file line number Diff line number Diff line change
    @@ -5,13 +5,13 @@ fun <T : Parcelable> Navigator(
    content: @Composable Router<T>.(T) -> Unit
    ) {
    val lifecycle = getLifecycle()
    val savedStateRegistry = getStateKeeper()
    val stateKeeper = getStateKeeper()

    val context =
    remember {
    DefaultComponentContext(
    lifecycle = lifecycle,
    stateKeeper = savedStateRegistry
    stateKeeper = stateKeeper
    )
    }

  3. arkivanov revised this gist Dec 7, 2020. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion Navigator.kt
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ fun <T : Parcelable> Navigator(
    configurationClass: KClass<out T>,
    content: @Composable Router<T>.(T) -> Unit
    ) {
    val lifecycle = remember { LifecycleRegistry() }
    val lifecycle = getLifecycle()
    val savedStateRegistry = getStateKeeper()

    val context =
    @@ -31,6 +31,15 @@ fun <T : Parcelable> Navigator(
    router.content(routerState.activeChild.configuration)
    }

    @Composable
    private fun getLifecycle(): Lifecycle {
    val lifecycle = remember { LifecycleRegistry() }
    onActive { lifecycle.resume() }
    onDispose { lifecycle.destroy() }

    return lifecycle
    }

    @Composable
    private fun getStateKeeper(key: String = "state"): StateKeeper {
    val savedStateRegistry: UiSavedStateRegistry? = AmbientUiSavedStateRegistry.current
  4. arkivanov created this gist Dec 7, 2020.
    55 changes: 55 additions & 0 deletions Navigator.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    @Composable
    fun <T : Parcelable> Navigator(
    initialConfiguration: () -> T,
    configurationClass: KClass<out T>,
    content: @Composable Router<T>.(T) -> Unit
    ) {
    val lifecycle = remember { LifecycleRegistry() }
    val savedStateRegistry = getStateKeeper()

    val context =
    remember {
    DefaultComponentContext(
    lifecycle = lifecycle,
    stateKeeper = savedStateRegistry
    )
    }

    val router =
    remember {
    val decomposeRouter =
    context.router(
    initialConfiguration = initialConfiguration,
    configurationClass = configurationClass,
    componentFactory = { configuration, _ -> configuration }
    )

    object : Router<T>, com.arkivanov.decompose.Router<T, T> by decomposeRouter {}
    }

    val routerState by router.state.asState()
    router.content(routerState.activeChild.configuration)
    }

    @Composable
    private fun getStateKeeper(key: String = "state"): StateKeeper {
    val savedStateRegistry: UiSavedStateRegistry? = AmbientUiSavedStateRegistry.current

    val dispatcher =
    remember {
    StateKeeperDispatcher(savedStateRegistry?.consumeRestored(key) as ParcelableContainer?)
    }

    if (savedStateRegistry != null) {
    val stateProvider = dispatcher::save
    onActive { savedStateRegistry.registerProvider(key, stateProvider) }
    onDispose { savedStateRegistry.unregisterProvider(key, stateProvider) }
    }

    return dispatcher
    }

    interface Router<in T : Parcelable> {
    fun push(configuration: T)
    fun pop()
    }