-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMainViewModel.kt
More file actions
58 lines (51 loc) · 2.12 KB
/
MainViewModel.kt
File metadata and controls
58 lines (51 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.androidvip.sysctlgui.ui.main
import androidx.compose.material3.SnackbarResult
import androidx.lifecycle.viewModelScope
import com.androidvip.sysctlgui.data.Prefs
import com.androidvip.sysctlgui.data.repository.CONTRAST_LEVEL_NORMAL
import com.androidvip.sysctlgui.domain.repository.AppPrefs
import com.androidvip.sysctlgui.utils.BaseViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
class MainViewModel(
private val appPrefs: AppPrefs
) : BaseViewModel<MainViewEvent, MainViewState, MainViewEffect>() {
private val themeSettingsFlow: Flow<ThemeSettings> = combine(
appPrefs.observeKey(Prefs.ForceDarkTheme.key, false),
appPrefs.observeKey(Prefs.DynamicColors.key, true),
appPrefs.observeKey(Prefs.ContrastLevel.key, CONTRAST_LEVEL_NORMAL)
) { forceDark, dynamicColors, contrastLevel ->
ThemeSettings(forceDark, dynamicColors, contrastLevel)
}
val themeState = themeSettingsFlow.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000),
initialValue = loadInitialThemeState()
)
private fun loadInitialThemeState(): ThemeSettings {
return ThemeSettings(
forceDark = appPrefs.forceDark,
contrastLevel = appPrefs.contrastLevel,
dynamicColors = appPrefs.dynamicColors
)
}
override fun createInitialState() = MainViewState()
override fun onEvent(event: MainViewEvent) {
when (event) {
is MainViewEvent.OnSateChangeRequested -> {
setState { event.newState }
}
is MainViewEvent.ShowSnackbarRequested -> {
setEffect { MainViewEffect.ShowSnackbar(event.message, event.actionLabel) }
}
is MainViewEvent.OnSnackbarResult -> {
val snackbarResult = event.result
if (snackbarResult == SnackbarResult.ActionPerformed) {
setEffect { MainViewEffect.ActUponSckbarActionPerformed }
}
}
}
}
}