-
-
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
124 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
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
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
package com.arkivanov.essenty.statekeeper | ||
|
||
import com.arkivanov.essenty.utils.internal.ExperimentalEssentyApi | ||
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 state a function that selects a state [S] from the resulting | ||
* object [T] and returns it for saving. | ||
* @param block 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 [block] function. | ||
*/ | ||
@ExperimentalEssentyApi | ||
inline fun <T, S : Any> StateKeeper.withSavedState( | ||
key: String, | ||
serializer: KSerializer<S>, | ||
crossinline state: (T) -> S, | ||
block: (savedState: S?) -> T, | ||
): T { | ||
val result = block(consume(key = key, strategy = serializer)) | ||
register(key = key, strategy = serializer) { state(result) } | ||
|
||
return result | ||
} | ||
|
||
/** | ||
* A convenience function for [StateKeeper.withSavedState]. | ||
*/ | ||
@ExperimentalEssentyApi | ||
inline fun <T, S : Any> StateKeeperOwner.withSavedState( | ||
key: String, | ||
serializer: KSerializer<S>, | ||
crossinline state: (T) -> S, | ||
block: (savedState: S?) -> T, | ||
): T = | ||
stateKeeper.withSavedState( | ||
key = key, | ||
serializer = serializer, | ||
state = state, | ||
block = block, | ||
) |
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
package com.arkivanov.essenty.statekeeper | ||
|
||
import com.arkivanov.essenty.utils.internal.ExperimentalEssentyApi | ||
import kotlinx.serialization.builtins.serializer | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
@OptIn(ExperimentalEssentyApi::class) | ||
class StateKeeperExtTest { | ||
|
||
@Test | ||
fun withSavedState_saves_and_restores_state() { | ||
val oldDispatcher = StateKeeperDispatcher() | ||
|
||
val holder = | ||
oldDispatcher.withSavedState(key = "SAVED_STATE", serializer = Int.serializer(), state = Holder::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(), state = Holder::state) { | ||
Holder(state = it ?: 0) | ||
} | ||
|
||
assertEquals(1, newHolder.state) | ||
} | ||
|
||
private class Holder(var state: Int) | ||
} |