Skip to content

Instantly share code, notes, and snippets.

@egorikftp
Last active Aug 22, 2021
Embed
What would you like to do?
fun <T> DataStore<Preferences>.get(
key: Preferences.Key<T>,
defaultValue: T
): T = runBlocking {
data.first()[key] ?: defaultValue
}
fun <T> DataStore<Preferences>.set(
key: Preferences.Key<T>,
value: T?
) = runBlocking<Unit> {
edit {
if (value == null) {
it.remove(key)
} else {
it[key] = value
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment