1
+ package com.vanced.store.datasource
2
+
3
+ import androidx.datastore.core.DataStore
4
+ import androidx.datastore.preferences.core.Preferences
5
+ import androidx.datastore.preferences.core.edit
6
+ import androidx.datastore.preferences.core.intPreferencesKey
7
+ import kotlinx.coroutines.flow.Flow
8
+ import kotlinx.coroutines.flow.map
9
+
10
+ interface PreferenceDatasource {
11
+
12
+ fun observeAppTheme (): Flow <AppTheme >
13
+ suspend fun saveAppTheme (theme : AppTheme )
14
+
15
+ fun observeAppAccent (): Flow <AppAccent >
16
+ suspend fun saveAppAccent (accent : AppAccent )
17
+
18
+ }
19
+
20
+ class PreferenceDatasourceImpl (
21
+ private val datastore : DataStore <Preferences >
22
+ ) : PreferenceDatasource {
23
+
24
+ override fun observeAppTheme (): Flow <AppTheme > {
25
+ return datastore.data.map {
26
+ AppTheme .values()[it[KEY_APP_THEME ] ? : 0 ]
27
+ }
28
+ }
29
+
30
+ override suspend fun saveAppTheme (theme : AppTheme ) {
31
+ datastore.edit {
32
+ it[KEY_APP_THEME ] = theme.ordinal
33
+ }
34
+ }
35
+
36
+ override fun observeAppAccent (): Flow <AppAccent > {
37
+ return datastore.data.map {
38
+ AppAccent .values()[it[KEY_APP_ACCENT ] ? : 0 ]
39
+ }
40
+ }
41
+
42
+ override suspend fun saveAppAccent (accent : AppAccent ) {
43
+ datastore.edit {
44
+ it[KEY_APP_ACCENT ] = accent.ordinal
45
+ }
46
+ }
47
+
48
+ private companion object {
49
+ val KEY_APP_THEME = intPreferencesKey(" app_theme" )
50
+ val KEY_APP_ACCENT = intPreferencesKey(" app_accent" )
51
+ }
52
+
53
+ }
54
+
55
+ enum class AppTheme {
56
+ System , Light , Dark
57
+ }
58
+
59
+ enum class AppAccent {
60
+ Blue , Orange , Pink , Purple
61
+ }
0 commit comments