Class that contains a store that maintains compose state beyond individual Android components.

There is no limitation on where these instance should be instantiated as that is left up to the developer.

Summary

Public constructors

Cmn

Public functions

State<T>
<T : Any?> getState(stateKey: StateStoreKey<T>, defaultValue: T)

Returns the state associated with the given key.

Cmn
Unit
<T : Any?> setState(stateKey: StateStoreKey<T>, value: T)

Sets the state for the given key.

Cmn
inline Unit
<T : Any?> updateState(
    stateKey: StateStoreKey<T>,
    defaultValue: T,
    update: (T) -> T
)

Retrieves the current state and uses that with the update lambda to set a new state for the specified key.

Cmn

Public properties

Set<StateStoreKey<*>>

The set of keys that are currently added to this StateStore.

Cmn

Extension functions

suspend Unit

Tracks changes to StateStore and syncs keys implementing PersistToDataStore to DataStore.

android
suspend Unit
StateStore.syncToDataStore(path: String, coroutineScope: CoroutineScope)

Tracks changes to StateStore and persists keys implementing PersistToDataStore to a DataStore backed by a file.

android

Public constructors

StateStore

StateStore()

Public functions

getState

fun <T : Any?> getState(
    stateKey: StateStoreKey<T>,
    defaultValue: T = stateKey.defaultValue
): State<T>

Returns the state associated with the given key.

Parameters
stateKey: StateStoreKey<T>

the key of the state to retrieve

defaultValue: T = stateKey.defaultValue

the value to put if there is no value for the key

setState

fun <T : Any?> setState(stateKey: StateStoreKey<T>, value: T): Unit

Sets the state for the given key.

If this is the first time the state is being set a listener is added to allow the given key to be automatically cleared from the map based on the StateStoreKey.autoClearKey and StateStoreKey.shouldClearState. For the automatic clearing, StateStoreKey.autoClearKey must be non-null, the value associated with this key must be non-null, and the StateStoreKey.shouldClearState condition must return true. AS long as all 3 of those conditions are not met, the listener will not be removed.

Parameters
stateKey: StateStoreKey<T>

the key to set a value for

value: T

the value to set inside the StateStore

updateState

inline fun <T : Any?> updateState(
    stateKey: StateStoreKey<T>,
    defaultValue: T = stateKey.defaultValue,
    update: (T) -> T
): Unit

Retrieves the current state and uses that with the update lambda to set a new state for the specified key.

Parameters
stateKey: StateStoreKey<T>

the key to update

defaultValue: T = stateKey.defaultValue

the value to use if no state has been set for the key

update: (T) -> T

lambda used to update the state using the current state

Public properties

keys

val keys: Set<StateStoreKey<*>>

The set of keys that are currently added to this StateStore.

This can be used in conjunction with the androidx.stateStore.transform APIs to provide a mechanism for reacting to state changes.

val scope = CoroutineScope(context + SupervisorJob())
val job = scope.launch {
listen {
val keys = stateStore.keys
// Do some processing of the keys
}
}

Extension functions

StateStore.syncToDataStore

suspend fun StateStore.syncToDataStore(
    dataStore: DataStore<StateStorePreferences>
): Unit

Tracks changes to StateStore and syncs keys implementing PersistToDataStore to DataStore.

Keys must implement PersistToDataStore to be saved.

import androidx.appstate.datastore.syncToDataStore
import androidx.appstate.statestore.StateStore
import androidx.datastore.core.DataStore

val stateStore = StateStore()

// Register the key by getting the state
val state = stateStore.getState(SampleKey)

stateStore.syncToDataStore(dataStore)
Parameters
dataStore: DataStore<StateStorePreferences>

the DataStore used to save and restore state

StateStore.syncToDataStore

suspend fun StateStore.syncToDataStore(path: String, coroutineScope: CoroutineScope): Unit

Tracks changes to StateStore and persists keys implementing PersistToDataStore to a DataStore backed by a file.

Keys must implement PersistToDataStore to be saved.

import androidx.appstate.datastore.syncToDataStore
import androidx.appstate.statestore.StateStore
import androidx.datastore.core.DataStore

val stateStore = StateStore()

// Register the key by getting the state
val state = stateStore.getState(SampleKey)

// The path must include the directory, for example, on Android:
// val path = context.filesDir.resolve("stateStore.preferences_pb").absolutePath
val path = "/path/to/directory/stateStore.preferences_pb"

stateStore.syncToDataStore(path, CoroutineScope(Dispatchers.IO))
Parameters
path: String

the file path to save and restore state from

coroutineScope: CoroutineScope

the CoroutineScope used for the DataStore