From 3390a98effb8cfe39a678edc6104f0f2dbca6c39 Mon Sep 17 00:00:00 2001 From: Tangem Date: Thu, 12 Jan 2023 16:07:03 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../DefaultWalletCurrenciesManager.kt | 65 +++++++------------ 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/domain/walletCurrencies/implementation/DefaultWalletCurrenciesManager.kt b/app/src/main/java/com/tangem/tap/domain/walletCurrencies/implementation/DefaultWalletCurrenciesManager.kt index e000fe1c6d..dd19a02575 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletCurrencies/implementation/DefaultWalletCurrenciesManager.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletCurrencies/implementation/DefaultWalletCurrenciesManager.kt @@ -128,34 +128,35 @@ internal class DefaultWalletCurrenciesManager( private fun List.addMissingBlockchains(card: CardDTO): List { val newCurrencies = arrayListOf() - for (currency in this.sortedByDescending { it is Currency.Blockchain }) { - when (currency) { - is Currency.Blockchain -> { - newCurrencies.add(currency.updateDerivationPath(card.derivationStyle)) + this + .groupBy { it.blockchain } + .forEach { (blockchain, currencies) -> + val rawDerivationPath: String? + val blockchainCurrency = currencies + .firstOrNull { it is Currency.Blockchain } + as? Currency.Blockchain + + // Add blockchain currency + if (blockchainCurrency != null) { + rawDerivationPath = findDerivationPath(blockchainCurrency, card.derivationStyle) + newCurrencies.add(blockchainCurrency.copy(derivationPath = rawDerivationPath)) + } else { + rawDerivationPath = findDerivationPath(currencies.first(), card.derivationStyle) + newCurrencies.add( + Currency.Blockchain( + blockchain = blockchain, + derivationPath = rawDerivationPath, + ), + ) } - is Currency.Token -> { - val containsTokenBlockchain = newCurrencies.any { - it.isBlockchain() && it.blockchain == currency.blockchain + // Add tokens currencies + currencies + .filterIsInstance() + .forEach { currency -> + newCurrencies.add(currency.copy(derivationPath = rawDerivationPath)) } - - if (containsTokenBlockchain) { - newCurrencies.add(currency.updateDerivationPath(card.derivationStyle)) - } else { - val derivationPath = findDerivationPath(currency, card.derivationStyle) - newCurrencies.add( - Currency.Blockchain( - blockchain = currency.blockchain, - derivationPath = derivationPath, - ), - ) - newCurrencies.add( - currency.copy(derivationPath = derivationPath), - ) - } - } } - } return newCurrencies } @@ -183,22 +184,6 @@ internal class DefaultWalletCurrenciesManager( .fold() } - private fun Currency.updateDerivationPath(cardDerivationStyle: DerivationStyle?): Currency { - val findDerivationPath: () -> String? = { - findDerivationPath(this, cardDerivationStyle) - } - - return when (this) { - is Currency.Blockchain -> this.copy( - derivationPath = findDerivationPath(), - ) - - is Currency.Token -> this.copy( - derivationPath = findDerivationPath(), - ) - } - } - private fun findDerivationPath(currency: Currency, cardDerivationStyle: DerivationStyle?): String? { return currency.derivationPath ?: currency.blockchain.derivationPath(cardDerivationStyle)?.rawPath }