-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSettingsViewModel.kt
More file actions
251 lines (200 loc) · 8.12 KB
/
Copy pathSettingsViewModel.kt
File metadata and controls
251 lines (200 loc) · 8.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package to.bitkit.viewmodels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import to.bitkit.data.SettingsStore
import to.bitkit.models.TransactionSpeed
import javax.inject.Inject
@HiltViewModel
class SettingsViewModel @Inject constructor(
private val settingsStore: SettingsStore,
) : ViewModel() {
fun reset() = viewModelScope.launch { settingsStore.reset() }
val hasSeenSpendingIntro = settingsStore.data.map { it.hasSeenSpendingIntro }
.asStateFlow(initialValue = false)
val notificationsGranted = settingsStore.data.map { it.notificationsGranted }
.asStateFlow(initialValue = false)
fun setNotificationPreference(granted: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(notificationsGranted = granted) }
}
}
val showNotificationDetails = settingsStore.data.map { it.showNotificationDetails }
.asStateFlow(initialValue = false)
fun toggleNotificationDetails() {
viewModelScope.launch {
settingsStore.update { it.copy(showNotificationDetails = !it.showNotificationDetails) }
}
}
fun setHasSeenSpendingIntro(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(hasSeenSpendingIntro = value) }
}
}
val hasSeenWidgetsIntro = settingsStore.data.map { it.hasSeenWidgetsIntro }
.asStateFlow(initialValue = false)
fun setHasSeenWidgetsIntro(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(hasSeenWidgetsIntro = value) }
}
}
val hasSeenTransferIntro = settingsStore.data.map { it.hasSeenTransferIntro }
.asStateFlow(initialValue = false)
fun setHasSeenTransferIntro(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(hasSeenTransferIntro = value) }
}
}
val hasSeenSavingsIntro = settingsStore.data.map { it.hasSeenSavingsIntro }
.asStateFlow(initialValue = false)
fun setHasSeenSavingsIntro(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(hasSeenSavingsIntro = value) }
}
}
val hasSeenShopIntro = settingsStore.data.map { it.hasSeenShopIntro }
.asStateFlow(initialValue = false)
fun setHasSeenShopIntro(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(hasSeenShopIntro = value) }
}
}
val hasSeenProfileIntro = settingsStore.data.map { it.hasSeenProfileIntro }
.asStateFlow(initialValue = false)
fun setHasSeenProfileIntro(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(hasSeenProfileIntro = value) }
}
}
val quickPayIntroSeen = settingsStore.data.map { it.quickPayIntroSeen }
.asStateFlow(initialValue = false)
fun setQuickPayIntroSeen(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(quickPayIntroSeen = value) }
}
}
val bgPaymentsIntroSeen = settingsStore.data.map { it.bgPaymentsIntroSeen }
.asStateFlow(initialValue = false)
fun setBgPaymentsIntroSeen(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(bgPaymentsIntroSeen = value) }
}
}
val isPinForPaymentsEnabled = settingsStore.data.map { it.isPinForPaymentsEnabled }
.asStateFlow(initialValue = false)
fun setIsPinForPaymentsEnabled(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(isPinForPaymentsEnabled = value) }
}
}
val defaultTransactionSpeed = settingsStore.data.map { it.defaultTransactionSpeed }
.asStateFlow(initialValue = TransactionSpeed.Medium)
fun setDefaultTransactionSpeed(speed: TransactionSpeed) {
viewModelScope.launch {
settingsStore.update { it.copy(defaultTransactionSpeed = speed) }
}
}
val isDevModeEnabled = settingsStore.data.map { it.isDevModeEnabled }
.asStateFlow(initialValue = false)
fun setIsDevModeEnabled(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(isDevModeEnabled = value) }
}
}
val isPinEnabled = settingsStore.data.map { it.isPinEnabled }
.asStateFlow(SharingStarted.Eagerly, false)
val isBiometricEnabled = settingsStore.data.map { it.isBiometricEnabled }
.asStateFlow(SharingStarted.Eagerly, false)
fun setIsBiometricEnabled(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(isBiometricEnabled = value) }
}
}
val showWidgets = settingsStore.data.map { it.showWidgets }
.asStateFlow(initialValue = false)
fun setShowWidgets(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(showWidgets = value) }
}
}
val showWidgetTitles = settingsStore.data.map { it.showWidgetTitles }
.asStateFlow(initialValue = false)
fun setShowWidgetTitles(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(showWidgetTitles = value) }
}
}
val lastUsedTags = settingsStore.data.map { it.lastUsedTags }
.asStateFlow(initialValue = emptyList())
fun deleteLastUsedTag(tag: String) {
viewModelScope.launch {
settingsStore.deleteLastUsedTag(tag)
}
}
val isQuickpayEnabled = settingsStore.data.map { it.isQuickPayEnabled }
.asStateFlow(initialValue = false)
fun setIsQuickPayEnabled(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(isQuickPayEnabled = value) }
}
}
val quickPayAmount = settingsStore.data.map { it.quickPayAmount }
.asStateFlow(initialValue = 5)
fun setQuickPayAmount(value: Int) {
viewModelScope.launch {
settingsStore.update { it.copy(quickPayAmount = value) }
}
}
val enableSwipeToHideBalance = settingsStore.data.map { it.enableSwipeToHideBalance }
.asStateFlow(initialValue = true)
fun setEnableSwipeToHideBalance(value: Boolean) {
viewModelScope.launch {
settingsStore.update {
it.copy(
enableSwipeToHideBalance = value,
hideBalance = if (!value) false else it.hideBalance,
hideBalanceOnOpen = if (!value) false else it.hideBalanceOnOpen,
)
}
}
}
val hideBalance = settingsStore.data.map { it.hideBalance }
.asStateFlow(initialValue = false)
fun setHideBalance(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(hideBalance = value) }
}
}
val hideBalanceOnOpen = settingsStore.data.map { it.hideBalanceOnOpen }
.asStateFlow(initialValue = false)
fun setHideBalanceOnOpen(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(hideBalanceOnOpen = value) }
}
}
val enableAutoReadClipboard = settingsStore.data.map { it.enableAutoReadClipboard }
.asStateFlow(initialValue = false)
fun setEnableAutoReadClipboard(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(enableAutoReadClipboard = value) }
}
}
val enableSendAmountWarning = settingsStore.data.map { it.enableSendAmountWarning }
.asStateFlow(initialValue = false)
fun setEnableSendAmountWarning(value: Boolean) {
viewModelScope.launch {
settingsStore.update { it.copy(enableSendAmountWarning = value) }
}
}
// utils
private fun <T> Flow<T>.asStateFlow(
started: SharingStarted = SharingStarted.WhileSubscribed(5000),
initialValue: T,
): StateFlow<T> = stateIn(viewModelScope, started, initialValue)
}