-
-
Save LexLeontiev/f009c060e4b82cc85186bbd623a87508 to your computer and use it in GitHub Desktop.
UserPreferencesRepo
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
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