Skip to content

Commit

Permalink
feat: Balance warning limit slider added
Browse files Browse the repository at this point in the history
  • Loading branch information
Lastaapps committed Jul 12, 2024
1 parent 9df1c1b commit f84f5e4
Show file tree
Hide file tree
Showing 16 changed files with 261 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023, Petr Laštovička as Lasta apps, All rights reserved
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
*
* This file is part of Menza.
*
Expand All @@ -22,16 +22,20 @@ package cz.lastaapps.menza.features.main.domain.usecase
import cz.lastaapps.api.main.domain.usecase.wallet.WalletGetBalanceUC
import cz.lastaapps.core.domain.UCContext
import cz.lastaapps.core.domain.UseCase
import cz.lastaapps.menza.features.settings.domain.usecase.GetBalanceWarningThresholdUC
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.map

internal class CheckLowBalanceUC(
context: UCContext,
private val walletGetBalanceUC: WalletGetBalanceUC,
private val getBalanceWarningThreshold: GetBalanceWarningThresholdUC,
) : UseCase(context) {
operator fun invoke() = walletGetBalanceUC()
.filterNotNull()
.map { it.balance <= 256 }
.combine(getBalanceWarningThreshold()) { balance, threshold ->
balance.balance < threshold
}
.distinctUntilChanged()
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,12 @@ internal class MainSettingsRepoImpl(
override fun isOliverRow(): Flow<Boolean> =
general.isOliverRow()
.map { it ?: true }

override suspend fun setBalanceWarningThreshold(threshold: Int) =
general.setBalanceWarningThreshold(threshold)

override fun getBalanceWarningThreshold(): Flow<Int> =
general.getBalanceWarningThreshold()
.map { it ?: 256 }

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import cz.lastaapps.menza.features.settings.domain.model.DishListMode
import cz.lastaapps.menza.features.settings.domain.model.PriceType
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onStart

@OptIn(ExperimentalSettingsApi::class)
@JvmInline
Expand Down Expand Up @@ -76,6 +74,9 @@ internal interface GeneralDataSource {

suspend fun setOliverRow(isUsed: Boolean)
fun isOliverRow(): Flow<Boolean?>

suspend fun setBalanceWarningThreshold(threshold: Int)
fun getBalanceWarningThreshold(): Flow<Int?>
}

@OptIn(ExperimentalSettingsApi::class)
Expand All @@ -95,6 +96,7 @@ internal class GeneralDataSourceImpl(
private const val dishLanguageKey = "dish_language"
private const val compactTodayViewKey = "compact_today_view"
private const val oliverRowsKey = "oliver_row"
private const val balanceWarningThresholdKey = "balance_warning_threshold"
}

override suspend fun storeAppSetupFinished() =
Expand Down Expand Up @@ -171,4 +173,10 @@ internal class GeneralDataSourceImpl(

override fun isOliverRow(): Flow<Boolean?> =
settings.getBooleanOrNullFlow(oliverRowsKey)

override suspend fun setBalanceWarningThreshold(threshold: Int) =
settings.putInt(balanceWarningThresholdKey, threshold)

override fun getBalanceWarningThreshold(): Flow<Int?> =
settings.getIntOrNullFlow(balanceWarningThresholdKey)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ import cz.lastaapps.menza.features.settings.data.datasource.OrderSettings
import cz.lastaapps.menza.features.settings.domain.MainSettingsRepo
import cz.lastaapps.menza.features.settings.domain.OrderRepo
import cz.lastaapps.menza.features.settings.domain.usecase.FullAppReloadUC
import cz.lastaapps.menza.features.settings.domain.usecase.GetBalanceWarningThresholdUC
import cz.lastaapps.menza.features.settings.domain.usecase.GetDishLanguageUC
import cz.lastaapps.menza.features.settings.domain.usecase.GetDishListModeUC
import cz.lastaapps.menza.features.settings.domain.usecase.GetImageScaleRangeUC
import cz.lastaapps.menza.features.settings.domain.usecase.GetImageScaleUC
import cz.lastaapps.menza.features.settings.domain.usecase.GetImagesOnMeteredUC
import cz.lastaapps.menza.features.settings.domain.usecase.GetOliverRow
import cz.lastaapps.menza.features.settings.domain.usecase.GetOliverRowUC
import cz.lastaapps.menza.features.settings.domain.usecase.GetPriceTypeUC
import cz.lastaapps.menza.features.settings.domain.usecase.GetSettingsEverOpenedUC
import cz.lastaapps.menza.features.settings.domain.usecase.OnSettingsOpenedUC
import cz.lastaapps.menza.features.settings.domain.usecase.SetBalanceWarningThresholdUC
import cz.lastaapps.menza.features.settings.domain.usecase.SetDishLanguageUC
import cz.lastaapps.menza.features.settings.domain.usecase.SetDishListModeUC
import cz.lastaapps.menza.features.settings.domain.usecase.SetImageScaleUC
Expand Down Expand Up @@ -89,9 +91,9 @@ val settingsModule = module {
factory {
// @formatter:off
SettingsViewModel(
get(), get(), get(), get(), get(), get(),
get(), get(), get(), get(), get(),
get(), get(), get(), get(), get(),
get(), get(), get(), get(),
)
// @formatter:on
}
Expand All @@ -116,7 +118,9 @@ val settingsModule = module {
// Others
factoryOf(::GetImageScaleUC)
factoryOf(::GetImagesOnMeteredUC)
factoryOf(::GetOliverRow)
factoryOf(::GetBalanceWarningThresholdUC)
factoryOf(::SetBalanceWarningThresholdUC)
factoryOf(::GetOliverRowUC)
factoryOf(::GetPriceTypeUC)
factoryOf(::GetSettingsEverOpenedUC)
factoryOf(::GetDishLanguageUC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,7 @@ internal interface MainSettingsRepo {

suspend fun setOliverRows(useOliverRows: Boolean)
fun isOliverRow(): Flow<Boolean>

suspend fun setBalanceWarningThreshold(threshold: Int)
fun getBalanceWarningThreshold(): Flow<Int>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
*
* This file is part of Menza.
*
* Menza is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Menza is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Menza. If not, see <https://www.gnu.org/licenses/>.
*/

package cz.lastaapps.menza.features.settings.domain.usecase

import cz.lastaapps.core.domain.UCContext
import cz.lastaapps.core.domain.UseCase
import cz.lastaapps.menza.features.settings.domain.MainSettingsRepo

class GetBalanceWarningThresholdUC internal constructor(
context: UCContext,
private val repo: MainSettingsRepo,
) : UseCase(context) {
operator fun invoke() = repo.getBalanceWarningThreshold()
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import cz.lastaapps.core.domain.UCContext
import cz.lastaapps.core.domain.UseCase
import cz.lastaapps.menza.features.settings.domain.MainSettingsRepo

class GetOliverRow internal constructor(
class GetOliverRowUC internal constructor(
context: UCContext,
private val repo: MainSettingsRepo,
) : UseCase(context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
*
* This file is part of Menza.
*
* Menza is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Menza is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Menza. If not, see <https://www.gnu.org/licenses/>.
*/

package cz.lastaapps.menza.features.settings.domain.usecase

import cz.lastaapps.core.domain.UCContext
import cz.lastaapps.core.domain.UseCase
import cz.lastaapps.menza.features.settings.domain.MainSettingsRepo

class SetBalanceWarningThresholdUC internal constructor(
context: UCContext,
private val repo: MainSettingsRepo,
) : UseCase(context) {
suspend operator fun invoke(threshold: Int) = launch {
repo.setBalanceWarningThreshold(threshold)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ internal fun SettingsContent(
onDiscounterPrices = viewModel::setPriceType,
downloadOnMetered = state.downloadOnMetered,
onDownloadOnMetered = viewModel::setDownloadOnMetered,
balanceThreshold = state.balanceWarningThreshold,
onBalanceThreshold = viewModel::setBalanceWarningThreshold,
initialMenzaBehaviour = state.initialMenzaBehaviour,
onInitialMenzaBehaviour = viewModel::setInitMenzaBehaviour,
menzaList = state.menzaList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import cz.lastaapps.menza.features.settings.domain.model.DarkMode
import cz.lastaapps.menza.features.settings.domain.model.InitialSelectionBehaviour
import cz.lastaapps.menza.features.settings.domain.model.PriceType
import cz.lastaapps.menza.features.settings.ui.util.name
import cz.lastaapps.menza.features.settings.ui.widget.BalanceThresholdSlider
import cz.lastaapps.menza.features.settings.ui.widget.FullReloadDialog
import cz.lastaapps.menza.features.settings.ui.widget.SettingsItem
import cz.lastaapps.menza.features.settings.ui.widget.SettingsSwitch
Expand All @@ -89,6 +90,8 @@ internal fun SettingsScreen(
onDiscounterPrices: (PriceType) -> Unit,
downloadOnMetered: Boolean,
onDownloadOnMetered: (Boolean) -> Unit,
balanceThreshold: Int,
onBalanceThreshold: (Int) -> Unit,
initialMenzaBehaviour: InitialSelectionBehaviour,
onInitialMenzaBehaviour: (InitialSelectionBehaviour) -> Unit,
menzaList: ImmutableList<Menza>,
Expand Down Expand Up @@ -146,6 +149,12 @@ internal fun SettingsScreen(
onChecked = onDownloadOnMetered,
)

BalanceThresholdSlider(
title = stringResource(id = R.string.settings_balance_threshold_title),
threshold = balanceThreshold,
onThreshold = onBalanceThreshold,
)

// Behaviour at startup
InitialBehaviourSelector(
initialMenzaBehaviour = initialMenzaBehaviour,
Expand Down Expand Up @@ -294,7 +303,7 @@ private fun FullDataReload(
if (showFullReload) {
FullReloadDialog(
onDismissRequest = { showFullReload = false },
onConfirm = onFullRefresh
onConfirm = onFullRefresh,
)
}

Expand Down Expand Up @@ -358,6 +367,8 @@ private fun SettingsScreenPreview() = PreviewWrapper {
onDiscounterPrices = {},
downloadOnMetered = false,
onDownloadOnMetered = {},
balanceThreshold = 256,
onBalanceThreshold = {},
initialMenzaBehaviour = InitialSelectionBehaviour.Specific,
onInitialMenzaBehaviour = {},
menzaList = persistentListOf(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ import cz.lastaapps.menza.features.settings.domain.model.DarkMode.Dark
import cz.lastaapps.menza.features.settings.domain.model.InitialSelectionBehaviour
import cz.lastaapps.menza.features.settings.domain.model.PriceType
import cz.lastaapps.menza.features.settings.domain.usecase.FullAppReloadUC
import cz.lastaapps.menza.features.settings.domain.usecase.GetBalanceWarningThresholdUC
import cz.lastaapps.menza.features.settings.domain.usecase.GetImagesOnMeteredUC
import cz.lastaapps.menza.features.settings.domain.usecase.GetPriceTypeUC
import cz.lastaapps.menza.features.settings.domain.usecase.OnSettingsOpenedUC
import cz.lastaapps.menza.features.settings.domain.usecase.SetBalanceWarningThresholdUC
import cz.lastaapps.menza.features.settings.domain.usecase.SetImagesOnMeteredUC
import cz.lastaapps.menza.features.settings.domain.usecase.SetPriceTypeUC
import cz.lastaapps.menza.features.settings.domain.usecase.initialmenza.GetInitialMenzaModeUI
Expand All @@ -56,6 +58,8 @@ internal class SettingsViewModel(
val setPriceTypeUC: SetPriceTypeUC,
val getImagesOnMeteredUC: GetImagesOnMeteredUC,
val setImagesOnMeteredUC: SetImagesOnMeteredUC,
val getBalanceWarningThresholdUC: GetBalanceWarningThresholdUC,
val setBalanceWarningThresholdUC: SetBalanceWarningThresholdUC,
val getMenzaListUC: GetOrderedVisibleMenzaListUC,
val getInitialMenzaUC: GetInitialMenzaModeUI,
val setInitialMenzaUC: SetInitialMenzaUC,
Expand All @@ -78,6 +82,9 @@ internal class SettingsViewModel(
getImagesOnMeteredUC().onEach {
updateState { copy(downloadOnMetered = it) }
}.launchInVM()
getBalanceWarningThresholdUC().onEach {
updateState { copy(balanceWarningThreshold = it) }
}.launchInVM()
getInitialMenzaUC().onEach {
updateState { copy(initialMenzaBehaviour = it) }
}.launchInVM()
Expand All @@ -101,6 +108,10 @@ internal class SettingsViewModel(
setImagesOnMeteredUC(enable)
}

fun setBalanceWarningThreshold(threshold: Int) = launchVM {
setBalanceWarningThresholdUC(threshold)
}

fun setInitMenzaBehaviour(behaviour: InitialSelectionBehaviour) = launchVM {
setInitialMenzaUC(behaviour)
}
Expand All @@ -120,6 +131,7 @@ internal data class SettingsState(
val darkMode: DarkMode = Dark,
val priceType: PriceType = PriceType.Unset,
val downloadOnMetered: Boolean = false,
val balanceWarningThreshold: Int = 0,
val initialMenzaBehaviour: InitialSelectionBehaviour = InitialSelectionBehaviour.Ask,
val menzaList: ImmutableList<Menza> = persistentListOf(),
val selectedMenza: Menza? = null,
Expand Down
Loading

0 comments on commit f84f5e4

Please sign in to comment.