Updated on 2026-08-14

This commit is contained in:
Tangem 2023-01-16 12:33:53 +04:00
commit 6ce21179d2
4 changed files with 26 additions and 44 deletions

View file

@ -8,10 +8,10 @@ import com.tangem.blockchain.common.WalletManager
import com.tangem.common.CompletionResult
import com.tangem.common.extensions.isZero
import com.tangem.common.services.Result
import com.tangem.core.analytics.Analytics
import com.tangem.domain.common.extensions.withMainContext
import com.tangem.operations.attestation.Attestation
import com.tangem.operations.attestation.OnlineCardVerifier
import com.tangem.core.analytics.Analytics
import com.tangem.tap.common.analytics.events.MainScreen
import com.tangem.tap.common.analytics.events.Token
import com.tangem.tap.common.extensions.copyToClipboard
@ -286,7 +286,7 @@ class WalletMiddleware {
is WalletAction.UserWalletChanged -> Unit
is WalletAction.WalletStoresChanged -> {
updateWalletStores(action.walletStores, walletState)
fetchTotalFiatBalance(action.walletStores, walletState)
fetchTotalFiatBalance(action.walletStores)
findMissedDerivations(action.walletStores)
tryToShowAppRatingWarning(action.walletStores)
}
@ -311,12 +311,9 @@ class WalletMiddleware {
}
}
private fun fetchTotalFiatBalance(walletStores: List<WalletStoreModel>, state: WalletState) {
private fun fetchTotalFiatBalance(walletStores: List<WalletStoreModel>) {
scope.launch(Dispatchers.Default) {
val totalFiatBalance = totalFiatBalanceCalculator.calculateOrNull(
prevAmount = state.totalBalance?.fiatAmount,
walletStores = walletStores,
)?.mapToReduxModel()
val totalFiatBalance = totalFiatBalanceCalculator.calculateOrNull(walletStores)?.mapToReduxModel()
if (totalFiatBalance != null) {
store.dispatchOnMain(WalletAction.TotalFiatBalanceChanged(totalFiatBalance))

View file

@ -6,9 +6,9 @@ import com.tangem.common.doOnFailure
import com.tangem.common.doOnSuccess
import com.tangem.common.flatMap
import com.tangem.common.map
import com.tangem.core.analytics.Analytics
import com.tangem.domain.common.ScanResponse
import com.tangem.domain.common.util.UserWalletId
import com.tangem.core.analytics.Analytics
import com.tangem.tap.common.analytics.events.MyWallets
import com.tangem.tap.common.extensions.dispatchOnMain
import com.tangem.tap.common.extensions.onUserWalletSelected
@ -98,21 +98,25 @@ internal class WalletSelectorMiddleware {
}
}
private fun updateBalances(walletStores: Map<UserWalletId, List<WalletStoreModel>>, state: WalletSelectorState) {
walletStores.forEach { (walletId, walletStores) ->
scope.launch(Dispatchers.Default) {
val foundWallet = state.wallets
.find { it.id == walletId }
private fun updateBalances(
updatedWalletStores: Map<UserWalletId, List<WalletStoreModel>>,
state: WalletSelectorState,
) {
if (updatedWalletStores.isNotEmpty()) scope.launch(Dispatchers.Default) {
state.wallets
.associateWith { updatedWalletStores[it.id] }
.forEach { (wallet, walletStores) ->
val isWalletTokensEmpty = (wallet.type as? UserWalletModel.Type.MultiCurrency)?.tokensCount == 0
val updatedWallet = if (walletStores == null && isWalletTokensEmpty) {
wallet.copy(fiatBalance = TotalFiatBalance.Loaded(BigDecimal.ZERO))
} else {
wallet.updateWalletStoresAndCalculateFiatBalance(walletStores.orEmpty())
}
if (foundWallet != null) {
val updatedWallet = foundWallet
.updateWalletStoresAndCalculateFiatBalance(walletStores)
if (foundWallet != updatedWallet) {
if (wallet != updatedWallet) {
store.dispatchOnMain(WalletSelectorAction.BalanceLoaded(updatedWallet))
}
}
}
}
}
@ -287,7 +291,6 @@ internal class WalletSelectorMiddleware {
selectedUserWallet == null -> {
store.dispatchOnMain(NavigationAction.PopBackTo(AppScreen.Welcome))
}
currentSelectedWalletId != selectedUserWallet.walletId -> {
store.onUserWalletSelected(selectedUserWallet)
}
@ -320,9 +323,8 @@ internal class WalletSelectorMiddleware {
is UserWalletModel.Type.SingleCurrency -> type
},
fiatBalance = totalFiatBalanceCalculator.calculate(
prevAmount = fiatBalance.amount,
walletStores = walletStores,
initial = TotalFiatBalance.Loaded(BigDecimal.ZERO),
initial = TotalFiatBalance.Loading,
),
)
}