From b02c25d60766df5142507b649026178becb3cec0 Mon Sep 17 00:00:00 2001 From: Arkadii Ivanov Date: Tue, 26 Nov 2024 21:42:31 +0000 Subject: [PATCH] Added key parameter to StateKeeper Android extensions --- state-keeper/api/android/state-keeper.api | 4 ++ .../essenty/statekeeper/AndroidExt.kt | 50 +++++++++++++++++-- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/state-keeper/api/android/state-keeper.api b/state-keeper/api/android/state-keeper.api index ddfc1ee..bf856f1 100644 --- a/state-keeper/api/android/state-keeper.api +++ b/state-keeper/api/android/state-keeper.api @@ -1,7 +1,11 @@ public final class com/arkivanov/essenty/statekeeper/AndroidExtKt { + public static final fun StateKeeper (Landroidx/savedstate/SavedStateRegistry;Ljava/lang/String;ZLkotlin/jvm/functions/Function0;)Lcom/arkivanov/essenty/statekeeper/StateKeeper; public static final fun StateKeeper (Landroidx/savedstate/SavedStateRegistry;ZLkotlin/jvm/functions/Function0;)Lcom/arkivanov/essenty/statekeeper/StateKeeper; + public static synthetic fun StateKeeper$default (Landroidx/savedstate/SavedStateRegistry;Ljava/lang/String;ZLkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lcom/arkivanov/essenty/statekeeper/StateKeeper; public static synthetic fun StateKeeper$default (Landroidx/savedstate/SavedStateRegistry;ZLkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lcom/arkivanov/essenty/statekeeper/StateKeeper; + public static final fun stateKeeper (Landroidx/savedstate/SavedStateRegistryOwner;Ljava/lang/String;ZLkotlin/jvm/functions/Function0;)Lcom/arkivanov/essenty/statekeeper/StateKeeper; public static final fun stateKeeper (Landroidx/savedstate/SavedStateRegistryOwner;ZLkotlin/jvm/functions/Function0;)Lcom/arkivanov/essenty/statekeeper/StateKeeper; + public static synthetic fun stateKeeper$default (Landroidx/savedstate/SavedStateRegistryOwner;Ljava/lang/String;ZLkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lcom/arkivanov/essenty/statekeeper/StateKeeper; public static synthetic fun stateKeeper$default (Landroidx/savedstate/SavedStateRegistryOwner;ZLkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lcom/arkivanov/essenty/statekeeper/StateKeeper; } diff --git a/state-keeper/src/androidMain/kotlin/com/arkivanov/essenty/statekeeper/AndroidExt.kt b/state-keeper/src/androidMain/kotlin/com/arkivanov/essenty/statekeeper/AndroidExt.kt index 18e16e9..4e322c9 100644 --- a/state-keeper/src/androidMain/kotlin/com/arkivanov/essenty/statekeeper/AndroidExt.kt +++ b/state-keeper/src/androidMain/kotlin/com/arkivanov/essenty/statekeeper/AndroidExt.kt @@ -18,17 +18,40 @@ private const val KEY_STATE = "STATE_KEEPER_STATE" fun StateKeeper( savedStateRegistry: SavedStateRegistry, discardSavedState: Boolean = false, - isSavingAllowed: () -> Boolean = { true } + isSavingAllowed: () -> Boolean = { true }, +): StateKeeper = + StateKeeper( + savedStateRegistry = savedStateRegistry, + key = KEY_STATE, + discardSavedState = discardSavedState, + isSavingAllowed = isSavingAllowed, + ) + +/** + * Creates a new instance of [StateKeeper] and attaches it to the provided AndroidX [SavedStateRegistry]. + * + * @param savedStateRegistry a [SavedStateRegistry] to attach the returned [StateKeeper] to. + * @param key a key to access the provided [SavedStateRegistry], to be used by the returned [StateKeeper]. + * @param discardSavedState a flag indicating whether any previously saved state should be discarded or not, + * default value is `false`. + * @param isSavingAllowed called before saving the state. + * When `true` then the state will be saved, otherwise it won't. Default value is `true`. + */ +fun StateKeeper( + savedStateRegistry: SavedStateRegistry, + key: String, + discardSavedState: Boolean = false, + isSavingAllowed: () -> Boolean = { true }, ): StateKeeper { val dispatcher = StateKeeperDispatcher( savedState = savedStateRegistry - .consumeRestoredStateForKey(KEY_STATE) + .consumeRestoredStateForKey(key = key) ?.getSerializableContainer(key = KEY_STATE) ?.takeUnless { discardSavedState }, ) - savedStateRegistry.registerSavedStateProvider(KEY_STATE) { + savedStateRegistry.registerSavedStateProvider(key = key) { Bundle().apply { if (isSavingAllowed()) { putSerializableContainer(key = KEY_STATE, value = dispatcher.save()) @@ -50,9 +73,30 @@ fun StateKeeper( fun SavedStateRegistryOwner.stateKeeper( discardSavedState: Boolean = false, isSavingAllowed: () -> Boolean = { true }, +): StateKeeper = + stateKeeper( + key = KEY_STATE, + discardSavedState = discardSavedState, + isSavingAllowed = isSavingAllowed, + ) + +/** + * Creates a new instance of [StateKeeper] and attaches it to the AndroidX [SavedStateRegistry]. + * + * @param key a key to access this [SavedStateRegistry], to be used by the returned [StateKeeper]. + * @param discardSavedState a flag indicating whether any previously saved state should be discarded or not, + * default value is `false`. + * @param isSavingAllowed called before saving the state. + * When `true` then the state will be saved, otherwise it won't. Default value is `true`. + */ +fun SavedStateRegistryOwner.stateKeeper( + key: String, + discardSavedState: Boolean = false, + isSavingAllowed: () -> Boolean = { true }, ): StateKeeper = StateKeeper( savedStateRegistry = savedStateRegistry, + key = key, discardSavedState = discardSavedState, isSavingAllowed = isSavingAllowed )