|
9 | 9 | */
|
10 | 10 | package org.mifos.core.datastore
|
11 | 11 |
|
12 |
| -import org.mifos.core.datastore.model.AppSettings |
| 12 | +import kotlinx.coroutines.flow.Flow |
| 13 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 14 | +import kotlinx.coroutines.flow.asStateFlow |
| 15 | +import org.mifos.core.model.DarkThemeConfig |
| 16 | +import org.mifos.core.model.ThemeBrand |
| 17 | +import org.mifos.core.model.UserData |
13 | 18 | import org.mifos.corebase.datastore.UserPreferencesDataStore
|
14 | 19 |
|
15 |
| -private const val APP_SETTINGS_KEY = "app_settings" |
| 20 | +private const val THEME_BRAND_KEY = "theme_brand" |
| 21 | +private const val DARK_THEME_CONFIG_KEY = "dark_theme_config" |
| 22 | +private const val DYNAMIC_COLOR_KEY = "use_dynamic_color" |
16 | 23 |
|
17 | 24 | class UserPreferencesRepositoryImpl(
|
18 | 25 | private val dataStore: UserPreferencesDataStore,
|
19 | 26 | ) : UserPreferencesRepository {
|
20 | 27 |
|
21 |
| - override suspend fun updateSettings(settings: AppSettings) { |
22 |
| - dataStore.putValue( |
23 |
| - key = APP_SETTINGS_KEY, |
24 |
| - value = settings, |
25 |
| - serializer = AppSettings.serializer(), |
26 |
| - ) |
| 28 | + private var _userData: MutableStateFlow<UserData> = MutableStateFlow(UserData()) |
| 29 | + override val userData: Flow<UserData> = _userData.asStateFlow() |
| 30 | + |
| 31 | + override suspend fun setThemeBrand(themeBrand: ThemeBrand) { |
| 32 | + dataStore.putValue(key = THEME_BRAND_KEY, value = themeBrand.brandName) |
| 33 | + getThemeBrand(themeBrand) |
| 34 | + } |
| 35 | + |
| 36 | + override suspend fun setDarkThemeConfig(darkThemeConfig: DarkThemeConfig) { |
| 37 | + dataStore.putValue(key = DARK_THEME_CONFIG_KEY, value = darkThemeConfig.name) |
| 38 | + getDarkThemeConfig(darkThemeConfig) |
| 39 | + } |
| 40 | + |
| 41 | + override suspend fun setDynamicColorPreference(useDynamicColor: Boolean) { |
| 42 | + dataStore.putValue(key = DYNAMIC_COLOR_KEY, value = useDynamicColor) |
| 43 | + getDynamicColorPreference(useDynamicColor) |
| 44 | + } |
| 45 | + |
| 46 | + override suspend fun getThemeBrand(themeBrand: ThemeBrand) { |
| 47 | + val themeBrandString = |
| 48 | + dataStore.getValue(key = THEME_BRAND_KEY, default = ThemeBrand.DEFAULT.brandName) |
| 49 | + _userData.value = _userData.value.copy(themeBrand = ThemeBrand.fromString(themeBrandString)) |
| 50 | + } |
| 51 | + |
| 52 | + override suspend fun getDarkThemeConfig(darkThemeConfig: DarkThemeConfig) { |
| 53 | + val darkThemeConfigString = |
| 54 | + dataStore.getValue( |
| 55 | + key = DARK_THEME_CONFIG_KEY, |
| 56 | + default = DarkThemeConfig.FOLLOW_SYSTEM.name, |
| 57 | + ) |
| 58 | + _userData.value = |
| 59 | + _userData.value.copy(darkThemeConfig = DarkThemeConfig.fromString(darkThemeConfigString)) |
27 | 60 | }
|
28 | 61 |
|
29 |
| - override suspend fun getSettings(defaultValue: AppSettings): AppSettings { |
30 |
| - return dataStore.getValue( |
31 |
| - key = APP_SETTINGS_KEY, |
32 |
| - default = defaultValue, |
33 |
| - serializer = AppSettings.serializer(), |
34 |
| - ) |
| 62 | + override suspend fun getDynamicColorPreference(useDynamicColor: Boolean) { |
| 63 | + val useDynamicColorBoolean = |
| 64 | + dataStore.getValue(key = DYNAMIC_COLOR_KEY, default = false) |
| 65 | + _userData.value = _userData.value.copy(useDynamicColor = useDynamicColorBoolean) |
35 | 66 | }
|
36 | 67 | }
|
0 commit comments