Skip to content

Commit d9f80f4

Browse files
rohitsat13florina-muntenescu
authored andcommitted
Modify the DataStore sample app to work with the new Preferences API.
1 parent 02a54eb commit d9f80f4

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

app/src/main/java/com/codelab/android/datastore/data/UserPreferencesRepository.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class UserPreferencesRepository private constructor(context: Context) {
7676
SortOrder.valueOf(
7777
preferences[Keys.SORT_ORDER_KEY] ?: SortOrder.NONE.name
7878
)
79+
80+
// Get our show completed value, defaulting to false if not set:
7981
val showCompleted = preferences[Keys.SHOW_COMPLETED_KEY] ?: false
8082
UserPreferences(showCompleted, sortOrder)
8183
}
@@ -86,10 +88,11 @@ class UserPreferencesRepository private constructor(context: Context) {
8688
suspend fun enableSortByDeadline(enable: Boolean) {
8789
// updateData handles data transactionally, ensuring that if the sort is updated at the same
8890
// time from another thread, we won't have conflicts
89-
dataStore.edit { prefs ->
90-
val currentOrder = prefs[Keys.SORT_ORDER_KEY]?.let { SortOrder.valueOf(it) }
91+
dataStore.edit { preferences ->
92+
val currentOrder =
93+
preferences[Keys.SORT_ORDER_KEY]?.let { SortOrder.valueOf(it) }
9194

92-
val newSortOrder =
95+
val newSortOrder =
9396
if (enable) {
9497
if (currentOrder == SortOrder.BY_PRIORITY) {
9598
SortOrder.BY_DEADLINE_AND_PRIORITY
@@ -104,7 +107,7 @@ class UserPreferencesRepository private constructor(context: Context) {
104107
}
105108
}
106109

107-
prefs[Keys.SORT_ORDER_KEY] = newSortOrder.name
110+
preferences[Keys.SORT_ORDER_KEY] = newSortOrder.name
108111
}
109112
}
110113

@@ -137,8 +140,8 @@ class UserPreferencesRepository private constructor(context: Context) {
137140
}
138141

139142
suspend fun updateShowCompleted(showCompleted: Boolean) {
140-
dataStore.edit { prefs ->
141-
prefs[Keys.SHOW_COMPLETED_KEY] = showCompleted
143+
dataStore.edit { preferences ->
144+
preferences[Keys.SHOW_COMPLETED_KEY] = showCompleted
142145
}
143146
}
144147

0 commit comments

Comments
 (0)