@@ -25,7 +25,7 @@ import androidx.datastore.preferences.core.booleanPreferencesKey
25
25
import androidx.datastore.preferences.core.edit
26
26
import androidx.datastore.preferences.core.emptyPreferences
27
27
import androidx.datastore.preferences.core.stringPreferencesKey
28
- import androidx.datastore.preferences.createDataStore
28
+ import androidx.datastore.preferences.preferencesDataStore
29
29
import kotlinx.coroutines.flow.Flow
30
30
import kotlinx.coroutines.flow.catch
31
31
import kotlinx.coroutines.flow.map
@@ -48,12 +48,11 @@ data class UserPreferences(
48
48
/* *
49
49
* Class that handles saving and retrieving user preferences
50
50
*/
51
- class UserPreferencesRepository private constructor( context : Context ) {
51
+ class UserPreferencesRepository ( private val context : Context ) {
52
52
53
53
private val TAG : String = " UserPreferencesRepo"
54
54
55
- private val dataStore: DataStore <Preferences > =
56
- context.createDataStore(
55
+ private val Context .dataStore: DataStore <Preferences > by preferencesDataStore(
57
56
name = USER_PREFERENCES_NAME ,
58
57
// Since we're migrating from SharedPreferences, add a migration based on the
59
58
// SharedPreferences name
@@ -69,7 +68,7 @@ class UserPreferencesRepository private constructor(context: Context) {
69
68
/* *
70
69
* Get the user preferences flow.
71
70
*/
72
- val userPreferencesFlow: Flow <UserPreferences > = dataStore.data
71
+ val userPreferencesFlow: Flow <UserPreferences > = context. dataStore.data
73
72
.catch { exception ->
74
73
// dataStore.data throws an IOException when an error is encountered when reading data
75
74
if (exception is IOException ) {
@@ -96,7 +95,7 @@ class UserPreferencesRepository private constructor(context: Context) {
96
95
suspend fun enableSortByDeadline (enable : Boolean ) {
97
96
// updateData handles data transactionally, ensuring that if the sort is updated at the same
98
97
// time from another thread, we won't have conflicts
99
- dataStore.edit { preferences ->
98
+ context. dataStore.edit { preferences ->
100
99
val currentOrder = SortOrder .valueOf(
101
100
preferences[PreferencesKeys .SORT_ORDER ] ? : SortOrder .NONE .name
102
101
)
@@ -126,7 +125,7 @@ class UserPreferencesRepository private constructor(context: Context) {
126
125
suspend fun enableSortByPriority (enable : Boolean ) {
127
126
// updateData handles data transactionally, ensuring that if the sort is updated at the same
128
127
// time from another thread, we won't have conflicts
129
- dataStore.edit { preferences ->
128
+ context. dataStore.edit { preferences ->
130
129
val currentOrder = SortOrder .valueOf(
131
130
preferences[PreferencesKeys .SORT_ORDER ] ? : SortOrder .NONE .name
132
131
)
@@ -151,21 +150,8 @@ class UserPreferencesRepository private constructor(context: Context) {
151
150
}
152
151
153
152
suspend fun updateShowCompleted (showCompleted : Boolean ) {
154
- dataStore.edit { preferences ->
153
+ context. dataStore.edit { preferences ->
155
154
preferences[PreferencesKeys .SHOW_COMPLETED ] = showCompleted
156
155
}
157
156
}
158
-
159
- companion object {
160
- @Volatile
161
- private var INSTANCE : UserPreferencesRepository ? = null
162
-
163
- fun getInstance (context : Context ): UserPreferencesRepository {
164
- return INSTANCE ? : synchronized(this ) {
165
- val instance = UserPreferencesRepository (context)
166
- INSTANCE = instance
167
- instance
168
- }
169
- }
170
- }
171
157
}
0 commit comments