From e51be03285ae0fe3d26c4e37ba38d1e2aebefe34 Mon Sep 17 00:00:00 2001 From: Tangem Date: Thu, 21 Apr 2022 09:01:58 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../com/tangem/tap/domain/TapWalletManager.kt | 39 +++++------------- .../tap/features/wallet/redux/WalletState.kt | 40 +++++++++++-------- 2 files changed, 34 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt b/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt index 2fee4ac723..e1cd14ba2f 100644 --- a/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt +++ b/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt @@ -1,17 +1,14 @@ package com.tangem.tap.domain import com.tangem.blockchain.common.* -import com.tangem.common.extensions.guard import com.tangem.common.services.Result import com.tangem.domain.common.ScanResponse import com.tangem.domain.common.TapWorkarounds.derivationStyle import com.tangem.domain.common.TapWorkarounds.isStart2Coin import com.tangem.domain.common.TapWorkarounds.isTestCard -import com.tangem.domain.common.extensions.toCoinId import com.tangem.domain.common.extensions.withMainContext import com.tangem.network.api.tangemTech.TangemTechService import com.tangem.tap.common.ThrottlerWithValues -import com.tangem.tap.common.extensions.dispatchDebugErrorNotification import com.tangem.tap.common.extensions.dispatchOnMain import com.tangem.tap.common.extensions.safeUpdate import com.tangem.tap.common.redux.global.FiatCurrencyName @@ -122,30 +119,22 @@ class TapWalletManager { } val toUpdateCurrencies = currencies.filter { !fiatRatesThrottler.isStillThrottled(it) } - val toUpdateIds = toUpdateCurrencies.mapNotNull { it.id }.distinct() + val toUpdateIds = toUpdateCurrencies.mapNotNull { it.coinId }.distinct() if (toUpdateIds.isEmpty()) return //TODO: refactoring: move fiatRatesThrottler to the TangemTechRepository when (val result = tangemTechService.coins.prices(fiatCurrency, toUpdateIds)) { is Result.Success -> { - val missedCurrencies = mutableListOf() - val updatedCurrencies = mutableMapOf?>() - - result.data.prices.forEach { (name, value) -> - val currency = toUpdateCurrencies.firstOrNull { it.id == name }.guard { - missedCurrencies.add(name) - return@forEach - } - val priceResult = Result.Success(value.toBigDecimal()) - updatedCurrencies[currency] = priceResult - - fiatRatesThrottler.updateThrottlingTo(currency) - fiatRatesThrottler.setValue(currency, priceResult) + val priceResultList: Map> = result.data.prices.mapValues { + Result.Success(it.value.toBigDecimal()) } - - if (missedCurrencies.isNotEmpty()) { - val missedNames = missedCurrencies.joinToString(", ") - store.dispatchDebugErrorNotification("Not found currencies to update: [$missedNames]") + val updatedCurrencies = mutableMapOf?>() + toUpdateCurrencies.forEach { currency -> + priceResultList[currency.coinId]?.let { + updatedCurrencies[currency] = it + fiatRatesThrottler.updateThrottlingTo(currency) + fiatRatesThrottler.setValue(currency, it) + } } handleFiatRatesResult(updatedCurrencies) } @@ -328,10 +317,4 @@ class TapWalletManager { fun Wallet.getFirstToken(): Token? { return getTokens().toList().getOrNull(0) -} - -val Currency.id: String? - get() = when (this) { - is Currency.Blockchain -> blockchain.toCoinId() - is Currency.Token -> token.id - } \ No newline at end of file +} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt index de5d1ee593..35db5ab1c9 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt @@ -5,6 +5,7 @@ import com.tangem.blockchain.common.* import com.tangem.blockchain.common.address.AddressType import com.tangem.blockchain.extensions.isAboveZero import com.tangem.common.extensions.isZero +import com.tangem.domain.common.extensions.toCoinId import com.tangem.domain.features.addCustomToken.CustomCurrency import com.tangem.tap.common.entities.Button import com.tangem.tap.common.extensions.toQrCode @@ -60,7 +61,7 @@ data class WalletState( val shouldShowDetails: Boolean = primaryWallet?.currencyData?.status != BalanceStatus.EmptyCard && - primaryWallet?.currencyData?.status != BalanceStatus.UnknownBlockchain + primaryWallet?.currencyData?.status != BalanceStatus.UnknownBlockchain val blockchains: List get() = wallets.mapNotNull { it.walletManager?.wallet?.blockchain } @@ -87,8 +88,8 @@ data class WalletState( if (blockchain == null) return null return walletsData.find { it.currency is Currency.Blockchain && - it.currency.blockchain == blockchain.blockchain && - it.currency.derivationPath == blockchain.derivationPath + it.currency.blockchain == blockchain.blockchain && + it.currency.derivationPath == blockchain.derivationPath } } @@ -96,7 +97,7 @@ data class WalletState( if (currency == null) return null return wallets.firstOrNull { it.blockchainNetwork.derivationPath == currency.derivationPath && - (it.blockchainNetwork.blockchain == currency.blockchain) + (it.blockchainNetwork.blockchain == currency.blockchain) } } @@ -111,7 +112,7 @@ data class WalletState( if (blockchainNetwork == null) return null return wallets.firstOrNull { it.blockchainNetwork.derivationPath == blockchainNetwork.derivationPath && - (it.blockchainNetwork.blockchain == blockchainNetwork.blockchain) + (it.blockchainNetwork.blockchain == blockchainNetwork.blockchain) } } @@ -141,23 +142,23 @@ data class WalletState( if (walletData.currency is Currency.Blockchain) { return wallet.recentTransactions.toPendingTransactions(wallet.address).isEmpty() && - wallet.amounts.toSendableAmounts().isEmpty() + wallet.amounts.toSendableAmounts().isEmpty() } else if (walletData.currency is Currency.Token) ( - return wallet.recentTransactions.toPendingTransactionsForToken( - walletData.currency.token, wallet.address - ).isEmpty() - && wallet.amounts[AmountType.Token(token = walletData.currency.token)] - ?.isAboveZero() != true - ) + return wallet.recentTransactions.toPendingTransactionsForToken( + walletData.currency.token, wallet.address + ).isEmpty() + && wallet.amounts[AmountType.Token(token = walletData.currency.token)] + ?.isAboveZero() != true + ) } return false } private fun isPrimaryCurrency(walletData: WalletData): Boolean { return (walletData.currency is Currency.Blockchain && - walletData.currency.blockchain == store.state.walletState.primaryBlockchain) - || (walletData.currency is Currency.Token && - walletData.currency.token == store.state.walletState.primaryToken) + walletData.currency.blockchain == store.state.walletState.primaryBlockchain) + || (walletData.currency is Currency.Token && + walletData.currency.token == store.state.walletState.primaryToken) } fun replaceWalletInWallets(wallet: WalletStore?): List { @@ -214,7 +215,7 @@ data class WalletState( return if (walletData.currency is Currency.Blockchain) { val walletStores = wallets.filterNot { it.blockchainNetwork.blockchain == walletData.currency.blockchain - && it.blockchainNetwork.derivationPath == walletData.currency.derivationPath + && it.blockchainNetwork.derivationPath == walletData.currency.derivationPath } copy(wallets = walletStores) } else { @@ -371,6 +372,11 @@ data class WalletRent( sealed interface Currency { + val coinId: String? + get() = when (this) { + is Blockchain -> blockchain.toCoinId() + is Token -> token.id + } val blockchain: com.tangem.blockchain.common.Blockchain val currencySymbol: CryptoCurrencyName val derivationPath: String? @@ -450,7 +456,7 @@ data class WalletStore( fun updateWallets(walletDataList: List): WalletStore { val relevantWalletDataList = walletDataList.filter { it.currency.blockchain == blockchainNetwork.blockchain && - it.currency.derivationPath == blockchainNetwork.derivationPath + it.currency.derivationPath == blockchainNetwork.derivationPath }.toMutableList() val updatedWalletDataList = walletsData.map { walletData -> val matchingWalletData = relevantWalletDataList.find { it.currency == walletData.currency }