Skip to content

Commit

Permalink
refactor: Created combined app settings object with all the settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Lastaapps committed Jul 14, 2024
1 parent 70f369d commit dc8b91c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,60 @@
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
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<AppSettings> = 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)

Expand Down Expand Up @@ -74,23 +105,6 @@ internal class MainSettingsRepoImpl(
override fun isSettingsEverOpened(): Flow<Boolean> =
general.isSettingsEverOpened()

override fun getUserSettings(): Flow<UserSettings> = 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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +30,9 @@ import cz.lastaapps.menza.features.settings.domain.model.PriceType
import kotlinx.coroutines.flow.Flow

internal interface MainSettingsRepo {

fun getAllSettings(): Flow<AppSettings>

suspend fun storeInitialMenzaMode(mode: InitialSelectionBehaviour)
fun getInitialMenzaMode(): Flow<InitialSelectionBehaviour>

Expand All @@ -45,8 +48,6 @@ internal interface MainSettingsRepo {
suspend fun storeSettingsEverOpened()
fun isSettingsEverOpened(): Flow<Boolean>

fun getUserSettings(): Flow<UserSettings>

suspend fun setPriceType(type: PriceType)
fun getPriceType(): Flow<PriceType>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,25 @@
* along with Menza. If not, see <https://www.gnu.org/licenses/>.
*/

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,
)

0 comments on commit dc8b91c

Please sign in to comment.