Skip to content

Extension for DataStore

In order to make you use DataStore more convenient, VastTools provides extension functions.

Store key-value pairs with Preferences DataStore by extension functions

Create a Preferences DataStore

Use the property delegate created by preferencesDataStore to create an instance of Datastore<Preferences>. Call it once at the top level of your kotlin file, and access it through this property throughout the rest of your application. This makes it easier to keep your DataStore as a singleton.

val Context.dataStore: DataStore<Preferences> by 
    preferencesDataStore(name = "settings")

Implement IDataStoreOwner

Version 0.5.6

object ThemeDs : IDataStoreOwner {
    override val dataStore: DataStore<Preferences> =
        ContextHelper.getAppContext().dataStore

    val isDark by boolean(false)
}

Read from a Preferences DataStore

Version 0.5.6

The following example shows you that Boolean type data can be read from Preferences DataStore by calling asNotNullFlow in Compose .

val darkTheme by ThemeDs.isDark.asNotNullFlow().collectAsState(false)

Write to a Preferences DataStore

Version 0.5.1

The following example shows you that Boolean type data can be written to Preferences DataStore in Compose .

val coroutineScope = rememberCoroutineScope()

Switch(checked = darkTheme, onCheckedChange = { value ->
    coroutineScope.launch {
        ThemeDs.isDark.set(value)
    }
})