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 c815b4b404..f5b14fe589 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 @@ -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): 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 ) } } diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt index a7437c94eb..f0c7973628 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt @@ -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 ), diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/OnWalletLoadedReducer.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/OnWalletLoadedReducer.kt index ce9b5cf80a..c44f18e21a 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/OnWalletLoadedReducer.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/OnWalletLoadedReducer.kt @@ -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 diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/TotalBalanceOperations.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/TotalBalanceOperations.kt index b949fe11d9..06d91d7db0 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/TotalBalanceOperations.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/TotalBalanceOperations.kt @@ -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, - appCurrency: FiatCurrency -): TotalBalance? { - return if (wallets.isNotEmpty()) { - TotalBalance( - state = wallets.findTotalBalanceState(), - fiatAmount = wallets.calculateTotalFiatAmount(), - fiatCurrency = appCurrency - ) - } else null -} - -private fun List.findTotalBalanceState(): TotalBalance.State { +fun List.findTotalBalanceState(): TotalBalance.State { return this.mapToTotalBalanceState() - .fold(initial = TotalBalance.State.Loading) { accState, newState -> - accState or newState - } + .reduce(TotalBalance.State::or) } -private fun List.calculateTotalFiatAmount(): BigDecimal { +fun List.calculateTotalFiatAmount(): BigDecimal { return this.map { it.currencyData.fiatAmount ?: BigDecimal.ZERO } .reduce(BigDecimal::plus) } -private fun List.mapToTotalBalanceState(): List { +fun List.mapToTotalBalanceState(): List { return this.map { when (it.currencyData.status) { BalanceStatus.VerifiedOnline, @@ -50,9 +34,9 @@ private fun List.mapToTotalBalanceState(): List 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) { diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt index e4126a3f8c..65e89b0d83 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt @@ -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( diff --git a/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/MultiWalletView.kt b/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/MultiWalletView.kt index 15eb7aedce..66b4a9e664 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/MultiWalletView.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/ui/wallet/MultiWalletView.kt @@ -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) + } } }