-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added StateKeeperOwner.withSavedState and InstanceKeeperOwner.retaine…
…dInstance extensions
- Loading branch information
Showing
7 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,8 @@ final fun com.arkivanov.essenty.instancekeeper/InstanceKeeperDispatcher(): com.a | |
final inline fun <#A: com.arkivanov.essenty.instancekeeper/InstanceKeeper.Instance> (com.arkivanov.essenty.instancekeeper/InstanceKeeper).com.arkivanov.essenty.instancekeeper/getOrCreate(kotlin/Any, kotlin/Function0<#A>): #A // com.arkivanov.essenty.instancekeeper/getOrCreate|[email protected](kotlin.Any;kotlin.Function0<0:0>){0§<com.arkivanov.essenty.instancekeeper.InstanceKeeper.Instance>}[0] | ||
final inline fun <#A: kotlin/Any?> (com.arkivanov.essenty.instancekeeper/InstanceKeeper).com.arkivanov.essenty.instancekeeper/getOrCreateSimple(kotlin/Any, kotlin/Function0<#A>): #A // com.arkivanov.essenty.instancekeeper/getOrCreateSimple|getOrCreateSimple@com.arkivanov.essenty.instancekeeper.InstanceKeeper(kotlin.Any;kotlin.Function0<0:0>){0§<kotlin.Any?>}[0] | ||
final inline fun <#A: reified com.arkivanov.essenty.instancekeeper/InstanceKeeper.Instance> (com.arkivanov.essenty.instancekeeper/InstanceKeeper).com.arkivanov.essenty.instancekeeper/getOrCreate(kotlin/Function0<#A>): #A // com.arkivanov.essenty.instancekeeper/getOrCreate|[email protected](kotlin.Function0<0:0>){0§<com.arkivanov.essenty.instancekeeper.InstanceKeeper.Instance>}[0] | ||
final inline fun <#A: reified com.arkivanov.essenty.instancekeeper/InstanceKeeper.Instance> (com.arkivanov.essenty.instancekeeper/InstanceKeeperOwner).com.arkivanov.essenty.instancekeeper/retainedInstance(kotlin/Any, kotlin/Function0<#A>): #A // com.arkivanov.essenty.instancekeeper/retainedInstance|retainedInstance@com.arkivanov.essenty.instancekeeper.InstanceKeeperOwner(kotlin.Any;kotlin.Function0<0:0>){0§<com.arkivanov.essenty.instancekeeper.InstanceKeeper.Instance>}[0] | ||
final inline fun <#A: reified com.arkivanov.essenty.instancekeeper/InstanceKeeper.Instance> (com.arkivanov.essenty.instancekeeper/InstanceKeeperOwner).com.arkivanov.essenty.instancekeeper/retainedInstance(kotlin/Function0<#A>): #A // com.arkivanov.essenty.instancekeeper/retainedInstance|retainedInstance@com.arkivanov.essenty.instancekeeper.InstanceKeeperOwner(kotlin.Function0<0:0>){0§<com.arkivanov.essenty.instancekeeper.InstanceKeeper.Instance>}[0] | ||
final inline fun <#A: reified kotlin/Any?> (com.arkivanov.essenty.instancekeeper/InstanceKeeper).com.arkivanov.essenty.instancekeeper/getOrCreateSimple(kotlin/Function0<#A>): #A // com.arkivanov.essenty.instancekeeper/getOrCreateSimple|getOrCreateSimple@com.arkivanov.essenty.instancekeeper.InstanceKeeper(kotlin.Function0<0:0>){0§<kotlin.Any?>}[0] | ||
final inline fun <#A: reified kotlin/Any?> (com.arkivanov.essenty.instancekeeper/InstanceKeeperOwner).com.arkivanov.essenty.instancekeeper/retainedSimpleInstance(kotlin/Any, kotlin/Function0<#A>): #A // com.arkivanov.essenty.instancekeeper/retainedSimpleInstance|retainedSimpleInstance@com.arkivanov.essenty.instancekeeper.InstanceKeeperOwner(kotlin.Any;kotlin.Function0<0:0>){0§<kotlin.Any?>}[0] | ||
final inline fun <#A: reified kotlin/Any?> (com.arkivanov.essenty.instancekeeper/InstanceKeeperOwner).com.arkivanov.essenty.instancekeeper/retainedSimpleInstance(kotlin/Function0<#A>): #A // com.arkivanov.essenty.instancekeeper/retainedSimpleInstance|retainedSimpleInstance@com.arkivanov.essenty.instancekeeper.InstanceKeeperOwner(kotlin.Function0<0:0>){0§<kotlin.Any?>}[0] |
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,4 +32,6 @@ final fun <#A: kotlin/Any> (com.arkivanov.essenty.statekeeper/SerializableContai | |
final fun <#A: kotlin/Any> com.arkivanov.essenty.statekeeper/SerializableContainer(#A?, kotlinx.serialization/SerializationStrategy<#A>): com.arkivanov.essenty.statekeeper/SerializableContainer // com.arkivanov.essenty.statekeeper/SerializableContainer|SerializableContainer(0:0?;kotlinx.serialization.SerializationStrategy<0:0>){0§<kotlin.Any>}[0] | ||
final fun <#A: kotlin/Any> com.arkivanov.essenty.statekeeper/polymorphicSerializer(kotlin.reflect/KClass<#A>, kotlinx.serialization.modules/SerializersModule): kotlinx.serialization/KSerializer<#A> // com.arkivanov.essenty.statekeeper/polymorphicSerializer|polymorphicSerializer(kotlin.reflect.KClass<0:0>;kotlinx.serialization.modules.SerializersModule){0§<kotlin.Any>}[0] | ||
final fun com.arkivanov.essenty.statekeeper/StateKeeperDispatcher(com.arkivanov.essenty.statekeeper/SerializableContainer? = ...): com.arkivanov.essenty.statekeeper/StateKeeperDispatcher // com.arkivanov.essenty.statekeeper/StateKeeperDispatcher|StateKeeperDispatcher(com.arkivanov.essenty.statekeeper.SerializableContainer?){}[0] | ||
final inline fun <#A: kotlin/Any?, #B: kotlin/Any> (com.arkivanov.essenty.statekeeper/StateKeeper).com.arkivanov.essenty.statekeeper/withSavedState(kotlin/String, kotlinx.serialization/KSerializer<#B>, crossinline kotlin/Function1<#A, #B>, kotlin/Function1<#B?, #A>): #A // com.arkivanov.essenty.statekeeper/withSavedState|[email protected](kotlin.String;kotlinx.serialization.KSerializer<0:1>;kotlin.Function1<0:0,0:1>;kotlin.Function1<0:1?,0:0>){0§<kotlin.Any?>;1§<kotlin.Any>}[0] | ||
final inline fun <#A: kotlin/Any?, #B: kotlin/Any> (com.arkivanov.essenty.statekeeper/StateKeeperOwner).com.arkivanov.essenty.statekeeper/withSavedState(kotlin/String, kotlinx.serialization/KSerializer<#B>, crossinline kotlin/Function1<#A, #B>, kotlin/Function1<#B?, #A>): #A // com.arkivanov.essenty.statekeeper/withSavedState|withSavedState@com.arkivanov.essenty.statekeeper.StateKeeperOwner(kotlin.String;kotlinx.serialization.KSerializer<0:1>;kotlin.Function1<0:0,0:1>;kotlin.Function1<0:1?,0:0>){0§<kotlin.Any?>;1§<kotlin.Any>}[0] | ||
final inline fun <#A: reified kotlin/Any> com.arkivanov.essenty.statekeeper/polymorphicSerializer(kotlinx.serialization.modules/SerializersModule): kotlinx.serialization/KSerializer<#A> // com.arkivanov.essenty.statekeeper/polymorphicSerializer|polymorphicSerializer(kotlinx.serialization.modules.SerializersModule){0§<kotlin.Any>}[0] |
42 changes: 42 additions & 0 deletions
42
state-keeper/src/commonMain/kotlin/com/arkivanov/essenty/statekeeper/StateKeeperExt.kt
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.arkivanov.essenty.statekeeper | ||
|
||
import kotlinx.serialization.KSerializer | ||
|
||
/** | ||
* Helper function for creating objects with a saved state. | ||
* | ||
* @param key a key for saving and restoring the state. | ||
* @param serializer a [KSerializer] for serializing and deserializing the state. | ||
* @param saveableState a function that selects a state [S] from the resulting | ||
* object [T] and returns it for saving. | ||
* @param init a function that accepts the previously saved state [S] (if any) and | ||
* returns an object of type [T]. | ||
* @return an object of type [T] returned by [init] function. | ||
*/ | ||
inline fun <T, S : Any> StateKeeper.withSavedState( | ||
key: String, | ||
serializer: KSerializer<S>, | ||
crossinline saveableState: (T) -> S, | ||
init: (savedState: S?) -> T, | ||
): T { | ||
val result = init(consume(key = key, strategy = serializer)) | ||
register(key = key, strategy = serializer) { saveableState(result) } | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* A convenience function for [StateKeeper.withSavedState]. | ||
*/ | ||
inline fun <T, S : Any> StateKeeperOwner.withSavedState( | ||
key: String, | ||
serializer: KSerializer<S>, | ||
crossinline saveableState: (T) -> S, | ||
init: (savedState: S?) -> T, | ||
): T = | ||
stateKeeper.withSavedState( | ||
key = key, | ||
serializer = serializer, | ||
saveableState = saveableState, | ||
init = init, | ||
) |
36 changes: 36 additions & 0 deletions
36
state-keeper/src/commonTest/kotlin/com/arkivanov/essenty/statekeeper/StateKeeperExtTest.kt
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.arkivanov.essenty.statekeeper | ||
|
||
import kotlinx.serialization.builtins.serializer | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class StateKeeperExtTest { | ||
|
||
@Test | ||
fun withSavedState_saves_and_restores_state() { | ||
val oldDispatcher = StateKeeperDispatcher() | ||
|
||
val holder = | ||
oldDispatcher.withSavedState( | ||
key = "SAVED_STATE", | ||
serializer = Int.serializer(), | ||
saveableState = { it.state }, | ||
) { Holder(state = it ?: 0) } | ||
|
||
holder.state++ | ||
|
||
val savedState = oldDispatcher.save().serializeAndDeserialize() | ||
val newDispatcher = StateKeeperDispatcher(savedState = savedState) | ||
|
||
val newHolder = | ||
newDispatcher.withSavedState( | ||
key = "SAVED_STATE", | ||
serializer = Int.serializer(), | ||
saveableState = { it.state }, | ||
) { Holder(state = it ?: 0) } | ||
|
||
assertEquals(1, newHolder.state) | ||
} | ||
|
||
class Holder(var state: Int) | ||
} |