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 25e54b765d..aff78739f8 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 @@ -79,7 +79,6 @@ internal class DefaultWalletCurrenciesManager( .filter { it !in currenciesToRemove } val remainingBlockchains = remainingCurrencies .filterIsInstance() - .map { it.blockchain } walletStoresRepository.deleteDifference(userWallet.walletId, remainingBlockchains) .flatMap { @@ -116,43 +115,50 @@ internal class DefaultWalletCurrenciesManager( } private fun List.addMissingBlockchains(card: CardDTO): List { - val newCurrencies = arrayListOf() + if (this.isEmpty()) return this + val currencies = this.asSequence() - 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, - ), - ) - } - - // Add tokens currencies - currencies - .filterIsInstance() - .forEach { currency -> - newCurrencies.add(currency.copy(derivationPath = rawDerivationPath)) - } + return currencies + .groupBy { currency -> + findBlockchainCurrency(currency, currencies, card.derivationStyle) + } + .mapValues { (blockchainCurrency, blockchainCurrencies) -> + findBlockchainTokens(blockchainCurrency, blockchainCurrencies) + } + .flatMap { (blockchainCurrency, blockchainTokens) -> + arrayListOf(blockchainCurrency) + blockchainTokens } - - return newCurrencies } - private fun findDerivationPath(currency: Currency, cardDerivationStyle: DerivationStyle?): String? { - return currency.derivationPath ?: currency.blockchain.derivationPath(cardDerivationStyle)?.rawPath + private fun findBlockchainCurrency( + currency: Currency, + currencies: Sequence, + cardDerivationStyle: DerivationStyle?, + ): Currency.Blockchain { + return currencies + .filterIsInstance() + .firstOrNull { + it.blockchain == currency.blockchain && + it.derivationPath == currency.derivationPath + } + ?: Currency.Blockchain( + blockchain = currency.blockchain, + derivationPath = currency.derivationPath + ?: currency.blockchain.derivationPath(cardDerivationStyle)?.rawPath, + ) + } + + private fun findBlockchainTokens( + blockchainCurrency: Currency.Blockchain, + blockchainCurrencies: List, + ): List { + return blockchainCurrencies + .filterIsInstance() + .map { token -> + token.copy( + derivationPath = token.derivationPath ?: blockchainCurrency.derivationPath, + ) + } } private suspend fun updateWalletStores( diff --git a/app/src/main/java/com/tangem/tap/domain/walletStores/implementation/DefaultWalletStoresManager.kt b/app/src/main/java/com/tangem/tap/domain/walletStores/implementation/DefaultWalletStoresManager.kt index 22e86d2fa1..58ee4e72e0 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletStores/implementation/DefaultWalletStoresManager.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletStores/implementation/DefaultWalletStoresManager.kt @@ -18,6 +18,7 @@ import com.tangem.tap.domain.walletStores.WalletStoresManager import com.tangem.tap.domain.walletStores.repository.WalletAmountsRepository import com.tangem.tap.domain.walletStores.repository.WalletManagersRepository import com.tangem.tap.domain.walletStores.repository.WalletStoresRepository +import com.tangem.tap.features.wallet.models.Currency import com.tangem.tap.features.wallet.models.toBlockchainNetworks import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow @@ -126,7 +127,9 @@ internal class DefaultWalletStoresManager( .also { blockchainNetworks -> walletStoresRepository.deleteDifference( userWalletId = userWalletId, - currentBlockchains = blockchainNetworks.map { it.blockchain }, + currentBlockchains = blockchainNetworks.map { + Currency.Blockchain(it.blockchain, it.derivationPath) + }, ) } .map { blockchainNetwork -> diff --git a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/WalletStoresRepository.kt b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/WalletStoresRepository.kt index 7b07a6eee0..9e8a8a9287 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/WalletStoresRepository.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/WalletStoresRepository.kt @@ -1,9 +1,9 @@ package com.tangem.tap.domain.walletStores.repository -import com.tangem.blockchain.common.Blockchain import com.tangem.common.CompletionResult import com.tangem.domain.common.util.UserWalletId import com.tangem.tap.domain.model.WalletStoreModel +import com.tangem.tap.features.wallet.models.Currency import kotlinx.coroutines.flow.Flow interface WalletStoresRepository { @@ -17,7 +17,7 @@ interface WalletStoresRepository { suspend fun deleteDifference( userWalletId: UserWalletId, - currentBlockchains: List, + currentBlockchains: List, ): CompletionResult suspend fun clear(): CompletionResult diff --git a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/DefaultWalletStoresRepository.kt b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/DefaultWalletStoresRepository.kt index b9087070e4..dba9881976 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/DefaultWalletStoresRepository.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/DefaultWalletStoresRepository.kt @@ -1,6 +1,5 @@ package com.tangem.tap.domain.walletStores.repository.implementation -import com.tangem.blockchain.common.Blockchain import com.tangem.common.CompletionResult import com.tangem.common.catching import com.tangem.domain.common.util.UserWalletId @@ -10,6 +9,7 @@ import com.tangem.tap.domain.walletStores.repository.implementation.utils.isSame import com.tangem.tap.domain.walletStores.repository.implementation.utils.replaceWalletStore import com.tangem.tap.domain.walletStores.repository.implementation.utils.updateWithSelf import com.tangem.tap.domain.walletStores.storage.WalletStoresStorage +import com.tangem.tap.features.wallet.models.Currency import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.firstOrNull @@ -43,13 +43,13 @@ internal class DefaultWalletStoresRepository : WalletStoresRepository { override suspend fun deleteDifference( userWalletId: UserWalletId, - currentBlockchains: List, + currentBlockchains: List, ): CompletionResult = catching { - if (currentBlockchains != getSync(userWalletId).map { it.blockchain }) { + if (currentBlockchains != getSync(userWalletId)) { walletStoresStorage.update { prevStores -> prevStores.apply { this[userWalletId] = this[userWalletId] - ?.filter { it.blockchain in currentBlockchains } + ?.filter { it.blockchainWalletData.currency in currentBlockchains } .orEmpty() } }