-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSettingsStore.kt
More file actions
141 lines (126 loc) · 5.29 KB
/
SettingsStore.kt
File metadata and controls
141 lines (126 loc) · 5.29 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
package to.bitkit.data
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.dataStore
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.Serializable
import to.bitkit.data.serializers.SettingsSerializer
import to.bitkit.env.Env
import to.bitkit.models.BitcoinDisplayUnit
import to.bitkit.models.CoinSelectionPreference
import to.bitkit.models.DEFAULT_ADDRESS_TYPE_STRING
import to.bitkit.models.PrimaryDisplay
import to.bitkit.models.SettingsBackupV1
import to.bitkit.models.Suggestion
import to.bitkit.models.TransactionSpeed
import to.bitkit.utils.Logger
import javax.inject.Inject
import javax.inject.Singleton
private val Context.settingsDataStore: DataStore<SettingsData> by dataStore(
fileName = "settings.json",
serializer = SettingsSerializer,
)
@Singleton
class SettingsStore @Inject constructor(
@ApplicationContext private val context: Context,
) {
private val store = context.settingsDataStore
val data: Flow<SettingsData> = store.data
@Volatile
var restoredMonitoredTypesFromBackup: Boolean = false
private set
suspend fun restoreFromBackup(payload: SettingsBackupV1) =
runCatching {
val data = payload.settings.resetPin()
store.updateData { data }
val monitored = data.addressTypesToMonitor
val selected = data.selectedAddressType
restoredMonitoredTypesFromBackup = monitored.size > 1 ||
(monitored.size == 1 && monitored.first() != selected)
}.onSuccess {
Logger.debug("Restored settings, monitoredFromBackup=$restoredMonitoredTypesFromBackup", context = TAG)
}
suspend fun update(transform: (SettingsData) -> SettingsData) {
store.updateData(transform)
}
suspend fun addLastUsedTag(newTag: String) {
store.updateData { currentSettings ->
val combinedTags = (listOf(newTag) + currentSettings.lastUsedTags).distinct()
val limitedTags = combinedTags.take(MAX_LAST_USED_TAGS)
currentSettings.copy(lastUsedTags = limitedTags)
}
}
suspend fun deleteLastUsedTag(tag: String) {
store.updateData { currentSettings ->
currentSettings.copy(lastUsedTags = currentSettings.lastUsedTags.filter { it != tag })
}
}
suspend fun addDismissedSuggestion(suggestion: Suggestion) {
store.updateData { currentSettings ->
val updatedDismissedSuggestions = (currentSettings.dismissedSuggestions + suggestion.name).distinct()
currentSettings.copy(dismissedSuggestions = updatedDismissedSuggestions)
}
}
suspend fun reset() {
store.updateData { SettingsData() }
restoredMonitoredTypesFromBackup = false
Logger.info("Deleted all user settings data.")
}
companion object {
private const val TAG = "SettingsStore"
private const val MAX_LAST_USED_TAGS = 10
}
}
@Serializable
data class SettingsData(
val primaryDisplay: PrimaryDisplay = PrimaryDisplay.BITCOIN,
val displayUnit: BitcoinDisplayUnit = BitcoinDisplayUnit.MODERN,
val selectedCurrency: String = "USD",
val defaultTransactionSpeed: TransactionSpeed = TransactionSpeed.default(),
val showEmptyBalanceView: Boolean = true,
val hasSeenSpendingIntro: Boolean = false,
val hasSeenWidgetsIntro: Boolean = false,
val hasSeenTransferIntro: Boolean = false,
val hasSeenSavingsIntro: Boolean = false,
val hasSeenShopIntro: Boolean = false,
val hasSeenProfileIntro: Boolean = false,
val hasSeenContactsIntro: Boolean = false,
val quickPayIntroSeen: Boolean = false,
val bgPaymentsIntroSeen: Boolean = false,
val isQuickPayEnabled: Boolean = false,
val quickPayAmount: Int = 5,
val lightningSetupStep: Int = 0,
val isPinEnabled: Boolean = false,
val isBiometricEnabled: Boolean = false,
val isPinForPaymentsEnabled: Boolean = false,
val isDevModeEnabled: Boolean = Env.isDebug,
val showWidgets: Boolean = true,
val showWidgetTitles: Boolean = false,
val lastUsedTags: List<String> = emptyList(),
val enableSwipeToHideBalance: Boolean = true,
val hideBalance: Boolean = false,
val hideBalanceOnOpen: Boolean = false,
val enableAutoReadClipboard: Boolean = false,
val enableSendAmountWarning: Boolean = false,
val backupVerified: Boolean = false,
val notificationsGranted: Boolean = false,
val showNotificationDetails: Boolean = true,
val dismissedSuggestions: List<String> = emptyList(),
val balanceWarningIgnoredMillis: Long = 0,
val backupWarningIgnoredMillis: Long = 0,
val notificationsIgnoredMillis: Long = 0,
val balanceWarningTimes: Int = 0,
val coinSelectAuto: Boolean = true,
val coinSelectPreference: CoinSelectionPreference = CoinSelectionPreference.BranchAndBound,
val electrumServer: String = Env.electrumServerUrl,
val rgsServerUrl: String? = Env.ldkRgsServerUrl,
val selectedAddressType: String = DEFAULT_ADDRESS_TYPE_STRING,
val addressTypesToMonitor: List<String> = listOf(DEFAULT_ADDRESS_TYPE_STRING),
val pendingRestoreAddressTypePrune: Boolean = false,
)
fun SettingsData.resetPin() = this.copy(
isPinEnabled = false,
isPinForPaymentsEnabled = false,
isBiometricEnabled = false,
)