From 704ebd760800a6b23a2cd9cea74c481b9adf04f4 Mon Sep 17 00:00:00 2001 From: Tangem Date: Thu, 19 Jan 2023 16:41:20 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../DefaultWalletCurrenciesManager.kt | 38 +++++++++------- .../utils/WalletDataOperations.kt | 4 +- .../utils/WalletStoreOperations.kt | 43 ++++++++----------- 3 files changed, 43 insertions(+), 42 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 2af51d87a6..25e54b765d 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 @@ -1,5 +1,6 @@ package com.tangem.tap.domain.walletCurrencies.implementation +import com.tangem.blockchain.common.Blockchain import com.tangem.blockchain.common.DerivationStyle import com.tangem.common.CompletionResult import com.tangem.common.flatMap @@ -54,28 +55,17 @@ internal class DefaultWalletCurrenciesManager( currenciesToAdd: List, ): CompletionResult = withContext(Dispatchers.Default) { val card = userWallet.scanResponse.card - val updatedBlockchainNetworks = currenciesToAdd - .addMissingBlockchains(card) - .toBlockchainNetworks() val newCurrencies = (getSavedCurrencies(userWallet.walletId) + currenciesToAdd) .addMissingBlockchains(card) - updateWalletStores(userWallet, updatedBlockchainNetworks) + updateWalletStores(userWallet, newCurrencies.toBlockchainNetworks()) .map { saveUserCurrencies(card, newCurrencies) } .flatMap { - val updatedBlockchains = updatedBlockchainNetworks - .map { it.blockchain } - val updatedWalletStores = walletStoresRepository.get(userWallet.walletId) - .firstOrNull() - ?.filter { it.blockchain in updatedBlockchains } - ?: return@flatMap CompletionResult.Success(Unit) - - walletAmountsRepository.updateAmountsForWalletStores( - walletStores = updatedWalletStores, + updateWalletStoresAmounts( userWallet = userWallet, - fiatCurrency = appCurrencyProvider(), + updatedBlockchains = currenciesToAdd.map { it.blockchain }.distinct(), ) } } @@ -161,6 +151,10 @@ internal class DefaultWalletCurrenciesManager( return newCurrencies } + private fun findDerivationPath(currency: Currency, cardDerivationStyle: DerivationStyle?): String? { + return currency.derivationPath ?: currency.blockchain.derivationPath(cardDerivationStyle)?.rawPath + } + private suspend fun updateWalletStores( userWallet: UserWallet, blockchainNetworks: List, @@ -184,7 +178,19 @@ internal class DefaultWalletCurrenciesManager( .fold() } - private fun findDerivationPath(currency: Currency, cardDerivationStyle: DerivationStyle?): String? { - return currency.derivationPath ?: currency.blockchain.derivationPath(cardDerivationStyle)?.rawPath + private suspend fun updateWalletStoresAmounts( + userWallet: UserWallet, + updatedBlockchains: List, + ): CompletionResult { + val updatedWalletStores = walletStoresRepository.get(userWallet.walletId) + .firstOrNull() + ?.filter { it.blockchain in updatedBlockchains } + ?: return CompletionResult.Success(Unit) + + return walletAmountsRepository.updateAmountsForWalletStores( + walletStores = updatedWalletStores, + userWallet = userWallet, + fiatCurrency = appCurrencyProvider(), + ) } } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletDataOperations.kt b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletDataOperations.kt index 7ad0e38363..64e2241737 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletDataOperations.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletDataOperations.kt @@ -158,12 +158,12 @@ internal fun List.updateWithUnreachable(): List.updateWithSelf( - walletsData: List, + newWalletsData: List, ): List { val oldWalletsData = this val updatedWalletsData = arrayListOf() - walletsData.forEach { newWalletData -> + newWalletsData.forEach { newWalletData -> val walletDataToUpdate = oldWalletsData.find(newWalletData::isSameWalletData) if (walletDataToUpdate != null) { updatedWalletsData.add(walletDataToUpdate.updateWithSelf(newWalletData)) diff --git a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletStoreOperations.kt b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletStoreOperations.kt index 2aa7ffcf2d..0e9e9d8715 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletStoreOperations.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletStores/repository/implementation/utils/WalletStoreOperations.kt @@ -88,39 +88,34 @@ internal fun WalletStoreModel.updateWithRent(rent: WalletStoreModel.WalletRent?) ) } -internal fun WalletStoreModel.isSameWalletStore(other: WalletStoreModel): Boolean { - return this.blockchain == other.blockchain && - this.derivationPath == other.derivationPath -} - private inline fun List.replaceWalletStores( walletStoresToUpdate: List, update: (walletStore: WalletStoreModel) -> WalletStoreModel, ): List { val mutableStores = ArrayList(this) - walletStoresToUpdate - .asSequence() - .map { it.blockchain to it.derivationPath } - .forEach { (blockchain, derivationPath) -> - val index = mutableStores.indexOfFirst { - it.blockchain == blockchain && it.derivationPath == derivationPath - } - val currentWalletStore = mutableStores[index] - val updatedWalletStore = update(currentWalletStore) + walletStoresToUpdate.forEach { walletStoreToUpdate -> + val index = mutableStores.indexOfFirst(walletStoreToUpdate::isSameWalletStore) + val currentWalletStore = mutableStores[index] + val updatedWalletStore = update(currentWalletStore) - if (currentWalletStore != updatedWalletStore) { - Timber.d( - """ - Update wallet store in storage - |- User wallet ID: ${updatedWalletStore.userWalletId} - |- Blockchain: ${updatedWalletStore.blockchain} - """.trimIndent(), - ) + if (currentWalletStore != updatedWalletStore) { + Timber.d( + """ + Update wallet store in storage + |- User wallet ID: ${updatedWalletStore.userWalletId} + |- Blockchain: ${updatedWalletStore.blockchain} + """.trimIndent(), + ) - mutableStores[index] = updatedWalletStore - } + mutableStores[index] = updatedWalletStore } + } return mutableStores +} + +internal fun WalletStoreModel.isSameWalletStore(other: WalletStoreModel): Boolean { + return this.blockchain == other.blockchain && + this.derivationPath == other.derivationPath } \ No newline at end of file