Skip to content

Instantly share code, notes, and snippets.

@egorikftp
Last active August 22, 2021 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egorikftp/2d9c149cad78cf9c5cfb0d825131042d to your computer and use it in GitHub Desktop.
Save egorikftp/2d9c149cad78cf9c5cfb0d825131042d to your computer and use it in GitHub Desktop.
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