Created
June 18, 2021 10:48
-
-
Save UtkuGlsvn/f00d1e8ff563eb4322d6461ecb139ff1 to your computer and use it in GitHub Desktop.
DataStorePrefManager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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