Updated on 2026-08-14
This commit is contained in:
parent
82eda99ca7
commit
5aa2af25f1
9 changed files with 110 additions and 55 deletions
|
|
@ -45,7 +45,7 @@ internal class DefaultQuotesRepository(
|
|||
.filterNotNull()
|
||||
.flatMapLatest { appCurrency ->
|
||||
fetchExpiredQuotes(currenciesIds, appCurrency.id, refresh = refresh)
|
||||
quotesStore.get(currenciesIds).map(quotesConverter::convertSet)
|
||||
quotesStore.get(currenciesIds).map { quotesConverter.convert(currenciesIds to it) }
|
||||
}
|
||||
.cancellable()
|
||||
.flowOn(dispatchers.io)
|
||||
|
|
@ -77,14 +77,15 @@ internal class DefaultQuotesRepository(
|
|||
|
||||
val quotes = quotesStore.getSync(currenciesIds)
|
||||
|
||||
quotesConverter.convertSet(quotes)
|
||||
quotesConverter.convert(currenciesIds to quotes)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getQuoteSync(currencyId: CryptoCurrency.ID): Quote? {
|
||||
return withContext(dispatchers.io) {
|
||||
val quote = quotesStore.getSync(setOf(currencyId)).firstOrNull()
|
||||
quote?.let { quotesConverter.convert(it) }
|
||||
val setOfCurrencyId = setOf(currencyId)
|
||||
val quote = quotesStore.getSync(setOfCurrencyId).firstOrNull()
|
||||
quote?.let { quotesConverter.convert(setOfCurrencyId to setOf(it)).firstOrNull() }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,16 +1,28 @@
|
|||
package com.tangem.data.tokens.utils
|
||||
|
||||
import com.tangem.datasource.local.quote.model.StoredQuote
|
||||
import com.tangem.domain.tokens.model.CryptoCurrency
|
||||
import com.tangem.domain.tokens.model.Quote
|
||||
import com.tangem.utils.converter.Converter
|
||||
import java.math.BigDecimal
|
||||
|
||||
internal class QuotesConverter : Converter<StoredQuote, Quote> {
|
||||
typealias QuotesConverterValue = Pair<Set<CryptoCurrency.ID>, Set<StoredQuote>>
|
||||
|
||||
override fun convert(value: StoredQuote): Quote {
|
||||
val (rawCurrencyId, responseQuote) = value
|
||||
internal class QuotesConverter : Converter<QuotesConverterValue, Set<Quote>> {
|
||||
|
||||
return Quote(
|
||||
override fun convert(value: QuotesConverterValue): Set<Quote> {
|
||||
val (setOfCurrencyId, setOfStoredQuote) = value
|
||||
return setOfCurrencyId.mapTo(hashSetOf()) { id ->
|
||||
setOfStoredQuote.find { id.rawCurrencyId == it.rawCurrencyId }
|
||||
?.let(::convertExistStoredQuote)
|
||||
?: Quote.Empty(id.rawCurrencyId)
|
||||
}
|
||||
}
|
||||
|
||||
private fun convertExistStoredQuote(storedQuote: StoredQuote): Quote {
|
||||
val (rawCurrencyId, responseQuote) = storedQuote
|
||||
|
||||
return Quote.Value(
|
||||
rawCurrencyId = rawCurrencyId,
|
||||
fiatRate = responseQuote.price ?: BigDecimal.ZERO,
|
||||
priceChange = (responseQuote.priceChange24h ?: BigDecimal.ZERO).movePointLeft(2),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue