Updated on 2026-08-14
This commit is contained in:
commit
6ce21179d2
4 changed files with 26 additions and 44 deletions
|
|
@ -2,33 +2,23 @@ package com.tangem.tap.domain.totalBalance
|
|||
|
||||
import com.tangem.tap.domain.model.TotalFiatBalance
|
||||
import com.tangem.tap.domain.model.WalletStoreModel
|
||||
import java.math.BigDecimal
|
||||
|
||||
interface TotalFiatBalanceCalculator {
|
||||
|
||||
/**
|
||||
* Calculate total fiat balance for list of [WalletStoreModel]
|
||||
* @param prevAmount Previous amount, used in [TotalFiatBalance.Refreshing]
|
||||
* @param walletStores List of [WalletStoreModel] to calculate fiat amount
|
||||
* @param initial Initial [TotalFiatBalance] state, used when list of [WalletStoreModel] is empty
|
||||
* @return [TotalFiatBalance] with state found with the [WalletStoreModel] list
|
||||
* */
|
||||
suspend fun calculate(
|
||||
prevAmount: BigDecimal?,
|
||||
walletStores: List<WalletStoreModel>,
|
||||
initial: TotalFiatBalance,
|
||||
): TotalFiatBalance
|
||||
suspend fun calculate(walletStores: List<WalletStoreModel>, initial: TotalFiatBalance): TotalFiatBalance
|
||||
|
||||
/**
|
||||
* Same as [TotalFiatBalanceCalculator.calculate] but returns null if list of [WalletStoreModel] is empty
|
||||
* @param prevAmount Previous amount, used in [TotalFiatBalance.Refreshing]
|
||||
* @param walletStores List of [WalletStoreModel] to calculate fiat amount
|
||||
* @return [TotalFiatBalance] with state found with the [WalletStoreModel] list
|
||||
* */
|
||||
suspend fun calculateOrNull(
|
||||
prevAmount: BigDecimal?,
|
||||
walletStores: List<WalletStoreModel>,
|
||||
): TotalFiatBalance?
|
||||
suspend fun calculateOrNull(walletStores: List<WalletStoreModel>): TotalFiatBalance?
|
||||
|
||||
companion object
|
||||
}
|
||||
|
|
@ -10,18 +10,11 @@ import kotlinx.coroutines.withContext
|
|||
import java.math.BigDecimal
|
||||
|
||||
internal class DefaultTotalFiatBalanceCalculator : TotalFiatBalanceCalculator {
|
||||
override suspend fun calculate(
|
||||
prevAmount: BigDecimal?,
|
||||
walletStores: List<WalletStoreModel>,
|
||||
initial: TotalFiatBalance,
|
||||
): TotalFiatBalance {
|
||||
return calculateOrNull(prevAmount, walletStores) ?: initial
|
||||
override suspend fun calculate(walletStores: List<WalletStoreModel>, initial: TotalFiatBalance): TotalFiatBalance {
|
||||
return calculateOrNull(walletStores) ?: initial
|
||||
}
|
||||
|
||||
override suspend fun calculateOrNull(
|
||||
prevAmount: BigDecimal?,
|
||||
walletStores: List<WalletStoreModel>,
|
||||
): TotalFiatBalance? {
|
||||
override suspend fun calculateOrNull(walletStores: List<WalletStoreModel>): TotalFiatBalance? {
|
||||
return if (walletStores.isEmpty()) {
|
||||
null
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue