Updated on 2026-08-14

This commit is contained in:
Tangem 2022-05-30 15:19:44 +04:00
commit 185fffe11c
27 changed files with 405 additions and 322 deletions

View file

@ -108,24 +108,22 @@ fun BigDecimal.isLessThanOrEqual(value: BigDecimal): Boolean {
fun BigDecimal.formatAmountAsSpannedString(
currencySymbol: String,
integerPartSizeProportion: Float = 1.4f
reminderPartSizeProportion: Float = 0.7f
): SpannedString {
val amount = this
.setScale(2, RoundingMode.HALF_UP)
.toString()
.formatWithSpaces()
val integer = amount.substringBefore('.')
val reminder = amount.substringAfter('.')
return buildSpannedString {
append(integer)
append('.')
append(
integer,
RelativeSizeSpan(integerPartSizeProportion),
"$reminder $currencySymbol",
RelativeSizeSpan(reminderPartSizeProportion),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
append('.')
append(reminder)
append(' ')
append(currencySymbol)
}
}

View file

@ -21,6 +21,14 @@ import com.tangem.tap.domain.tokens.models.BlockchainNetwork
import com.tangem.tap.features.onboarding.products.twins.redux.TwinCardsState
import com.tangem.tap.features.tokens.redux.TokenWithBlockchain
import com.tangem.tap.features.wallet.models.*
import com.tangem.tap.features.wallet.models.PendingTransaction
import com.tangem.tap.features.wallet.models.TotalBalance
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
@ -189,11 +197,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 {
@ -210,6 +218,7 @@ data class WalletState(
}
}
return copy(wallets = updatedWallets + walletStoresMutable)
.updateTotalBalance()
}
fun removeWallet(walletData: WalletData?): WalletState {
@ -271,11 +280,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.models.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.models.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.models.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

@ -24,7 +24,11 @@ import com.tangem.tap.domain.statePrinter.printScanResponseState
import com.tangem.tap.domain.statePrinter.printWalletState
import com.tangem.tap.domain.termsOfUse.CardTou
import com.tangem.tap.features.details.redux.DetailsAction
import com.tangem.tap.features.wallet.redux.*
import com.tangem.tap.features.wallet.redux.Artwork
import com.tangem.tap.features.wallet.redux.ErrorType
import com.tangem.tap.features.wallet.redux.ProgressState
import com.tangem.tap.features.wallet.redux.WalletAction
import com.tangem.tap.features.wallet.redux.WalletState
import com.tangem.tap.features.wallet.ui.adapters.WarningMessagesAdapter
import com.tangem.tap.features.wallet.ui.wallet.MultiWalletView
import com.tangem.tap.features.wallet.ui.wallet.SingleWalletView
@ -78,7 +82,7 @@ class WalletFragment : Fragment(R.layout.fragment_wallet), StoreSubscriber<Walle
(activity as? AppCompatActivity)?.setSupportActionBar(binding.toolbar)
binding.toolbar.setNavigationOnClickListener {
store.dispatch(NavigationAction.PopBackTo(AppScreen.Home))
store.dispatch(WalletAction.Scan)
}
setupWarningsRecyclerView()
walletView.changeWalletView(this, binding)

View file

@ -2,6 +2,7 @@ package com.tangem.tap.features.wallet.ui.adapters
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
@ -10,6 +11,7 @@ import com.tangem.blockchain.common.Blockchain
import com.tangem.blockchain.common.Token
import com.tangem.domain.common.TapWorkarounds.derivationStyle
import com.tangem.tap.common.extensions.getString
import com.tangem.tap.common.extensions.hide
import com.tangem.tap.common.extensions.loadCurrenciesIcon
import com.tangem.tap.common.extensions.show
import com.tangem.tap.features.wallet.redux.Currency
@ -61,57 +63,63 @@ class WalletAdapter
RecyclerView.ViewHolder(binding.root) {
fun bind(wallet: WalletData) = with(binding) {
tvCurrency.text = wallet.currencyData.currency
tvAmount.text = wallet.currencyData.amountFormatted.orEmpty()
tvAmountFiat.text = wallet.currencyData.fiatAmountFormatted
tvExchangeRate.text = wallet.fiatRateString
cardWallet.setOnClickListener {
store.dispatch(WalletAction.MultiWallet.SelectWallet(wallet))
val status = wallet.currencyData.status
val isCustomCurrency = wallet.currency.isCustomCurrency(
derivationStyle = store.state.globalState
.scanResponse
?.card
?.derivationStyle
)
val statusMessage = when (status) {
BalanceStatus.TransactionInProgress -> {
root.getString(R.string.wallet_balance_tx_in_progress)
}
BalanceStatus.Unreachable -> {
root.getString(R.string.wallet_balance_blockchain_unreachable)
}
BalanceStatus.NoAccount -> {
root.getString(R.string.wallet_error_no_account)
}
BalanceStatus.VerifiedOnline,
BalanceStatus.SameCurrencyTransactionInProgress,
BalanceStatus.EmptyCard,
BalanceStatus.UnknownBlockchain,
BalanceStatus.Loading,
null -> null
}
val blockchain = wallet.currency.blockchain
val token = (wallet.currency as? Currency.Token)?.token
val isCustom = wallet.currency
.isCustomCurrency(store.state.globalState.scanResponse?.card?.derivationStyle)
tvExchangeRate.show(!isCustom)
tvCustomCurrency.show(isCustom)
if (status == null || status == BalanceStatus.Loading) {
lContent.root.hide()
lShimmer.root.veil()
} else {
lShimmer.root.unVeil()
lContent.root.show()
}
Picasso.get().loadCurrenciesIcon(
imageView = ivCurrency,
textView = tvTokenLetter,
token = token, blockchain = blockchain,
token = (wallet.currency as? Currency.Token)?.token,
blockchain = wallet.currency.blockchain,
)
when (wallet.currencyData.status) {
BalanceStatus.VerifiedOnline,
BalanceStatus.SameCurrencyTransactionInProgress -> hideMessage()
BalanceStatus.Loading -> if (wallet.currencyData.amountFormatted == null) {
showMessage(root.getString(R.string.wallet_balance_loading))
}
BalanceStatus.TransactionInProgress ->
showMessage(root.getString(R.string.wallet_balance_tx_in_progress))
BalanceStatus.Unreachable ->
showMessage(root.getString(R.string.wallet_balance_blockchain_unreachable))
lContent.tvCurrency.text = wallet.currencyData.currency
lContent.tvAmountFiat.text = wallet.currencyData.fiatAmountFormatted ?: ""
lContent.tvAmount.text = wallet.currencyData.amountFormatted ?: ""
BalanceStatus.NoAccount ->
showMessage(root.getString(R.string.wallet_error_no_account))
else -> {
}
lContent.tvStatus.isVisible = statusMessage != null
lContent.tvStatus.text = statusMessage
lContent.tvExchangeRate.isVisible = statusMessage == null
lContent.tvExchangeRate.text = if (isCustomCurrency) {
root.getString(id = R.string.token_item_no_rate)
} else {
wallet.fiatRateString
}
}
private fun showMessage(message: String) {
toggleMessage(true)
binding.tvStatus.text = message
}
private fun hideMessage() {
toggleMessage(false)
}
private fun toggleMessage(show: Boolean) {
binding.tvAmount.show(!show)
binding.tvStatus.show(show)
cardWallet.setOnClickListener {
store.dispatch(WalletAction.MultiWallet.SelectWallet(wallet))
}
}
}
}

View file

@ -1,7 +1,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
@ -41,7 +41,6 @@ class MultiWalletView : WalletView {
setFragment(fragment, binding)
onViewCreated()
showMultiWalletView(binding)
setupButtons(binding)
}
@ -52,7 +51,6 @@ class MultiWalletView : WalletView {
lAddress.root.hide()
lButtonsShort.root.hide()
lButtonsLong.root.hide()
btnScanMultiwallet.show()
rvMultiwallet.show()
btnAddToken.show()
setupWalletCardNumber(binding)
@ -70,11 +68,6 @@ class MultiWalletView : WalletView {
}
}
private fun setupButtons(binding: FragmentWalletBinding) = with(binding) {
btnScanMultiwallet.setOnClickListener { store.dispatch(WalletAction.Scan) }
}
override fun setFragment(fragment: WalletFragment, binding: FragmentWalletBinding) {
this.fragment = fragment
this.binding = binding
@ -136,29 +129,26 @@ 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) {
if (totalBalance.state == TotalBalance.State.Loading) {
veilBalance.veil()
} else {
veilBalance.unVeil()
}
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)
}
}
}
@ -204,7 +194,6 @@ class MultiWalletView : WalletView {
private fun configureButtonsForEmptyWalletState(binding: FragmentWalletBinding) =
with(binding) {
btnScanMultiwallet.hide()
lButtonsLong.root.show()
lButtonsLong.btnScanLong.setOnClickListener { store.dispatch(WalletAction.Scan) }
lButtonsLong.btnConfirmLong.setOnClickListener { store.dispatch(WalletAction.CreateWallet) }

View file

@ -13,7 +13,13 @@ import com.tangem.tap.common.extensions.show
import com.tangem.tap.common.redux.StateDialog
import com.tangem.tap.features.onboarding.products.twins.redux.TwinCardsState
import com.tangem.tap.features.wallet.models.PendingTransaction
import com.tangem.tap.features.wallet.redux.*
import com.tangem.tap.features.wallet.redux.Currency
import com.tangem.tap.features.wallet.redux.TradeCryptoState
import com.tangem.tap.features.wallet.redux.WalletAction
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.ui.BalanceWidget
import com.tangem.tap.features.wallet.ui.MultipleAddressUiHelper
import com.tangem.tap.features.wallet.ui.WalletFragment
@ -51,7 +57,6 @@ class SingleWalletView : WalletView {
private fun showSingleWalletView(binding: FragmentWalletBinding) = with(binding) {
rvMultiwallet.hide()
btnAddToken.hide()
btnScanMultiwallet.hide()
rvPendingTransaction.hide()
lCardBalance.root.show()
lAddress.root.show()