Skip to content

Commit

Permalink
Outsource keys for preferences into dedicated resource file
Browse files Browse the repository at this point in the history
  • Loading branch information
Faltenreich committed Mar 30, 2024
1 parent 48340ec commit 1425f03
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
package com.faltenreich.diaguard.preference.store

import com.faltenreich.diaguard.MR
import com.faltenreich.diaguard.preference.store.color.ColorScheme
import com.faltenreich.diaguard.preference.store.screen.StartScreen
import com.faltenreich.diaguard.shared.di.inject
import com.faltenreich.diaguard.shared.keyvalue.KeyValueStore
import com.faltenreich.diaguard.shared.localization.Localization
import dev.icerock.moko.resources.StringResource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

// TODO: Migrate preferences
class PreferenceStore(
private val keyValueStore: KeyValueStore = inject(),
private val keyValueStore: KeyValueStore,
private val localization: Localization,
) {

val startScreen: Flow<StartScreen>
get() = keyValueStore.read<Int>(KEY_START_SCREEN).map { stableId ->
stableId?.let(StartScreen::valueOf) ?: StartScreen.DASHBOARD
}
private fun getKey(resource: StringResource, vararg arguments: Any): String {
val key = localization.getString(resource, arguments).takeIf(String::isNotBlank)
requireNotNull(key)
return key
}

val colorScheme: Flow<ColorScheme>
get() = keyValueStore.read<Int>(KEY_COLOR_SCHEME).map { stableId ->
stableId?.let(ColorScheme::valueOf) ?: ColorScheme.SYSTEM
fun getStartScreen(): Flow<StartScreen> {
return keyValueStore.read<Int>(getKey(MR.strings.preference_start_screen)).map { stableId ->
stableId?.let(StartScreen::valueOf) ?: StartScreen.DASHBOARD
}
}

suspend fun setStartScreen(startScreen: StartScreen) {
keyValueStore.write(KEY_START_SCREEN, startScreen.stableId)
keyValueStore.write(getKey(MR.strings.preference_start_screen), startScreen.stableId)
}

suspend fun setColorScheme(colorScheme: ColorScheme) {
keyValueStore.write(KEY_COLOR_SCHEME, colorScheme.stableId)
fun getColorScheme(): Flow<ColorScheme> {
return keyValueStore.read<Int>(getKey(MR.strings.preference_theme)).map { stableId ->
stableId?.let(ColorScheme::valueOf) ?: ColorScheme.SYSTEM
}
}

companion object {

private const val KEY_START_SCREEN = "startScreen"
private const val KEY_COLOR_SCHEME = "colorScheme"
suspend fun setColorScheme(colorScheme: ColorScheme) {
keyValueStore.write(getKey(MR.strings.preference_theme), colorScheme.stableId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class GetColorSchemeUseCase(
) {

operator fun invoke(): Flow<ColorScheme> {
return preferenceStore.colorScheme
return preferenceStore.getColorScheme()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class GetStartScreenUseCase(
) {

operator fun invoke(): Flow<StartScreen> {
return preferenceStore.startScreen
return preferenceStore.getStartScreen()
}
}
85 changes: 85 additions & 0 deletions shared/src/commonMain/moko-resources/base/strings-preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="utf-8"?>

<resources>

<!-- General -->
<string name="preference_theme">theme</string>
<string name="preference_start_screen">"startscreen"</string>

<!-- Timeline -->
<string name="preference_timeline">timeline</string>
<string name="preference_timeline_show_dots">timelineShowDots</string>
<string name="preference_timeline_show_lines">timelineShowLines</string>

<!-- Reminder -->
<string name="preference_alarm_start">alarmStartInMillis</string>
<string name="preference_alarm_sound">sound</string>
<string name="preference_alarm_vibration">vibration</string>

<!-- Data -->
<string name="preference_backup">backup</string>
<string name="preference_import">import</string>

<string name="preference_categories">categories</string>
<string name="preference_categories_active">%s_active</string>
<string name="preference_categories_sort_index">%s_sortIndex</string>

<string name="preference_tags">tags</string>
<string name="preference_tags_imported">didImportTagsForLanguage</string>

<string name="preference_food">food</string>
<string name="preference_food_imported">didImportCommonFoodForLanguage</string>
<string name="preference_food_show_custom">showCustomFood</string>
<string name="preference_food_show_common">showCommonFood</string>
<string name="preference_food_show_branded">showBrandedFood</string>

<string name="preference_input_queries">inputQueries</string>

<string name="preference_cgm">cgm</string>
<string name="preference_cgm_xdrip">cgmXDrip</string>
<string name="preference_cgm_notification">cgmNotification</string>

<!-- Export -->
<string name="preference_export_pdf_style">exportPdfStyle</string>
<string name="preference_include_calendar_week">exportHeader</string>
<string name="preference_include_generated_date">exportFooter</string>
<string name="preference_include_page_number">includePageNumber</string>
<string name="preference_export_notes">export_notes</string>
<string name="preference_export_tags">export_tags</string>
<string name="preference_export_food">export_food</string>
<string name="preference_export_skip_empty_days">skipEmptyDays</string>
<string name="preference_export_categories">exportCategories</string>
<string name="preference_export_insulin_split">exportInsulinSplit</string>

<!-- Therapy -->
<string name="preference_extrema">extrema</string>
<string name="preference_extrema_highlight">targets_highlight</string>
<string name="preference_extrema_target">target</string>
<string name="preference_extrema_hyperclycemia">hyperclycemia</string>
<string name="preference_extrema_hypoclycemia">hypoclycemia</string>

<string name="preference_uni">unit</string>
<string name="preference_unit_bloodsugar">unit_bloodsugar</string>
<string name="preference_unit_meal">unit_meal</string>
<string name="preference_unit_meal_factor">unit_meal_factor</string>
<string name="preference_unit_hba1c">unit_hba1c</string>
<string name="preference_unit_weight">unit_weight</string>


<string name="preference_decimal_places">Decimal places</string>

<string name="preference_factor">factor</string>

<string name="preference_factor_meal">unit_meal_factor</string>
<string name="preference_factor_meal_interval">intervalFactor</string>
<string name="preference_factor_meal_interval_for_hour">intervalFactor%d</string>

<string name="preference_factor_correction">correction</string>
<string name="preference_factor_correction_interval">intervalCorrection</string>
<string name="preference_factor_correction_interval_for_hour">intervalCorrection%d</string>

<string name="preference_factor_basal_rate">basalRate</string>
<string name="preference_factor_basal_rate_interval">intervalBasalRate</string>
<string name="preference_factor_basal_rate_interval_for_hour">intervalBasalRate%d</string>

</resources>

0 comments on commit 1425f03

Please sign in to comment.