Updated on 2026-08-14

This commit is contained in:
Tangem 2023-08-09 13:24:48 +03:00
parent d1a09370c7
commit 3b33d4db0a
24 changed files with 363 additions and 484 deletions

View file

@ -36,7 +36,7 @@ internal class DefaultNetworksRepository(
private val responseCurrenciesFactory by lazy { ResponseCurrenciesFactory(DemoConfig()) }
private val networkStatusFactory by lazy { NetworkStatusFactory() }
private val networksStatuses: MutableStateFlow<HashSet<NetworkStatus>> = MutableStateFlow(hashSetOf())
private val networksStatuses: MutableStateFlow<List<NetworkStatus>> = MutableStateFlow(emptyList())
override fun getNetworks(networksIds: Set<Network.ID>): Set<Network> {
return networkConverter.convertSet(networksIds)
@ -48,7 +48,9 @@ internal class DefaultNetworksRepository(
refresh: Boolean,
): Flow<Set<NetworkStatus>> = channelFlow {
launch(dispatchers.io) {
networksStatuses.collect(::send)
networksStatuses.collect {
send(it.toSet())
}
}
launch(dispatchers.io) {
@ -82,17 +84,22 @@ internal class DefaultNetworksRepository(
private suspend fun fetchNetworkStatus(userWalletId: UserWalletId, networkId: Network.ID) {
val currencies = getCurrencies(userWalletId)
.asSequence()
.filter { it.networkId == networkId }
val result = walletManagersFacade.update(
userWalletId = userWalletId,
networkId = networkId,
extraTokens = currencies.filterIsInstanceTo(hashSetOf()),
)
val networkStatus = networkStatusFactory.createNetworkStatus(networkId, result, currencies)
val networkStatus = networkStatusFactory.createNetworkStatus(
networkId = networkId,
result = result,
currencies = currencies.toSet(),
)
networksStatuses.update { statuses ->
statuses.apply {
addOrReplace(networkStatus) { it.networkId == networkStatus.networkId }
}
statuses.addOrReplace(networkStatus) { it.networkId == networkStatus.networkId }
}
}

View file

@ -3,21 +3,27 @@ package com.tangem.data.tokens.repository
import com.tangem.domain.tokens.model.CryptoCurrency
import com.tangem.domain.tokens.model.Quote
import com.tangem.domain.tokens.repository.QuotesRepository
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.channelFlow
import java.math.BigDecimal
import kotlin.random.Random
internal class MockQuotesRepository : QuotesRepository {
override fun getQuotes(currenciesIds: Set<CryptoCurrency.ID>, refresh: Boolean): Flow<Set<Quote>> {
return flowOf(
currenciesIds.map {
return channelFlow {
val quotes = currenciesIds.map {
Quote(
currencyId = it,
fiatRate = BigDecimal.ZERO,
priceChange = BigDecimal.ZERO,
)
}.toSet(),
)
}.toSet()
delay(Random.nextLong(from = 200, until = 2_000))
send(quotes)
}
}
}

View file

@ -5,7 +5,6 @@ import com.tangem.domain.tokens.model.NetworkStatus
import com.tangem.domain.tokens.models.Network
import com.tangem.domain.walletmanager.model.CryptoCurrencyAmount
import com.tangem.domain.walletmanager.model.UpdateWalletManagerResult
import timber.log.Timber
import java.math.BigDecimal
internal class NetworkStatusFactory {
@ -33,25 +32,20 @@ internal class NetworkStatusFactory {
amounts: Set<CryptoCurrencyAmount>,
currencies: Set<CryptoCurrency>,
): Map<CryptoCurrency.ID, BigDecimal> {
val formattedAmounts = hashMapOf<CryptoCurrency.ID, BigDecimal>()
currencies.forEach { currency ->
val amount = when (currency) {
is CryptoCurrency.Coin -> amounts.singleOrNull { it is CryptoCurrencyAmount.Coin }
is CryptoCurrency.Token -> amounts.singleOrNull {
it is CryptoCurrencyAmount.Token &&
it.id == getTokenIdString(currency.id) &&
it.tokenContractAddress == currency.contractAddress
return amounts
.asSequence()
.mapNotNull { amount ->
val currency = when (amount) {
is CryptoCurrencyAmount.Coin -> currencies.singleOrNull { it is CryptoCurrency.Coin }
is CryptoCurrencyAmount.Token -> currencies.firstOrNull {
it is CryptoCurrency.Token &&
getTokenIdString(it.id) == amount.id &&
it.contractAddress == amount.tokenContractAddress
}
}
}?.value
if (amount == null) {
Timber.e("Unable to find a token amount for: ${currency.name}")
} else {
formattedAmounts[currency.id] = amount
currency?.id?.let { it to amount.value }
}
}
return formattedAmounts
.toMap()
}
}