Updated on 2026-08-14

This commit is contained in:
Tangem 2022-05-25 12:05:51 +03:00
parent e362149f9e
commit eaa1215307
6 changed files with 70 additions and 61 deletions

View file

@ -32,6 +32,8 @@ import com.tangem.tap.features.wallet.models.WalletRent
import com.tangem.tap.features.wallet.models.WalletWarning
import com.tangem.tap.features.wallet.models.toPendingTransactions
import com.tangem.tap.features.wallet.models.toPendingTransactionsForToken
import com.tangem.tap.features.wallet.redux.reducers.calculateTotalFiatAmount
import com.tangem.tap.features.wallet.redux.reducers.findTotalBalanceState
import com.tangem.tap.features.wallet.ui.BalanceStatus
import com.tangem.tap.features.wallet.ui.BalanceWidgetData
import com.tangem.tap.network.exchangeServices.CurrencyExchangeManager
@ -200,11 +202,11 @@ data class WalletState(
.distinct().map { getWalletStore(it) }.mapNotNull { it?.updateWallets(walletsData) }
return updateWalletStores(walletStores)
}
fun updateWalletStore(walletStore: WalletStore?): WalletState {
return copy(wallets = replaceWalletInWallets(walletStore))
.updateTotalBalance()
}
private fun updateWalletStores(walletStores: List<WalletStore>): WalletState {
@ -221,6 +223,7 @@ data class WalletState(
}
}
return copy(wallets = updatedWallets + walletStoresMutable)
.updateTotalBalance()
}
fun removeWallet(walletData: WalletData?): WalletState {
@ -282,11 +285,18 @@ data class WalletState(
}
}
fun updateTotalBalance(
totalBalance: TotalBalance?
): WalletState {
return this.copy(
totalBalance = totalBalance
private fun updateTotalBalance(): WalletState {
return if (wallets.isNotEmpty()) {
val walletsData = wallets.flatMap(WalletStore::walletsData)
this.copy(
totalBalance = TotalBalance(
state = walletsData.findTotalBalanceState(),
fiatAmount = walletsData.calculateTotalFiatAmount(),
fiatCurrency = store.state.globalState.appCurrency
)
)
} else this.copy(
totalBalance = null
)
}
}

View file

@ -10,7 +10,12 @@ import com.tangem.tap.domain.getFirstToken
import com.tangem.tap.domain.tokens.BlockchainNetwork
import com.tangem.tap.features.wallet.models.removeUnknownTransactions
import com.tangem.tap.features.wallet.models.toPendingTransactions
import com.tangem.tap.features.wallet.redux.*
import com.tangem.tap.features.wallet.redux.Currency
import com.tangem.tap.features.wallet.redux.WalletAction
import com.tangem.tap.features.wallet.redux.WalletData
import com.tangem.tap.features.wallet.redux.WalletMainButton
import com.tangem.tap.features.wallet.redux.WalletState
import com.tangem.tap.features.wallet.redux.WalletStore
import com.tangem.tap.features.wallet.ui.BalanceStatus
import com.tangem.tap.features.wallet.ui.BalanceWidgetData
import com.tangem.tap.features.wallet.ui.TokenData
@ -126,7 +131,7 @@ class MultiWalletReducer {
),
fiatAmountFormatted = tokenWalletData.fiatRate?.let {
action.amount.value
?.toFiatString(it, store.state.globalState.appCurrency.code)
?.toFiatString(it, store.state.globalState.appCurrency.symbol)
},
blockchainAmount = wallet.amounts[AmountType.Coin]?.value ?: BigDecimal.ZERO
),

View file

