Skip to content

Commit f1941e8

Browse files
[Preferences DataStore] Update to alpha07
1 parent 9294a73 commit f1941e8

File tree

4 files changed

+14
-25
lines changed

4 files changed

+14
-25
lines changed

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import androidx.datastore.preferences.core.booleanPreferencesKey
2525
import androidx.datastore.preferences.core.edit
2626
import androidx.datastore.preferences.core.emptyPreferences
2727
import androidx.datastore.preferences.core.stringPreferencesKey
28-
import androidx.datastore.preferences.createDataStore
28+
import androidx.datastore.preferences.preferencesDataStore
2929
import kotlinx.coroutines.flow.Flow
3030
import kotlinx.coroutines.flow.catch
3131
import kotlinx.coroutines.flow.map
@@ -48,12 +48,11 @@ data class UserPreferences(
4848
/**
4949
* Class that handles saving and retrieving user preferences
5050
*/
51-
class UserPreferencesRepository private constructor(context: Context) {
51+
class UserPreferencesRepository (private val context: Context) {
5252

5353
private val TAG: String = "UserPreferencesRepo"
5454

55-
private val dataStore: DataStore<Preferences> =
56-
context.createDataStore(
55+
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(
5756
name = USER_PREFERENCES_NAME,
5857
// Since we're migrating from SharedPreferences, add a migration based on the
5958
// SharedPreferences name
@@ -69,7 +68,7 @@ class UserPreferencesRepository private constructor(context: Context) {
6968
/**
7069
* Get the user preferences flow.
7170
*/
72-
val userPreferencesFlow: Flow<UserPreferences> = dataStore.data
71+
val userPreferencesFlow: Flow<UserPreferences> = context.dataStore.data
7372
.catch { exception ->
7473
// dataStore.data throws an IOException when an error is encountered when reading data
7574
if (exception is IOException) {
@@ -96,7 +95,7 @@ class UserPreferencesRepository private constructor(context: Context) {
9695
suspend fun enableSortByDeadline(enable: Boolean) {
9796
// updateData handles data transactionally, ensuring that if the sort is updated at the same
9897
// time from another thread, we won't have conflicts
99-
dataStore.edit { preferences ->
98+
context.dataStore.edit { preferences ->
10099
val currentOrder = SortOrder.valueOf(
101100
preferences[PreferencesKeys.SORT_ORDER] ?: SortOrder.NONE.name
102101
)
@@ -126,7 +125,7 @@ class UserPreferencesRepository private constructor(context: Context) {
126125
suspend fun enableSortByPriority(enable: Boolean) {
127126
// updateData handles data transactionally, ensuring that if the sort is updated at the same
128127
// time from another thread, we won't have conflicts
129-
dataStore.edit { preferences ->
128+
context.dataStore.edit { preferences ->
130129
val currentOrder = SortOrder.valueOf(
131130
preferences[PreferencesKeys.SORT_ORDER] ?: SortOrder.NONE.name
132131
)
@@ -151,21 +150,8 @@ class UserPreferencesRepository private constructor(context: Context) {
151150
}
152151

153152
suspend fun updateShowCompleted(showCompleted: Boolean) {
154-
dataStore.edit { preferences ->
153+
context.dataStore.edit { preferences ->
155154
preferences[PreferencesKeys.SHOW_COMPLETED] = showCompleted
156155
}
157156
}
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-
}
171157
}

app/src/main/java/com/codelab/android/datastore/ui/TasksActivity.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ class TasksActivity : AppCompatActivity() {
4141

4242
viewModel = ViewModelProvider(
4343
this,
44-
TasksViewModelFactory(TasksRepository, UserPreferencesRepository.getInstance(this))
44+
TasksViewModelFactory(
45+
TasksRepository,
46+
UserPreferencesRepository(this.applicationContext)
47+
)
4548
).get(TasksViewModel::class.java)
4649

4750
setupRecyclerView()

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ buildscript {
2222
jcenter()
2323
}
2424
dependencies {
25-
classpath "com.android.tools.build:gradle:4.1.0"
25+
classpath 'com.android.tools.build:gradle:7.0.0-alpha08'
2626
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
2727
}
2828
}
@@ -43,7 +43,7 @@ ext {
4343
constraintLayoutVersion = '2.0.2'
4444
coreVersion = '1.3.2'
4545
coroutinesVersion = '1.3.9'
46-
dataStoreVersion = '1.0.0-alpha06'
46+
dataStoreVersion = '1.0.0-alpha07'
4747
materialVersion = '1.2.1'
4848
lifecycleVersion = '2.2.0'
4949

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
6+
distributionUrl=https://services.gradle.org/distributions/gradle-6.8.2-bin.zip

0 commit comments

Comments
 (0)