-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSettingsViewModel.kt
More file actions
72 lines (63 loc) · 2.32 KB
/
SettingsViewModel.kt
File metadata and controls
72 lines (63 loc) · 2.32 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.rickyhu.hushkeyboard.settings
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.rickyhu.hushkeyboard.data.AppSettings
import com.rickyhu.hushkeyboard.data.SettingsRepository
import com.rickyhu.hushkeyboard.data.ThemeOption
import com.rickyhu.hushkeyboard.data.WideNotationOption
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
@HiltViewModel
class SettingsViewModel @Inject constructor(
private val settingsRepository: SettingsRepository
) : ViewModel() {
val settingsState = settingsRepository.settings.stateIn(
viewModelScope,
SharingStarted.WhileSubscribed(5000),
AppSettings()
).map { settings ->
SettingsState(
themeOption = settings.themeOption,
addSpaceAfterNotation = settings.addSpaceAfterNotation,
smartDelete = settings.smartDelete,
vibrateOnTap = settings.vibrateOnTap,
wideNotationOption = settings.wideNotationOption
)
}
fun updateThemeOption(themeOption: ThemeOption) {
viewModelScope.launch {
settingsRepository.updateThemeOption(themeOption)
}
}
fun updateWideNotationOption(wideNotationOption: WideNotationOption) {
viewModelScope.launch {
settingsRepository.updateWideNotationOption(wideNotationOption)
}
}
fun updateSmartDelete(smartDelete: Boolean) {
viewModelScope.launch {
settingsRepository.updateSmartDelete(smartDelete)
}
}
fun updateAddSpaceBetweenNotation(addSpaceBetweenNotation: Boolean) {
viewModelScope.launch {
settingsRepository.updateAddSpaceBetweenNotation(addSpaceBetweenNotation)
}
}
fun updateVibrateOnTap(vibrateOnTap: Boolean) {
viewModelScope.launch {
settingsRepository.updateVibrateOnTap(vibrateOnTap)
}
}
}
data class SettingsState(
val themeOption: ThemeOption = ThemeOption.System,
val wideNotationOption: WideNotationOption = WideNotationOption.WideWithW,
val smartDelete: Boolean = true,
val addSpaceAfterNotation: Boolean = true,
val vibrateOnTap: Boolean = true
)