Updated on 2026-08-14

This commit is contained in:
Tangem 2025-02-25 17:17:19 +05:00
parent 6d7ae39ba2
commit a09aae7e41
23 changed files with 337 additions and 87 deletions

View file

@ -1,13 +1,14 @@
package com.tangem.feature.tokendetails.presentation.tokendetails.state.factory
import com.tangem.core.ui.format.bigdecimal.crypto
import com.tangem.core.ui.format.bigdecimal.fiat
import com.tangem.core.ui.format.bigdecimal.format
import com.tangem.core.ui.utils.BigDecimalFormatter
import com.tangem.domain.appcurrency.model.AppCurrency
import com.tangem.domain.staking.model.stakekit.YieldBalance
import com.tangem.domain.tokens.model.CryptoCurrencyStatus
import com.tangem.feature.tokendetails.presentation.tokendetails.state.*
import com.tangem.feature.tokendetails.presentation.tokendetails.state.utils.getBalance
import com.tangem.lib.crypto.BlockchainUtils
import com.tangem.utils.Provider
import com.tangem.utils.converter.Converter
import java.math.BigDecimal
@ -26,7 +27,8 @@ internal class TokenDetailsBalanceSelectStateConverter(
val yieldBalance = cryptoCurrencyStatus.value.yieldBalance as? YieldBalance.Data
val stakingCryptoAmount = yieldBalance?.getTotalWithRewardsStakingBalance()
val stakingFiatAmount = stakingCryptoAmount?.let { cryptoCurrencyStatus.value.fiatRate?.multiply(it) }
val includeStakingTotalBalance =
BlockchainUtils.isIncludeStakingTotalBalance(cryptoCurrencyStatus.currency.network.id.value)
copy(
tokenBalanceBlockState = if (tokenBalanceBlockState is TokenDetailsBalanceBlockState.Content) {
tokenBalanceBlockState.copy(
@ -36,11 +38,13 @@ internal class TokenDetailsBalanceSelectStateConverter(
stakingFiatAmount = stakingFiatAmount,
selectedBalanceType = value.type,
appCurrency = appCurrencyProvider(),
includeStaking = includeStakingTotalBalance,
),
displayCryptoBalance = formatCryptoAmount(
status = cryptoCurrencyStatus,
stakingCryptoAmount = stakingCryptoAmount,
selectedBalanceType = value.type,
includeStaking = includeStakingTotalBalance,
),
)
} else {
@ -55,25 +59,26 @@ internal class TokenDetailsBalanceSelectStateConverter(
stakingFiatAmount: BigDecimal?,
selectedBalanceType: BalanceType,
appCurrency: AppCurrency,
includeStaking: Boolean,
): String {
val fiatAmount = status.fiatAmount ?: return BigDecimalFormatter.EMPTY_BALANCE_SIGN
val totalAmount = fiatAmount.getBalance(selectedBalanceType, stakingFiatAmount)
val fiatAmount = status.fiatAmount?.getBalance(selectedBalanceType, stakingFiatAmount, includeStaking)
return BigDecimalFormatter.formatFiatAmount(
fiatAmount = totalAmount,
fiatCurrencyCode = appCurrency.code,
fiatCurrencySymbol = appCurrency.symbol,
)
return fiatAmount.format {
fiat(
fiatCurrencyCode = appCurrency.code,
fiatCurrencySymbol = appCurrency.symbol,
)
}
}
private fun formatCryptoAmount(
status: CryptoCurrencyStatus,
stakingCryptoAmount: BigDecimal?,
selectedBalanceType: BalanceType,
includeStaking: Boolean,
): String {
val amount = status.value.amount ?: return BigDecimalFormatter.EMPTY_BALANCE_SIGN
val totalAmount = amount.getBalance(selectedBalanceType, stakingCryptoAmount)
val amount = status.value.amount?.getBalance(selectedBalanceType, stakingCryptoAmount, includeStaking)
return totalAmount.format { crypto(status.currency) }
return amount.format { crypto(status.currency) }
}
}

View file

@ -19,13 +19,14 @@ import com.tangem.domain.staking.model.stakekit.RewardBlockType
import com.tangem.domain.staking.model.stakekit.YieldBalance
import com.tangem.domain.tokens.error.CurrencyStatusError
import com.tangem.domain.tokens.model.CryptoCurrencyStatus
import com.tangem.feature.tokendetails.presentation.tokendetails.model.TokenDetailsClickIntents
import com.tangem.feature.tokendetails.presentation.tokendetails.state.*
import com.tangem.feature.tokendetails.presentation.tokendetails.state.components.TokenDetailsNotification
import com.tangem.feature.tokendetails.presentation.tokendetails.state.factory.txhistory.TokenDetailsTxHistoryTransactionStateConverter
import com.tangem.feature.tokendetails.presentation.tokendetails.state.utils.getBalance
import com.tangem.feature.tokendetails.presentation.tokendetails.model.TokenDetailsClickIntents
import com.tangem.features.tokendetails.impl.R
import com.tangem.lib.crypto.BlockchainUtils.isBSC
import com.tangem.lib.crypto.BlockchainUtils.isIncludeStakingTotalBalance
import com.tangem.lib.crypto.BlockchainUtils.isSolana
import com.tangem.utils.Provider
import com.tangem.utils.StringsSigns.DASH_SIGN
@ -98,6 +99,7 @@ internal class TokenDetailsLoadedBalanceConverter(
val stakingCryptoAmount = (status.value.yieldBalance as? YieldBalance.Data)?.getTotalWithRewardsStakingBalance()
val stakingFiatAmount = stakingCryptoAmount?.let { status.value.fiatRate?.multiply(it) }
val isBalanceSelectorEnabled = !stakingCryptoAmount.isNullOrZero()
val includeStakingTotalBalance = isIncludeStakingTotalBalance(status.currency.network.id.value)
return when (status.value) {
is CryptoCurrencyStatus.NoQuote,
is CryptoCurrencyStatus.Loaded,
@ -112,11 +114,13 @@ internal class TokenDetailsLoadedBalanceConverter(
stakingFiatAmount,
currentState.selectedBalanceType,
appCurrencyProvider(),
includeStakingTotalBalance,
),
displayCryptoBalance = formatCryptoAmount(
status,
stakingCryptoAmount,
currentState.selectedBalanceType,
includeStakingTotalBalance,
),
balanceSegmentedButtonConfig = currentState.balanceSegmentedButtonConfig,
onBalanceSelect = clickIntents::onBalanceSelect,
@ -298,9 +302,10 @@ internal class TokenDetailsLoadedBalanceConverter(
stakingFiatAmount: BigDecimal?,
selectedBalanceType: BalanceType,
appCurrency: AppCurrency,
includeStaking: Boolean,
): String {
val fiatAmount = status.fiatAmount ?: return DASH_SIGN
val totalAmount = fiatAmount.getBalance(selectedBalanceType, stakingFiatAmount)
val totalAmount = fiatAmount.getBalance(selectedBalanceType, stakingFiatAmount, includeStaking)
return totalAmount.format {
fiat(
@ -314,9 +319,10 @@ internal class TokenDetailsLoadedBalanceConverter(
status: CryptoCurrencyStatus,
stakingCryptoAmount: BigDecimal?,
selectedBalanceType: BalanceType,
includeStaking: Boolean,
): String {
val amount = status.value.amount ?: return DASH_SIGN
val totalAmount = amount.getBalance(selectedBalanceType, stakingCryptoAmount)
val totalAmount = amount.getBalance(selectedBalanceType, stakingCryptoAmount, includeStaking)
return totalAmount.format { crypto(status.currency) }
}

View file

@ -3,8 +3,12 @@ package com.tangem.feature.tokendetails.presentation.tokendetails.state.utils
import com.tangem.feature.tokendetails.presentation.tokendetails.state.BalanceType
import java.math.BigDecimal
internal fun BigDecimal.getBalance(selectedBalanceType: BalanceType, stakingAmount: BigDecimal?): BigDecimal {
return if (selectedBalanceType == BalanceType.ALL && stakingAmount != null) {
internal fun BigDecimal.getBalance(
selectedBalanceType: BalanceType,
stakingAmount: BigDecimal?,
includeStaking: Boolean,
): BigDecimal {
return if (selectedBalanceType == BalanceType.ALL && stakingAmount != null && includeStaking) {
this.plus(stakingAmount)
} else {
this