Skip to content

Commit

Permalink
Merge pull request #182 from arkivanov/state-keeper-key
Browse files Browse the repository at this point in the history
Added key parameter to StateKeeper Android extensions
  • Loading branch information
arkivanov authored Nov 26, 2024
2 parents afdcd68 + b02c25d commit 0879b2d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
4 changes: 4 additions & 0 deletions state-keeper/api/android/state-keeper.api
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
)

0 comments on commit 0879b2d

Please sign in to comment.