@ -11,7 +11,11 @@ import com.tangem.tap.domain.getFirstToken
import com.tangem.tap.domain.tokens.BlockchainNetwork
import com.tangem.tap.features.wallet.models.removeUnknownTransactions
import com.tangem.tap.features.wallet.models.toPendingTransactions
import com.tangem.tap.features.wallet.redux.*
import com.tangem.tap.features.wallet.redux.Currency
import com.tangem.tap.features.wallet.redux.ProgressState
import com.tangem.tap.features.wallet.redux.TradeCryptoState
import com.tangem.tap.features.wallet.redux.WalletMainButton
import com.tangem.tap.features.wallet.redux.WalletState
import com.tangem.tap.features.wallet.ui.BalanceStatus
import com.tangem.tap.features.wallet.ui.BalanceWidgetData
import com.tangem.tap.features.wallet.ui.TokenData
@ -97,7 +101,7 @@ class OnWalletLoadedReducer {
token.symbol
),
fiatAmount = tokenFiatAmount,
fiatAmountFormatted = tokenFiatAmount?.toFormattedFiatValue(fiatCurrency.code)
fiatAmountFormatted = tokenFiatAmount?.toFormattedFiatValue(fiatCurrency.symbol)
),
pendingTransactions = tokenPendingTransactions.removeUnknownTransactions(),
mainButton = WalletMainButton.SendButton(tokenSendButton),
@ -114,9 +118,6 @@ class OnWalletLoadedReducer {
}
return walletState
.updateWalletsData(wallets)
.updateTotalBalance(
totalBalance = obtainTotalBalance(wallets, fiatCurrency)
)
.copy(
state = state,
error = null

View file

@ -1,37 +1,21 @@
package com.tangem.tap.features.wallet.redux.reducers
import com.tangem.tap.common.entities.FiatCurrency
import com.tangem.tap.features.wallet.models.TotalBalance
import com.tangem.tap.features.wallet.redux.WalletData
import com.tangem.tap.features.wallet.ui.BalanceStatus
import java.math.BigDecimal
fun obtainTotalBalance(
wallets: List<WalletData>,
appCurrency: FiatCurrency
): TotalBalance? {
return if (wallets.isNotEmpty()) {
TotalBalance(
state = wallets.findTotalBalanceState(),
fiatAmount = wallets.calculateTotalFiatAmount(),
fiatCurrency = appCurrency
)
} else null
}
private fun List<WalletData>.findTotalBalanceState(): TotalBalance.State {
fun List<WalletData>.findTotalBalanceState(): TotalBalance.State {
return this.mapToTotalBalanceState()
.fold(initial = TotalBalance.State.Loading) { accState, newState ->
accState or newState
}
.reduce(TotalBalance.State::or)
}
private fun List<WalletData>.calculateTotalFiatAmount(): BigDecimal {
fun List<WalletData>.calculateTotalFiatAmount(): BigDecimal {
return this.map { it.currencyData.fiatAmount ?: BigDecimal.ZERO }
.reduce(BigDecimal::plus)
}
private fun List<WalletData>.mapToTotalBalanceState(): List<TotalBalance.State> {
fun List<WalletData>.mapToTotalBalanceState(): List<TotalBalance.State> {
return this.map {
when (it.currencyData.status) {
BalanceStatus.VerifiedOnline,
@ -50,9 +34,9 @@ private fun List<WalletData>.mapToTotalBalanceState(): List<TotalBalance.State>
infix fun TotalBalance.State.or(newState: TotalBalance.State): TotalBalance.State {
return when (this) {
TotalBalance.State.Loading -> when (newState) {
TotalBalance.State.Loading -> this
TotalBalance.State.Loading,
TotalBalance.State.SomeTokensFailed,
TotalBalance.State.Success -> newState
TotalBalance.State.Success -> this
}
TotalBalance.State.Success,
TotalBalance.State.SomeTokensFailed -> when (newState) {

View file

@ -16,12 +16,24 @@ import com.tangem.tap.domain.extensions.getArtworkUrl
import com.tangem.tap.domain.getFirstToken
import com.tangem.tap.domain.tokens.BlockchainNetwork
import com.tangem.tap.features.wallet.models.WalletRent
import com.tangem.tap.features.wallet.redux.*
import com.tangem.tap.features.wallet.redux.AddressData
import com.tangem.tap.features.wallet.redux.Artwork
import com.tangem.tap.features.wallet.redux.Currency
import com.tangem.tap.features.wallet.redux.ErrorType
import com.tangem.tap.features.wallet.redux.ProgressState
import com.tangem.tap.features.wallet.redux.TradeCryptoState
import com.tangem.tap.features.wallet.redux.WalletAction
import com.tangem.tap.features.wallet.redux.WalletAddresses
import com.tangem.tap.features.wallet.redux.WalletData
import com.tangem.tap.features.wallet.redux.WalletDialog
import com.tangem.tap.features.wallet.redux.WalletMainButton
import com.tangem.tap.features.wallet.redux.WalletState
import com.tangem.tap.features.wallet.redux.WalletStore
import com.tangem.tap.features.wallet.ui.BalanceStatus
import com.tangem.tap.features.wallet.ui.BalanceWidgetData
import com.tangem.tap.store
import org.rekotlin.Action
import java.math.BigDecimal
import org.rekotlin.Action
class WalletReducer {
companion object {
@ -436,9 +448,6 @@ private fun setMultiWalletFiatRate(
return state
.updateWalletsData(newWalletsData)
.updateTotalBalance(
totalBalance = obtainTotalBalance(newWalletsData, appCurrency)
)
}
private fun setSingleWalletFiatRate(

View file

@ -2,6 +2,7 @@ package com.tangem.tap.features.wallet.ui.wallet
import android.app.Dialog
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.tangem.common.card.Card
import com.tangem.domain.common.TapWorkarounds.derivationStyle
@ -129,29 +130,28 @@ class MultiWalletView : WalletView {
binding: FragmentWalletBinding,
totalBalance: TotalBalance?,
) = with(binding.lCardTotalBalance) {
if (totalBalance == null) {
this.root.hide()
return@with
}
root.isVisible = totalBalance != null
if (totalBalance != null) {
tvBalance.animateVisibility(
show = totalBalance.state != TotalBalance.State.Loading,
hiddenVisibility = View.INVISIBLE
)
pbLoading.animateVisibility(
show = totalBalance.state == TotalBalance.State.Loading
)
tvProcessing.animateVisibility(
show = totalBalance.state == TotalBalance.State.SomeTokensFailed
)
tvBalance.animateVisibility(
show = totalBalance.state != TotalBalance.State.Loading,
hiddenVisibility = View.INVISIBLE
)
pbLoading.animateVisibility(
show = totalBalance.state == TotalBalance.State.Loading
)
tvProcessing.animateVisibility(
show = totalBalance.state == TotalBalance.State.SomeTokensFailed
)
tvBalance.text = if (totalBalance.state == TotalBalance.State.SomeTokensFailed) ""
else totalBalance.fiatAmount.formatAmountAsSpannedString(
currencySymbol = totalBalance.fiatCurrency.symbol
)
tvCurrencyName.text = totalBalance.fiatCurrency.code
tvBalance.text = totalBalance.fiatAmount.formatAmountAsSpannedString(
currencySymbol = totalBalance.fiatCurrency.symbol
)
tvCurrencyName.text = totalBalance.fiatCurrency.code
tvCurrencyName.setOnClickListener {
store.dispatch(WalletAction.AppCurrencyAction.ChooseAppCurrency)
tvCurrencyName.setOnClickListener {
store.dispatch(WalletAction.AppCurrencyAction.ChooseAppCurrency)
}
}
}