-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAppWidgetPreferencesStore.kt
More file actions
91 lines (76 loc) · 3.13 KB
/
AppWidgetPreferencesStore.kt
File metadata and controls
91 lines (76 loc) · 3.13 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
package to.bitkit.appwidget
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.dataStore
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import to.bitkit.appwidget.model.AppWidgetData
import to.bitkit.appwidget.model.AppWidgetEntry
import to.bitkit.appwidget.model.AppWidgetType
import to.bitkit.data.dto.ArticleDTO
import to.bitkit.data.dto.price.GraphPeriod
import to.bitkit.data.dto.price.PriceDTO
import to.bitkit.data.serializers.AppWidgetDataSerializer
import javax.inject.Inject
import javax.inject.Singleton
private val Context.appWidgetDataStore: DataStore<AppWidgetData> by dataStore(
fileName = "appwidget_data.json",
serializer = AppWidgetDataSerializer,
)
@EntryPoint
@InstallIn(SingletonComponent::class)
interface AppWidgetEntryPoint {
fun appWidgetPreferencesStore(): AppWidgetPreferencesStore
}
@Singleton
class AppWidgetPreferencesStore @Inject constructor(
@ApplicationContext private val context: Context,
) {
private val store = context.appWidgetDataStore
val data: Flow<AppWidgetData> = store.data
suspend fun registerWidget(appWidgetId: Int, type: AppWidgetType) {
store.updateData { data ->
if (data.entries.any { it.appWidgetId == appWidgetId }) return@updateData data
data.copy(entries = data.entries + AppWidgetEntry(appWidgetId = appWidgetId, type = type))
}
}
suspend fun unregisterWidget(appWidgetId: Int) {
store.updateData { data ->
data.copy(entries = data.entries.filter { it.appWidgetId != appWidgetId })
}
}
suspend fun getEntry(appWidgetId: Int): AppWidgetEntry? =
store.data.first().entries.find { it.appWidgetId == appWidgetId }
suspend fun updateEntry(appWidgetId: Int, transform: (AppWidgetEntry) -> AppWidgetEntry) {
store.updateData { data ->
data.copy(
entries = data.entries.map {
if (it.appWidgetId == appWidgetId) transform(it) else it
},
)
}
}
suspend fun getActiveWidgetTypes(): Set<AppWidgetType> =
store.data.first().entries.map { it.type }.toSet()
suspend fun getActivePricePeriods(): Set<GraphPeriod> =
store.data.first().entries
.filter { it.type == AppWidgetType.PRICE }
.map { it.pricePreferences.period }
.toSet()
fun hasWidgetsOfType(type: AppWidgetType): Flow<Boolean> =
data.map { it.entries.any { entry -> entry.type == type } }
suspend fun cachePriceData(period: GraphPeriod, price: PriceDTO) {
store.updateData { it.copy(cachedPrices = it.cachedPrices + (period to price)) }
}
suspend fun cacheArticles(articles: List<ArticleDTO>) {
store.updateData { it.copy(cachedArticles = articles) }
}
suspend fun bumpArticleRotationTick() {
store.updateData { it.copy(articleRotationTick = it.articleRotationTick + 1) }
}
}