com.mattshoe.shoebex.kdux.kotlinx.serialization.StoreDslMenuExtensions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Kdux.kotlinx-serialization Show documentation
Show all versions of Kdux.kotlinx-serialization Show documentation
Kdux is a Kotlin-based, platform-agnostic state management library that implements the Redux pattern,
providing structured concurrency with built-in coroutine support. It is designed to integrate seamlessly
with any Kotlin project, particularly excelling in Android applications using MVI architecture.
The newest version!
package com.mattshoe.shoebex.kdux.kotlinx.serialization
import kdux.dsl.StoreDslMenu
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
/**
* Adds a [PersistenceEnhancer] to the store that uses `kotlinx-serialization` to serialize State objects, enabling automatic persistence and restoration of the store's state.
* This function is designed to make it easy to persist and restore the state of your store across application restarts
* or other lifecycle events.
*
* This function enables automatic state persistence for the store, leveraging Kotlinx Serialization to serialize
* the state and store it in a file. The state is restored from the file when the store is initialized.
*
* ### Important Details:
*
* The [key] parameter is used to determine the filename or unique identifier for the stored state. You must ensure that the
* key is unique if multiple stores are being persisted, to avoid conflicts. The [key] can be used to associate user-data to avoid
* exposing the wrong user's data to another, by appending a user-id or otherwise to the key.
*
* @param State The type representing the state managed by the store. The state must be serializable by Kotlinx Serialization.
* @param Action The type representing the actions that can be dispatched to the store.
* @param key A unique identifier for the persisted state, used to determine the filename or key under which the state is stored.
* @param onError A lambda function invoked if an error occurs during serialization or deserialization. Defaults to a no-op.
*/
@OptIn(ExperimentalSerializationApi::class)
inline fun StoreDslMenu.persistWithKotlinxSerialization(
key: String,
noinline onError: (State?, Throwable) -> Unit = { _, _ -> }
) {
persist(
key,
serializer = { state, outputStream ->
outputStream.write(
Json.encodeToString(state).toByteArray()
)
},
deserializer = {
Json.decodeFromStream(it)
},
onError
)
}
@OptIn(ExperimentalSerializationApi::class)
fun StoreDslMenu.persist(
key: String,
onError: (State?, Throwable) -> Unit
) {
persist(
key,
serializer = { state, outputStream ->
TODO()
},
deserializer = {
TODO()
},
onError
)
}