Skip to content

Instantly share code, notes, and snippets.

@UtkuGlsvn
Created June 18, 2021 10:48
Show Gist options
  • Save UtkuGlsvn/f00d1e8ff563eb4322d6461ecb139ff1 to your computer and use it in GitHub Desktop.
Save UtkuGlsvn/f00d1e8ff563eb4322d6461ecb139ff1 to your computer and use it in GitHub Desktop.
DataStorePrefManager
private const val STORE_NAME = "data_store_pref"
private const val DARK_THEME = "is_dark_theme"
class DataStorePrefManager constructor(private val context: Context) {
companion object {
private val Context.dataStore by preferencesDataStore(STORE_NAME)
private val NIGHT_MODE_KEY = booleanPreferencesKey(DARK_THEME)
}
suspend fun setNightMode(isNightMode: Boolean) {
context.dataStore.edit {
it[NIGHT_MODE_KEY] = isNightMode
}
}
fun readNightMode(): Flow<Boolean> = context.dataStore.data.map {
it[NIGHT_MODE_KEY] ?: false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment