Last active
August 4, 2023 12:29
-
-
Save arkivanov/c19fbc0dac4b5c296cc4a888426fa17e to your computer and use it in GitHub Desktop.
Revisions
-
arkivanov revised this gist
Dec 7, 2020 . 1 changed file with 1 addition and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 router = remember { -
arkivanov revised this gist
Dec 7, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 stateKeeper = getStateKeeper() val context = remember { DefaultComponentContext( lifecycle = lifecycle, stateKeeper = stateKeeper ) } -
arkivanov revised this gist
Dec 7, 2020 . 1 changed file with 10 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 = 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 -
arkivanov created this gist
Dec 7, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() }