From dc8b91cb49ab8ca80f63b11e4ca9789a81f03863 Mon Sep 17 00:00:00 2001 From: Lasta apps Date: Sun, 14 Jul 2024 13:39:58 +0300 Subject: [PATCH] refactor: Created combined app settings object with all the settings --- .../settings/data/MainSettingsRepoImpl.kt | 60 ++++++++++++------- .../settings/domain/MainSettingsRepo.kt | 7 ++- .../settings/domain/model/AppSettings.kt} | 22 +++++-- 3 files changed, 59 insertions(+), 30 deletions(-) rename app/src/main/kotlin/cz/lastaapps/menza/{domain/model/UserSettings.kt => features/settings/domain/model/AppSettings.kt} (56%) diff --git a/app/src/main/kotlin/cz/lastaapps/menza/features/settings/data/MainSettingsRepoImpl.kt b/app/src/main/kotlin/cz/lastaapps/menza/features/settings/data/MainSettingsRepoImpl.kt index 89c1e789..f161b255 100644 --- a/app/src/main/kotlin/cz/lastaapps/menza/features/settings/data/MainSettingsRepoImpl.kt +++ b/app/src/main/kotlin/cz/lastaapps/menza/features/settings/data/MainSettingsRepoImpl.kt @@ -20,10 +20,10 @@ package cz.lastaapps.menza.features.settings.data import cz.lastaapps.api.core.domain.model.MenzaType -import cz.lastaapps.menza.domain.model.UserSettings import cz.lastaapps.menza.features.settings.data.datasource.GeneralDataSource import cz.lastaapps.menza.features.settings.data.datasource.InitMenzaDataSource import cz.lastaapps.menza.features.settings.domain.MainSettingsRepo +import cz.lastaapps.menza.features.settings.domain.model.AppSettings import cz.lastaapps.menza.features.settings.domain.model.AppThemeType import cz.lastaapps.menza.features.settings.domain.model.DarkMode import cz.lastaapps.menza.features.settings.domain.model.DishLanguage @@ -31,18 +31,49 @@ import cz.lastaapps.menza.features.settings.domain.model.DishListMode import cz.lastaapps.menza.features.settings.domain.model.InitialSelectionBehaviour import cz.lastaapps.menza.features.settings.domain.model.PriceType import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.channelFlow -import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.update -import kotlinx.coroutines.launch internal class MainSettingsRepoImpl( private val initial: InitMenzaDataSource, private val general: GeneralDataSource, private val defaults: DefaultsProvider, ) : MainSettingsRepo { + + override fun getAllSettings(): Flow = combine( + getInitialMenzaMode(), + getLatestMenza(), + getPreferredMenza(), + isAppSetupFinished(), + isSettingsEverOpened(), + getPriceType(), + getDarkMode(), + getAppTheme(), + getImageScale(), + getImagesOnMetered(), + getDishLanguage(), + isCompactTodayView(), + isOliverRow(), + getBalanceWarningThreshold(), + ) { arr -> + AppSettings( + arr[0] as InitialSelectionBehaviour, + arr[1] as MenzaType?, + arr[2] as MenzaType?, + arr[3] as Boolean, + arr[4] as Boolean, + arr[5] as PriceType, + arr[6] as DarkMode, + arr[7] as AppThemeType?, + arr[8] as Float, + arr[9] as Boolean, + arr[10] as DishLanguage, + arr[11] as DishListMode, + arr[12] as Boolean, + arr[13] as Int, + ) + } + override suspend fun storeInitialMenzaMode(mode: InitialSelectionBehaviour) = initial.storeInitialMenzaMode(mode) @@ -74,23 +105,6 @@ internal class MainSettingsRepoImpl( override fun isSettingsEverOpened(): Flow = general.isSettingsEverOpened() - override fun getUserSettings(): Flow = channelFlow { - val userSettings = MutableStateFlow(UserSettings()) - - launch { - isOliverRow().collectLatest { - userSettings.update { state -> - state.copy(useOliverRows = it) - } - } - } - - - userSettings.collectLatest { - send(it) - } - } - override suspend fun setPriceType(type: PriceType) = general.setPriceType(type) diff --git a/app/src/main/kotlin/cz/lastaapps/menza/features/settings/domain/MainSettingsRepo.kt b/app/src/main/kotlin/cz/lastaapps/menza/features/settings/domain/MainSettingsRepo.kt index c7fe5074..21fe7ff1 100644 --- a/app/src/main/kotlin/cz/lastaapps/menza/features/settings/domain/MainSettingsRepo.kt +++ b/app/src/main/kotlin/cz/lastaapps/menza/features/settings/domain/MainSettingsRepo.kt @@ -20,7 +20,7 @@ package cz.lastaapps.menza.features.settings.domain import cz.lastaapps.api.core.domain.model.MenzaType -import cz.lastaapps.menza.domain.model.UserSettings +import cz.lastaapps.menza.features.settings.domain.model.AppSettings import cz.lastaapps.menza.features.settings.domain.model.AppThemeType import cz.lastaapps.menza.features.settings.domain.model.DarkMode import cz.lastaapps.menza.features.settings.domain.model.DishLanguage @@ -30,6 +30,9 @@ import cz.lastaapps.menza.features.settings.domain.model.PriceType import kotlinx.coroutines.flow.Flow internal interface MainSettingsRepo { + + fun getAllSettings(): Flow + suspend fun storeInitialMenzaMode(mode: InitialSelectionBehaviour) fun getInitialMenzaMode(): Flow @@ -45,8 +48,6 @@ internal interface MainSettingsRepo { suspend fun storeSettingsEverOpened() fun isSettingsEverOpened(): Flow - fun getUserSettings(): Flow - suspend fun setPriceType(type: PriceType) fun getPriceType(): Flow diff --git a/app/src/main/kotlin/cz/lastaapps/menza/domain/model/UserSettings.kt b/app/src/main/kotlin/cz/lastaapps/menza/features/settings/domain/model/AppSettings.kt similarity index 56% rename from app/src/main/kotlin/cz/lastaapps/menza/domain/model/UserSettings.kt rename to app/src/main/kotlin/cz/lastaapps/menza/features/settings/domain/model/AppSettings.kt index 44df6a82..91776dbf 100644 --- a/app/src/main/kotlin/cz/lastaapps/menza/domain/model/UserSettings.kt +++ b/app/src/main/kotlin/cz/lastaapps/menza/features/settings/domain/model/AppSettings.kt @@ -17,11 +17,25 @@ * along with Menza. If not, see . */ -package cz.lastaapps.menza.domain.model +package cz.lastaapps.menza.features.settings.domain.model import androidx.compose.runtime.Immutable +import cz.lastaapps.api.core.domain.model.MenzaType @Immutable -data class UserSettings( - val useOliverRows: Boolean = false, -) +internal data class AppSettings( + val initialMenzaMode: InitialSelectionBehaviour, + val latestMenza: MenzaType?, + val preferredMenza: MenzaType?, + val isAppSetupFinished: Boolean, + val isSettingsEverOpened: Boolean, + val priceType: PriceType, + val darkMode: DarkMode, + val appTheme: AppThemeType?, + val imageScale: Float, + val imagesOnMetered: Boolean, + val dishLanguage: DishLanguage, + val compactTodayView: DishListMode, + val useOliverRows: Boolean, + val balanceWarningThreshold: Int, +) \ No newline at end of file