Skip to content

Instantly share code, notes, and snippets.

@arisris
Last active August 26, 2022 23:32
Show Gist options
  • Select an option

  • Save arisris/3714ef437bf610ffc5c885f68e9ea135 to your computer and use it in GitHub Desktop.

Select an option

Save arisris/3714ef437bf610ffc5c885f68e9ea135 to your computer and use it in GitHub Desktop.
Datastore Preferences Composable
@Composable
fun TestPrefs() {
val (count, isLoading, setCount) = rememberIntPreferences("counter", 0)
Column {
Text("Current Count: ${if (isLoading) "loading" else count}")
Button(onClick = { setCount(count+1) }) {
Text("Increment by 1")
}
Button(onClick = { setCount(count-1) }) {
Text("Decrement by 1")
}
}
}
package com.kliksob.forgswitch2.utils
import android.content.Context
import androidx.compose.runtime.*
import androidx.compose.ui.platform.LocalContext
import androidx.datastore.preferences.core.*
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
@Composable
fun rememberBooleanSetting(
key: String,
value: Boolean,
coroutineScope: CoroutineScope = rememberCoroutineScope()
) = rememberPreferences(
key = booleanPreferencesKey(key),
initialValue = value,
coroutineScope = coroutineScope
)
@Composable
fun rememberStringPreference(
key: String,
value: String,
coroutineScope: CoroutineScope = rememberCoroutineScope()
) = rememberPreferences(
key = stringPreferencesKey(key),
initialValue = value,
coroutineScope = coroutineScope
)
@Composable
fun rememberIntPreference(
key: String,
value: Int,
coroutineScope: CoroutineScope = rememberCoroutineScope()
) = rememberPreferences(
key = intPreferencesKey(key),
initialValue = value,
coroutineScope = coroutineScope
)
@Composable
fun rememberDoublePreference(
key: String,
value: Double,
coroutineScope: CoroutineScope = rememberCoroutineScope()
) = rememberPreferences(
key = doublePreferencesKey(key),
initialValue = value,
coroutineScope = coroutineScope
)
@Composable
fun rememberFloatPreference(
key: String,
value: Float,
coroutineScope: CoroutineScope = rememberCoroutineScope()
) = rememberPreferences(
key = floatPreferencesKey(key),
initialValue = value,
coroutineScope = coroutineScope
)
@Composable
fun rememberStringSetPreference(
key: String,
value: Set<String>,
coroutineScope: CoroutineScope = rememberCoroutineScope()
) = rememberPreferences(
key = stringSetPreferencesKey(key),
initialValue = value,
coroutineScope = coroutineScope
)
@Composable
fun rememberLongPreference(
key: String,
value: Long,
coroutineScope: CoroutineScope = rememberCoroutineScope()
) = rememberPreferences(
key = longPreferencesKey(key),
initialValue = value,
coroutineScope = coroutineScope
)
private val Context.dataStore by preferencesDataStore("app_preferences_data")
@Composable
private inline fun <reified T> rememberPreferences(
key: Preferences.Key<T>,
initialValue: T,
coroutineScope: CoroutineScope
): Triple<T, Boolean, (value: T) -> Unit> {
val context = LocalContext.current
var isLoading by remember { mutableStateOf(true) }
val current by context.dataStore.data.map {
isLoading = false
it[key] ?: initialValue
}.collectAsState(initial = initialValue)
return Triple(
current,
isLoading
) {
coroutineScope.launch {
context.dataStore.edit { pref ->
pref[key] = it
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment