File tree Expand file tree Collapse file tree 4 files changed +20
-7
lines changed
java/com/codelab/android/datastore Expand file tree Collapse file tree 4 files changed +20
-7
lines changed Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ enum class SortOrder {
34
34
/* *
35
35
* Class that handles saving and retrieving user preferences
36
36
*/
37
- class UserPreferencesRepository (context : Context ) {
37
+ class UserPreferencesRepository private constructor (context : Context ) {
38
38
39
39
private val sharedPreferences =
40
40
context.applicationContext.getSharedPreferences(USER_PREFERENCES_NAME , Context .MODE_PRIVATE )
@@ -97,4 +97,17 @@ class UserPreferencesRepository(context: Context) {
97
97
putString(SORT_ORDER_KEY , sortOrder.name)
98
98
}
99
99
}
100
+
101
+ companion object {
102
+ @Volatile
103
+ private var INSTANCE : UserPreferencesRepository ? = null
104
+
105
+ fun getInstance (context : Context ): UserPreferencesRepository {
106
+ return INSTANCE ? : synchronized(this ) {
107
+ val instance = UserPreferencesRepository (context)
108
+ INSTANCE = instance
109
+ instance
110
+ }
111
+ }
112
+ }
100
113
}
Original file line number Diff line number Diff line change @@ -41,14 +41,14 @@ class TasksActivity : AppCompatActivity() {
41
41
42
42
viewModel = ViewModelProvider (
43
43
this ,
44
- TasksViewModelFactory (TasksRepository , UserPreferencesRepository (this ))
44
+ TasksViewModelFactory (TasksRepository , UserPreferencesRepository .getInstance (this ))
45
45
).get(TasksViewModel ::class .java)
46
46
47
47
setupRecyclerView()
48
48
setupFilterListeners(viewModel)
49
49
setupSort()
50
50
51
- viewModel.tasksUiModel.observe(this ) { tasksUiModel ->
51
+ viewModel.tasksUiModel.observe(owner = this ) { tasksUiModel ->
52
52
adapter.submitList(tasksUiModel.tasks)
53
53
updateSort(tasksUiModel.sortOrder)
54
54
binding.showCompletedSwitch.isChecked = tasksUiModel.showCompleted
Original file line number Diff line number Diff line change 51
51
android : layout_height =" wrap_content"
52
52
android : layout_gravity =" center"
53
53
android : src =" @drawable/ic_baseline_filter_list_24"
54
- android : tint =" ?attr/colorOnSecondary" />
54
+ app : tint =" ?attr/colorOnSecondary" />
55
55
56
56
<TextView
57
57
android : id =" @+id/show_completed"
85
85
android : layout_height =" wrap_content"
86
86
android : layout_gravity =" center"
87
87
android : src =" @drawable/ic_baseline_reorder_24"
88
- android : tint =" ?attr/colorOnSurface" />
88
+ app : tint =" ?attr/colorOnSurface" />
89
89
90
90
<com .google.android.material.chip.ChipGroup
91
91
android : layout_width =" match_parent"
Original file line number Diff line number Diff line change @@ -40,8 +40,8 @@ task clean(type: Delete) {
40
40
41
41
ext {
42
42
supportLibVersion = ' 1.2.0'
43
- constraintLayoutVersion = ' 2.0.1 '
44
- coreVersion = ' 1.3.1 '
43
+ constraintLayoutVersion = ' 2.0.2 '
44
+ coreVersion = ' 1.3.2 '
45
45
coroutinesVersion = ' 1.3.9'
46
46
materialVersion = ' 1.2.1'
47
47
lifecycleVersion = ' 2.2.0'
You can’t perform that action at this time.
0 commit comments