From f6151916ad5e50802af4f4d73e61cefb9ea77966 Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 17 Dec 2024 14:17:03 +0400 Subject: [PATCH] Updated on 2026-08-14 --- .../repository/DefaultCurrenciesRepository.kt | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt b/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt index ea747277bc..4fb9ef47d2 100644 --- a/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt +++ b/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt @@ -138,32 +138,32 @@ internal class DefaultCurrenciesRepository( } private fun populateCurrenciesWithMissedCoins(currencies: List): List { - val networkToCoin = mutableMapOf() - val networkToTokens = mutableMapOf>() + val currenciesSequence = currencies.asSequence() - currencies.forEach { currency -> - when (currency) { - is CryptoCurrency.Coin -> { - networkToCoin[currency.network] = currency - } - is CryptoCurrency.Token -> { - val tokens = networkToTokens.getOrElse(currency.network, ::emptyList) - networkToTokens[currency.network] = tokens + currency - } - } - } + val networksWithTokens = currenciesSequence + .filterIsInstance() + .map { it.network } + .distinct() + + val networksWithCoins = currenciesSequence + .filterIsInstance() + .map { it.network } + .distinct() + + val networksNeedingCoins = (networksWithTokens - networksWithCoins.toSet()).toMutableList() + + if (networksNeedingCoins.isEmpty()) return currencies return buildList { - if (networkToTokens.isEmpty()) { - // add only coin here - addAll(networkToCoin.values) - } else { - networkToTokens.forEach { (network, networkTokens) -> - val networkCoin = networkToCoin[network] ?: cryptoCurrencyFactory.createCoin(network) + currencies.forEach { currency -> + if (currency is CryptoCurrency.Token && currency.network in networksNeedingCoins) { + val coin = cryptoCurrencyFactory.createCoin(currency.network) + add(coin) - add(networkCoin) - addAll(networkTokens) + networksNeedingCoins.remove(currency.network) } + + add(currency) } } }