Skip to content

Instantly share code, notes, and snippets.

@LexLeontiev
Created August 10, 2024 16:06
Show Gist options
  • Save LexLeontiev/f009c060e4b82cc85186bbd623a87508 to your computer and use it in GitHub Desktop.
Save LexLeontiev/f009c060e4b82cc85186bbd623a87508 to your computer and use it in GitHub Desktop.
UserPreferencesRepo
class UserPreferencesRepo(
dataStore: DataStore<Preferences>,
) {
val userPreferencesFlow: Flow<UserPreferences> = dataStore.data
.catch { exception ->
if (exception is IOException) {
emit(emptyPreferences())
} else {
throw exception
}
}.map { preferences ->
val booleanPref = preferences[booleanPreferencesKey(PreferenceKey.KEY_BOOLEAN_PREFERENCE)] ?: false
val intPref = preferences[intPreferencesKey(PreferenceKey.KEY_INT_PREFERENCE)] ?: 0
// get other prefs here
UserPreferences(booleanPref, intPref, ...)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment