Skip to content

Commit 5279809

Browse files
committed
feat: AI mode!!!
1 parent a31a22d commit 5279809

30 files changed

Lines changed: 343 additions & 26 deletions

File tree

api/core/src/commonMain/kotlin/cz/lastaapps/api/core/domain/model/RequestParams.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
2+
* Copyright 2025, Petr Laštovička as Lasta apps, All rights reserved
33
*
44
* This file is part of Menza.
55
*
@@ -19,7 +19,7 @@
1919

2020
package cz.lastaapps.api.core.domain.model
2121

22-
@JvmInline
23-
value class RequestParams(
22+
data class RequestParams(
2423
val language: DataLanguage,
24+
val aiMode: Boolean,
2525
)

api/main/src/commonMain/kotlin/cz/lastaapps/api/main/data/KocourkovRepoImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
2+
* Copyright 2025, Petr Laštovička as Lasta apps, All rights reserved
33
*
44
* This file is part of Menza.
55
*

api/main/src/commonMain/kotlin/cz/lastaapps/api/main/domain/usecase/GetImportantRequestParams.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
2+
* Copyright 2025, Petr Laštovička as Lasta apps, All rights reserved
33
*
44
* This file is part of Menza.
55
*
@@ -27,5 +27,5 @@ import cz.lastaapps.core.domain.UseCase
2727
class GetImportantRequestParams internal constructor(
2828
context: UCContext,
2929
) : UseCase(context) {
30-
operator fun invoke() = DataLanguage.entries.map { RequestParams(it) }
30+
operator fun invoke() = DataLanguage.entries.map { RequestParams(it, false) }
3131
}

api/main/src/commonMain/kotlin/cz/lastaapps/api/main/domain/usecase/SyncTodayDishListUC.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ import cz.lastaapps.core.domain.UseCase
3232
import cz.lastaapps.core.domain.error.ApiError.RatingError
3333
import kotlinx.coroutines.async
3434
import kotlinx.coroutines.coroutineScope
35+
import kotlinx.coroutines.delay
36+
import kotlinx.coroutines.flow.first
3537
import org.koin.core.component.KoinComponent
3638
import org.koin.core.component.get
3739
import org.koin.core.parameter.parametersOf
40+
import kotlin.random.Random
41+
import kotlin.time.Duration.Companion.milliseconds
3842

3943
class SyncTodayDishListUC(
4044
context: UCContext,
@@ -49,8 +53,15 @@ class SyncTodayDishListUC(
4953
coroutineScope {
5054
val syncDish =
5155
async {
56+
val params = getRequestParamsUC()
57+
58+
// It takes a while to think...
59+
if (params.first().aiMode) {
60+
delay(Random.nextInt(2000).milliseconds)
61+
}
62+
5263
get<TodayDishRepo> { parametersOf(menza.type) }.sync(
53-
getRequestParamsUC(),
64+
params,
5465
isForced = isForced,
5566
)
5667
}

app/src/main/kotlin/cz/lastaapps/menza/domain/usecase/GetRequestParamsUCImpl.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
2+
* Copyright 2025, Petr Laštovička as Lasta apps, All rights reserved
33
*
44
* This file is part of Menza.
55
*
@@ -35,9 +35,11 @@ internal class GetRequestParamsUCImpl(
3535
GetRequestParamsUC {
3636
override fun invoke(): Flow<RequestParams> =
3737
setting
38-
.getDishLanguage()
39-
.distinctUntilChanged()
38+
.getAllSettings()
4039
.map {
41-
RequestParams(language = it)
40+
RequestParams(
41+
language = it.dataLanguage,
42+
aiMode = it.aiMode,
43+
)
4244
}.distinctUntilChanged()
4345
}

app/src/main/kotlin/cz/lastaapps/menza/features/info/ui/screen/InfoScreen.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ private fun InfoContent(
176176
PullToRefreshWrapper(
177177
isRefreshing = state.isLoading,
178178
onRefresh = onRefresh,
179+
useAIMode = state.useAIMode,
179180
) {
180181
AboveOrSideBySideLayout(
181182
topLeft = contactAndMessage,

app/src/main/kotlin/cz/lastaapps/menza/features/info/ui/vm/InfoViewModel.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,22 @@ import cz.lastaapps.core.ui.vm.VMContext
3636
import cz.lastaapps.core.ui.vm.VMState
3737
import cz.lastaapps.core.util.extensions.localLogger
3838
import cz.lastaapps.menza.features.main.domain.usecase.GetSelectedMenzaUC
39+
import cz.lastaapps.menza.features.settings.domain.usecase.settings.GetAppSettingsUC
3940
import kotlinx.coroutines.CoroutineScope
4041
import kotlinx.coroutines.Job
4142
import kotlinx.coroutines.coroutineScope
4243
import kotlinx.coroutines.flow.collectLatest
4344
import kotlinx.coroutines.flow.launchIn
4445
import kotlinx.coroutines.flow.mapLatest
46+
import kotlinx.coroutines.flow.onEach
4547
import kotlinx.coroutines.launch
4648

4749
internal class InfoViewModel(
4850
context: VMContext,
4951
private val getSelectedMenza: GetSelectedMenzaUC,
5052
private val getInfo: GetInfoUC,
5153
private val syncInfo: SyncInfoUC,
54+
private val getSettingsUC: GetAppSettingsUC,
5255
) : StateViewModel<InfoState>(InfoState(), context),
5356
ErrorHolder {
5457
private val log = localLogger()
@@ -76,6 +79,11 @@ internal class InfoViewModel(
7679
}
7780
}
7881
}.launchIn(scope)
82+
83+
getSettingsUC()
84+
.onEach {
85+
updateState { copy(useAIMode = it.aiMode) }
86+
}.launchIn(scope)
7987
}
8088

8189
private var syncJob: Job? = null
@@ -96,7 +104,10 @@ internal class InfoViewModel(
96104
) {
97105
withLoading({ copy(isLoading = it) }) {
98106
when (val res = syncInfo(menza, isForced = isForced).mapSync()) {
99-
is Left -> updateState { copy(error = res.value) }
107+
is Left -> {
108+
updateState { copy(error = res.value) }
109+
}
110+
100111
is Right -> {}
101112
}
102113
}
@@ -113,4 +124,5 @@ internal data class InfoState(
113124
val isLoading: Boolean = false,
114125
val items: Info? = null,
115126
val error: DomainError? = null,
127+
val useAIMode: Boolean = false,
116128
) : VMState

app/src/main/kotlin/cz/lastaapps/menza/features/settings/data/MainSettingsRepoImpl.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ internal class MainSettingsRepoImpl(
6060
getAlternativeNavigation().distinctUntilChanged(),
6161
isDishListModeChosen().distinctUntilChanged(),
6262
getCurrency().distinctUntilChanged(),
63+
getAIMode().distinctUntilChanged(),
6364
) { arr ->
6465
AppSettings(
6566
initialMenzaMode = arr[0] as InitialSelectionBehaviour,
@@ -79,6 +80,7 @@ internal class MainSettingsRepoImpl(
7980
alternativeNavigation = arr[14] as Boolean,
8081
isDishListModeChosen = arr[15] as Boolean,
8182
currency = arr[16] as Currency,
83+
aiMode = arr[17] as Boolean,
8284
)
8385
}.distinctUntilChanged()
8486

@@ -162,4 +164,8 @@ internal class MainSettingsRepoImpl(
162164
override suspend fun setCurrency(currency: Currency) = general.setCurrency(currency)
163165

164166
override fun getCurrency(): Flow<Currency> = general.getCurrency().map { it ?: AppSettings.default.currency }
167+
168+
override suspend fun setAIMode(enabled: Boolean) = general.setAIMode(enabled)
169+
170+
override fun getAIMode(): Flow<Boolean> = general.getAIMode().map { it ?: AppSettings.default.aiMode }
165171
}

app/src/main/kotlin/cz/lastaapps/menza/features/settings/data/datasource/GeneralDataSource.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ internal interface GeneralDataSource {
103103
suspend fun setCurrency(currency: Currency)
104104

105105
fun getCurrency(): Flow<Currency?>
106+
107+
suspend fun setAIMode(enabled: Boolean)
108+
109+
fun getAIMode(): Flow<Boolean?>
106110
}
107111

108112
@OptIn(ExperimentalSettingsApi::class)
@@ -127,6 +131,7 @@ internal class GeneralDataSourceImpl(
127131
private const val alternativeNavigationKey = "alternative_navigation"
128132
private const val dishListModeChosenKey = "dish_list_mode_chosen"
129133
private const val currencyKey = "currency"
134+
private const val aiModeKey = "aiMode"
130135
}
131136

132137
override suspend fun storeAppSetupFinished() = settings.putBoolean(appSetupFinishedKey, true)
@@ -208,4 +213,8 @@ internal class GeneralDataSourceImpl(
208213
settings.getIntOrNullFlow(currencyKey).map { id ->
209214
Currency.entries.firstOrNull { type -> type.id == id }
210215
}
216+
217+
override suspend fun setAIMode(enabled: Boolean) = settings.putBoolean(aiModeKey, enabled)
218+
219+
override fun getAIMode(): Flow<Boolean?> = settings.getBooleanOrNullFlow(aiModeKey)
211220
}

app/src/main/kotlin/cz/lastaapps/menza/features/settings/di/settingsModule.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import cz.lastaapps.menza.features.settings.domain.usecase.settings.GetOliverRow
5959
import cz.lastaapps.menza.features.settings.domain.usecase.settings.GetPriceTypeUC
6060
import cz.lastaapps.menza.features.settings.domain.usecase.settings.GetSettingsEverOpenedUC
6161
import cz.lastaapps.menza.features.settings.domain.usecase.settings.OnSettingsOpenedUC
62+
import cz.lastaapps.menza.features.settings.domain.usecase.settings.SetAIMode
6263
import cz.lastaapps.menza.features.settings.domain.usecase.settings.SetAlternativeNavigationUC
6364
import cz.lastaapps.menza.features.settings.domain.usecase.settings.SetBalanceWarningThresholdUC
6465
import cz.lastaapps.menza.features.settings.domain.usecase.settings.SetCurrencyUC
@@ -110,6 +111,7 @@ val settingsModule =
110111
get(),
111112
get(),
112113
get(),
114+
get(),
113115
)
114116
// @formatter:on
115117
}
@@ -147,6 +149,7 @@ val settingsModule =
147149
factoryOf(::GetImageScaleRangeUC)
148150
factoryOf(::SetImagesOnMeteredUC)
149151
factoryOf(::SetOliverRow)
152+
factoryOf(::SetAIMode)
150153
factoryOf(::SetPriceTypeUC)
151154
factoryOf(::SetDishLanguageUC)
152155
factoryOf(::GetDishListModeUC)

0 commit comments

Comments
 (0)