mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 08:45:27 +02:00
Update currencies only when cache is not available
This commit is contained in:
parent
5554de21fb
commit
ecd06ec947
@ -199,23 +199,28 @@ class UnitsRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun updateRates(unit: AbstractUnit): LocalDate? = withContext(Dispatchers.IO) {
|
suspend fun updateRates(unit: AbstractUnit): LocalDate? = withContext(Dispatchers.IO) {
|
||||||
try {
|
var basedConversions = currencyRatesDao.getLatestRates(baseId = unit.id)
|
||||||
val conversions = CurrencyApi.service.getCurrencyPairs(unit.id)
|
val epochDay = LocalDate.now().toEpochDay()
|
||||||
val rates = conversions.currency
|
|
||||||
.map { (pairId, pairValue) ->
|
|
||||||
CurrencyRatesEntity(
|
|
||||||
baseUnitId = unit.id,
|
|
||||||
date = LocalDate.now().toEpochDay(),
|
|
||||||
pairUnitId = pairId,
|
|
||||||
pairUnitValue = BigDecimal.valueOf(pairValue)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
currencyRatesDao.insertRates(rates)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.d("UnitsRepository", "Skipped update: $e")
|
|
||||||
}
|
|
||||||
val basedConversions = currencyRatesDao.getLatestRates(baseId = unit.id)
|
|
||||||
|
|
||||||
|
if (basedConversions.firstOrNull()?.date != epochDay) {
|
||||||
|
try {
|
||||||
|
val conversions = CurrencyApi.service.getCurrencyPairs(unit.id)
|
||||||
|
val rates = conversions.currency
|
||||||
|
.map { (pairId, pairValue) ->
|
||||||
|
CurrencyRatesEntity(
|
||||||
|
baseUnitId = unit.id,
|
||||||
|
date = epochDay,
|
||||||
|
pairUnitId = pairId,
|
||||||
|
pairUnitValue = BigDecimal.valueOf(pairValue)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
currencyRatesDao.insertRates(rates)
|
||||||
|
|
||||||
|
basedConversions = currencyRatesDao.getLatestRates(baseId = unit.id)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.d("UnitsRepository", "Skipped update: $e")
|
||||||
|
}
|
||||||
|
}
|
||||||
myUnits.update { units ->
|
myUnits.update { units ->
|
||||||
units.map { localUnit ->
|
units.map { localUnit ->
|
||||||
if (localUnit.group != UnitGroup.CURRENCY) return@map localUnit
|
if (localUnit.group != UnitGroup.CURRENCY) return@map localUnit
|
||||||
|
Loading…
x
Reference in New Issue
Block a user