|
| 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.currentCoroutineContext |
| 7 | +import kotlinx.coroutines.delay |
| 8 | +import kotlinx.coroutines.flow.Flow |
| 9 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 10 | +import kotlinx.coroutines.flow.StateFlow |
| 11 | +import kotlinx.coroutines.flow.asStateFlow |
| 12 | +import kotlinx.coroutines.flow.combine |
| 13 | +import kotlinx.coroutines.flow.distinctUntilChanged |
| 14 | +import kotlinx.coroutines.flow.flow |
| 15 | +import kotlinx.coroutines.flow.flowOn |
| 16 | +import kotlinx.coroutines.flow.map |
| 17 | +import kotlinx.coroutines.flow.update |
| 18 | +import kotlinx.coroutines.isActive |
| 19 | +import kotlinx.coroutines.launch |
| 20 | +import kotlinx.coroutines.withContext |
| 21 | +import to.bitkit.data.CacheStore |
| 22 | +import to.bitkit.data.SettingsStore |
| 23 | +import to.bitkit.di.BgDispatcher |
| 24 | +import to.bitkit.env.Env |
| 25 | +import to.bitkit.models.BitcoinDisplayUnit |
| 26 | +import to.bitkit.models.ConvertedAmount |
| 27 | +import to.bitkit.models.FxRate |
| 28 | +import to.bitkit.models.PrimaryDisplay |
| 29 | +import to.bitkit.models.Toast |
| 30 | +import to.bitkit.services.CurrencyService |
| 31 | +import to.bitkit.ui.shared.toast.ToastEventBus |
| 32 | +import to.bitkit.utils.Logger |
| 33 | +import java.util.Date |
| 34 | +import javax.inject.Inject |
| 35 | +import javax.inject.Singleton |
| 36 | + |
| 37 | +@Singleton |
| 38 | +class CurrencyRepo @Inject constructor( |
| 39 | + @BgDispatcher private val bgDispatcher: CoroutineDispatcher, |
| 40 | + private val currencyService: CurrencyService, |
| 41 | + private val settingsStore: SettingsStore, |
| 42 | + private val cacheStore: CacheStore |
| 43 | +) { |
| 44 | + private val repoScope = CoroutineScope(bgDispatcher + SupervisorJob()) |
| 45 | + |
| 46 | + private val _currencyState = MutableStateFlow(CurrencyState()) |
| 47 | + val currencyState: StateFlow<CurrencyState> = _currencyState.asStateFlow() |
| 48 | + |
| 49 | + private var lastSuccessfulRefresh: Date? = null |
| 50 | + private var isRefreshing = false |
| 51 | + |
| 52 | + private val pollingFlow: Flow<Unit> |
| 53 | + get() = flow { |
| 54 | + while (currentCoroutineContext().isActive) { |
| 55 | + emit(Unit) |
| 56 | + delay(Env.fxRateRefreshInterval) |
| 57 | + } |
| 58 | + }.flowOn(bgDispatcher) |
| 59 | + |
| 60 | + init { |
| 61 | + startPolling() |
| 62 | + observeStaleData() |
| 63 | + collectCachedData() |
| 64 | + } |
| 65 | + |
| 66 | + private fun startPolling() { |
| 67 | + repoScope.launch { |
| 68 | + pollingFlow.collect { |
| 69 | + refresh() |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private fun observeStaleData() { |
| 75 | + repoScope.launch { |
| 76 | + currencyState.map { it.hasStaleData }.distinctUntilChanged().collect { isStale -> |
| 77 | + if (isStale) { |
| 78 | + ToastEventBus.send( |
| 79 | + type = Toast.ToastType.ERROR, |
| 80 | + title = "Rates currently unavailable", |
| 81 | + description = "An error has occurred. Please try again later." |
| 82 | + ) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private fun collectCachedData() { |
| 89 | + repoScope.launch { |
| 90 | + combine(settingsStore.data, cacheStore.data) { settings, cachedData -> |
| 91 | + _currencyState.value.copy( |
| 92 | + rates = cachedData.cachedRates, |
| 93 | + selectedCurrency = settings.selectedCurrency, |
| 94 | + displayUnit = settings.displayUnit, |
| 95 | + primaryDisplay = settings.primaryDisplay, |
| 96 | + currencySymbol = cachedData.cachedRates.firstOrNull { rate -> |
| 97 | + rate.quote == settings.selectedCurrency |
| 98 | + }?.currencySymbol ?: "$" |
| 99 | + ) |
| 100 | + }.collect { newState -> |
| 101 | + _currencyState.update { newState } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + suspend fun triggerRefresh() = withContext(bgDispatcher) { |
| 107 | + refresh() |
| 108 | + } |
| 109 | + |
| 110 | + private suspend fun refresh() { |
| 111 | + if (isRefreshing) return |
| 112 | + isRefreshing = true |
| 113 | + try { |
| 114 | + val fetchedRates = currencyService.fetchLatestRates() |
| 115 | + cacheStore.update { it.copy(cachedRates = fetchedRates) } |
| 116 | + _currencyState.update { |
| 117 | + it.copy( |
| 118 | + error = null, |
| 119 | + hasStaleData = false |
| 120 | + ) |
| 121 | + } |
| 122 | + lastSuccessfulRefresh = Date() |
| 123 | + Logger.debug("Currency rates refreshed successfully", context = TAG) |
| 124 | + } catch (e: Exception) { |
| 125 | + _currencyState.update { it.copy(error = e) } |
| 126 | + Logger.error("Currency rates refresh failed", e, context = TAG) |
| 127 | + |
| 128 | + lastSuccessfulRefresh?.let { last -> |
| 129 | + _currencyState.update { |
| 130 | + it.copy(hasStaleData = Date().time - last.time > Env.fxRateStaleThreshold) |
| 131 | + } |
| 132 | + } |
| 133 | + } finally { |
| 134 | + isRefreshing = false |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + suspend fun togglePrimaryDisplay() = withContext(bgDispatcher) { |
| 139 | + currencyState.value.primaryDisplay.let { |
| 140 | + val newDisplay = if (it == PrimaryDisplay.BITCOIN) PrimaryDisplay.FIAT else PrimaryDisplay.BITCOIN |
| 141 | + settingsStore.update { it.copy(primaryDisplay = newDisplay) } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + suspend fun setPrimaryDisplayUnit(unit: PrimaryDisplay) = withContext(bgDispatcher) { |
| 146 | + settingsStore.update { it.copy(primaryDisplay = unit) } |
| 147 | + } |
| 148 | + |
| 149 | + suspend fun setBtcDisplayUnit(unit: BitcoinDisplayUnit) = withContext(bgDispatcher) { |
| 150 | + settingsStore.update { it.copy(displayUnit = unit) } |
| 151 | + } |
| 152 | + |
| 153 | + suspend fun setSelectedCurrency(currency: String) = withContext(bgDispatcher) { |
| 154 | + settingsStore.update { it.copy(selectedCurrency = currency) } |
| 155 | + refresh() |
| 156 | + } |
| 157 | + |
| 158 | + fun getCurrencySymbol(): String { |
| 159 | + val currentState = currencyState.value |
| 160 | + return currentState.rates.firstOrNull { it.quote == currentState.selectedCurrency }?.currencySymbol ?: "" |
| 161 | + } |
| 162 | + |
| 163 | + // Conversion helpers |
| 164 | + fun convertSatsToFiat(sats: Long, currency: String? = null): ConvertedAmount? { |
| 165 | + val targetCurrency = currency ?: currencyState.value.selectedCurrency |
| 166 | + val rate = currencyService.getCurrentRate(targetCurrency, currencyState.value.rates) |
| 167 | + return rate?.let { currencyService.convert(sats = sats, rate = it) } |
| 168 | + } |
| 169 | + |
| 170 | + fun convertFiatToSats(fiatAmount: Double, currency: String? = null): Long { |
| 171 | + val sourceCurrency = currency ?: currencyState.value.selectedCurrency |
| 172 | + return currencyService.convertFiatToSats(fiatAmount, sourceCurrency, currencyState.value.rates) |
| 173 | + } |
| 174 | + |
| 175 | + companion object { |
| 176 | + private const val TAG = "CurrencyRepo" |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +data class CurrencyState( |
| 181 | + val rates: List<FxRate> = emptyList(), |
| 182 | + val error: Throwable? = null, |
| 183 | + val hasStaleData: Boolean = false, |
| 184 | + val selectedCurrency: String = "USD", |
| 185 | + val currencySymbol: String = "$", |
| 186 | + val displayUnit: BitcoinDisplayUnit = BitcoinDisplayUnit.MODERN, |
| 187 | + val primaryDisplay: PrimaryDisplay = PrimaryDisplay.BITCOIN, |
| 188 | +) |
0 commit comments