|
| 1 | +package to.bitkit.repositories |
| 2 | + |
| 3 | +import kotlinx.coroutines.CoroutineDispatcher |
| 4 | +import kotlinx.coroutines.CoroutineScope |
| 5 | +import kotlinx.coroutines.SupervisorJob |
| 6 | +import kotlinx.coroutines.delay |
| 7 | +import kotlinx.coroutines.flow.Flow |
| 8 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 9 | +import kotlinx.coroutines.flow.StateFlow |
| 10 | +import kotlinx.coroutines.flow.asStateFlow |
| 11 | +import kotlinx.coroutines.flow.map |
| 12 | +import kotlinx.coroutines.flow.update |
| 13 | +import kotlinx.coroutines.launch |
| 14 | +import kotlinx.coroutines.withContext |
| 15 | +import to.bitkit.data.WidgetsStore |
| 16 | +import to.bitkit.data.widgets.NewsService |
| 17 | +import to.bitkit.data.widgets.WidgetService |
| 18 | +import to.bitkit.di.BgDispatcher |
| 19 | +import to.bitkit.models.WidgetType |
| 20 | +import to.bitkit.utils.Logger |
| 21 | +import javax.inject.Inject |
| 22 | +import javax.inject.Singleton |
| 23 | +import kotlin.time.Duration.Companion.minutes |
| 24 | + |
| 25 | +@Singleton |
| 26 | +class WidgetsRepo @Inject constructor( |
| 27 | + @BgDispatcher private val bgDispatcher: CoroutineDispatcher, |
| 28 | + private val newsService: NewsService, |
| 29 | + private val widgetsStore: WidgetsStore |
| 30 | +) { |
| 31 | + private val repoScope = CoroutineScope(bgDispatcher + SupervisorJob()) |
| 32 | + |
| 33 | + val widgetsDataFlow = widgetsStore.data |
| 34 | + val articlesFlow = widgetsStore.articlesFlow |
| 35 | + |
| 36 | + private val _refreshStates = MutableStateFlow( |
| 37 | + WidgetType.entries.associateWith { false } |
| 38 | + ) |
| 39 | + val refreshStates: StateFlow<Map<WidgetType, Boolean>> = _refreshStates.asStateFlow() |
| 40 | + |
| 41 | + init { |
| 42 | + startPeriodicUpdates() |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Start periodic updates for all widgets |
| 47 | + */ |
| 48 | + private fun startPeriodicUpdates() { |
| 49 | + startPeriodicUpdate(newsService) { articles -> |
| 50 | + widgetsStore.updateArticles(articles) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Generic method to start periodic updates for any widget service |
| 56 | + */ |
| 57 | + private fun <T> startPeriodicUpdate( |
| 58 | + service: WidgetService<T>, |
| 59 | + updateStore: suspend (T) -> Unit |
| 60 | + ) { |
| 61 | + repoScope.launch { |
| 62 | + while (true) { |
| 63 | + updateWidget(service, updateStore) |
| 64 | + delay(service.refreshInterval) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Update a specific widget type |
| 71 | + */ |
| 72 | + private suspend fun <T> updateWidget( |
| 73 | + service: WidgetService<T>, |
| 74 | + updateStore: suspend (T) -> Unit |
| 75 | + ) { |
| 76 | + val widgetType = service.widgetType |
| 77 | + _refreshStates.update { it + (widgetType to true) } |
| 78 | + |
| 79 | + service.fetchData() |
| 80 | + .onSuccess { data -> |
| 81 | + updateStore(data) |
| 82 | + Logger.debug("Updated $widgetType widget successfully") |
| 83 | + } |
| 84 | + .onFailure { error -> |
| 85 | + Logger.warn(e = error, msg = "Failed to update $widgetType widget", context = TAG) |
| 86 | + } |
| 87 | + |
| 88 | + _refreshStates.update { it + (widgetType to false) } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Manually refresh all widgets |
| 93 | + */ |
| 94 | + suspend fun refreshAllWidgets(): Result<Unit> = runCatching { |
| 95 | + listOf( |
| 96 | + updateWidget(newsService) { articles -> |
| 97 | + widgetsStore.updateArticles(articles) |
| 98 | + }, |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Manually refresh specific widget |
| 104 | + */ |
| 105 | + suspend fun refreshWidget(widgetType: WidgetType): Result<Unit> = runCatching { |
| 106 | + when (widgetType) { |
| 107 | + WidgetType.NEWS -> updateWidget(newsService) { articles -> |
| 108 | + widgetsStore.updateArticles(articles) |
| 109 | + } |
| 110 | + WidgetType.WEATHER -> { |
| 111 | + // TODO: Implement when PriceService is ready |
| 112 | + throw NotImplementedError("Weather widget not implemented yet") |
| 113 | + } |
| 114 | + WidgetType.PRICE -> { |
| 115 | + // TODO: Implement when PriceService is ready |
| 116 | + throw NotImplementedError("Price widget not implemented yet") |
| 117 | + } |
| 118 | + WidgetType.BLOCK -> { |
| 119 | + // TODO: Implement when BlockService is ready |
| 120 | + throw NotImplementedError("Block widget not implemented yet") |
| 121 | + } |
| 122 | + WidgetType.CALCULATOR -> { |
| 123 | + // TODO: Implement when CalculatorService is ready |
| 124 | + throw NotImplementedError("Calculator widget not implemented yet") |
| 125 | + } |
| 126 | + WidgetType.FACTS -> { |
| 127 | + // TODO: Implement when FactsService is ready |
| 128 | + throw NotImplementedError("Facts widget not implemented yet") |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Get refresh state for a specific widget type |
| 135 | + */ |
| 136 | + fun getRefreshState(widgetType: WidgetType): Flow<Boolean> { |
| 137 | + return refreshStates.map { it[widgetType] ?: false } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Check if a widget type is currently supported |
| 142 | + */ |
| 143 | + fun isWidgetSupported(widgetType: WidgetType): Boolean { |
| 144 | + return when (widgetType) { |
| 145 | + WidgetType.NEWS, WidgetType.WEATHER -> true |
| 146 | + else -> false |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + companion object { |
| 151 | + private const val TAG = "WidgetsRepo" |
| 152 | + } |
| 153 | +} |
0 commit comments