-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWidgetsStore.kt
More file actions
186 lines (162 loc) · 5.92 KB
/
WidgetsStore.kt
File metadata and controls
186 lines (162 loc) · 5.92 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
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.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.serialization.Serializable
import to.bitkit.data.dto.ArticleDTO
import to.bitkit.data.dto.BlockDTO
import to.bitkit.data.dto.WeatherDTO
import to.bitkit.data.dto.price.PriceDTO
import to.bitkit.data.serializers.WidgetsSerializer
import to.bitkit.models.WidgetType
import to.bitkit.models.WidgetWithPosition
import to.bitkit.models.WidgetsBackupV1
import to.bitkit.models.widget.BlocksPreferences
import to.bitkit.models.widget.CalculatorValues
import to.bitkit.models.widget.FactsPreferences
import to.bitkit.models.widget.HeadlinePreferences
import to.bitkit.models.widget.PricePreferences
import to.bitkit.models.widget.WeatherPreferences
import to.bitkit.utils.Logger
import javax.inject.Inject
import javax.inject.Singleton
private val Context.widgetsDataStore: DataStore<WidgetsData> by dataStore(
fileName = "widgets.json",
serializer = WidgetsSerializer,
)
@Suppress("TooManyFunctions")
@Singleton
class WidgetsStore @Inject constructor(
@ApplicationContext private val context: Context,
) {
private val store = context.widgetsDataStore
val data: Flow<WidgetsData> = store.data
val articlesFlow: Flow<List<ArticleDTO>> = data.map { it.articles }
val factsFlow: Flow<List<String>> = data.map { it.facts }
val blocksFlow: Flow<BlockDTO?> = data.map { it.block }
val weatherFlow: Flow<WeatherDTO?> = data.map { it.weather }
val priceFlow: Flow<PriceDTO?> = data.map { it.price }
suspend fun restoreFromBackup(payload: WidgetsBackupV1) =
runCatching {
val data = payload.widgets
store.updateData { data }
}.onSuccess {
Logger.debug("Restored widgets", TAG)
}
suspend fun updateCalculatorValues(calculatorValues: CalculatorValues) {
store.updateData {
it.copy(calculatorValues = calculatorValues)
}
}
suspend fun updateArticles(articles: List<ArticleDTO>) {
store.updateData {
it.copy(articles = articles)
}
}
suspend fun updateHeadlinePreferences(preferences: HeadlinePreferences) {
store.updateData {
it.copy(headlinePreferences = preferences)
}
}
suspend fun updateFactsPreferences(preferences: FactsPreferences) {
store.updateData {
it.copy(factsPreferences = preferences)
}
}
suspend fun updateBlocksPreferences(preferences: BlocksPreferences) {
store.updateData {
it.copy(blocksPreferences = preferences)
}
}
suspend fun updateWeatherPreferences(preferences: WeatherPreferences) {
store.updateData {
it.copy(weatherPreferences = preferences)
}
}
suspend fun updatePricePreferences(preferences: PricePreferences) {
store.updateData { currentStore ->
currentStore.copy(
pricePreferences = preferences.copy(
enabledPairs = preferences.enabledPairs.sortedBy { tradingPair ->
tradingPair.position
}
)
)
}
}
suspend fun updateFacts(facts: List<String>) {
store.updateData {
it.copy(facts = facts)
}
}
suspend fun updateBlock(block: BlockDTO) {
store.updateData {
it.copy(block = block)
}
}
suspend fun updateWeather(weather: WeatherDTO) {
store.updateData {
it.copy(weather = weather)
}
}
suspend fun updatePrice(price: PriceDTO) {
store.updateData {
it.copy(price = price)
}
}
suspend fun reset() {
store.updateData { WidgetsData() }
Logger.info("Deleted all widgets data.")
}
suspend fun addWidget(type: WidgetType) {
if (store.data.first().widgets.map { it.type }.contains(type)) return
store.updateData { data ->
val nextPosition = (data.widgets.maxOfOrNull { it.position } ?: -1) + 1
data.copy(
widgets = (data.widgets + WidgetWithPosition(type = type, position = nextPosition))
.sortedBy { it.position }
)
}
}
suspend fun deleteWidget(type: WidgetType) {
if (!store.data.first().widgets.map { it.type }.contains(type)) return
store.updateData { data ->
val updated = data.copy(widgets = data.widgets.filterNot { it.type == type })
when (type) {
WidgetType.CALCULATOR -> updated.copy(calculatorValues = CalculatorValues())
else -> updated
}
}
}
suspend fun updateWidgets(widgets: List<WidgetWithPosition>) {
store.updateData {
it.copy(widgets = widgets)
}
}
companion object {
private const val TAG = "WidgetsStore"
}
}
@Serializable
data class WidgetsData(
val widgets: List<WidgetWithPosition> = listOf(
WidgetWithPosition(type = WidgetType.SUGGESTIONS, position = 0),
WidgetWithPosition(type = WidgetType.PRICE, position = 1),
WidgetWithPosition(type = WidgetType.BLOCK, position = 2),
),
val headlinePreferences: HeadlinePreferences = HeadlinePreferences(),
val factsPreferences: FactsPreferences = FactsPreferences(),
val blocksPreferences: BlocksPreferences = BlocksPreferences(),
val weatherPreferences: WeatherPreferences = WeatherPreferences(),
val pricePreferences: PricePreferences = PricePreferences(),
val calculatorValues: CalculatorValues = CalculatorValues(),
val articles: List<ArticleDTO> = emptyList(),
val facts: List<String> = emptyList(),
val block: BlockDTO? = null,
val weather: WeatherDTO? = null,
val price: PriceDTO? = null,
)