Updated on 2026-08-14

This commit is contained in:
Tangem 2024-10-31 15:55:14 +05:00
parent f76dcdd658
commit b2f3753b17
3 changed files with 44 additions and 40 deletions

View file

@ -12,7 +12,7 @@ open class BigDecimalFiatFormat(
val fiatCurrencySymbol: String,
val locale: Locale = Locale.getDefault(),
) : BigDecimalFormat {
override fun invoke(p1: BigDecimal): String = error("")
override fun invoke(value: BigDecimal): String = defaultAmount()(value)
}
// == Initializers ==

View file

@ -2,12 +2,13 @@ package com.tangem.features.staking.impl.presentation.state.converters
import com.tangem.common.extensions.isZero
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.*
import com.tangem.domain.tokens.model.CryptoCurrencyStatus
import com.tangem.features.staking.impl.presentation.state.InnerYieldBalanceState
import com.tangem.lib.crypto.BlockchainUtils.isBSC
import com.tangem.lib.crypto.BlockchainUtils.isSolana
import com.tangem.utils.Provider
import com.tangem.utils.converter.Converter
@ -44,11 +45,12 @@ internal class YieldBalancesConverter(
val (type, isActionable) = getRewardBlockType()
InnerYieldBalanceState.Data(
rewardsCrypto = cryptoRewardsValue.format { crypto(cryptoCurrency) },
rewardsFiat = BigDecimalFormatter.formatFiatAmount(
fiatAmount = fiatRewardsValue,
fiatCurrencyCode = appCurrency.code,
fiatCurrencySymbol = appCurrency.symbol,
),
rewardsFiat = fiatRewardsValue.format {
fiat(
fiatCurrencyCode = appCurrency.code,
fiatCurrencySymbol = appCurrency.symbol,
)
},
rewardBlockType = type,
isActionable = isActionable,
balance = balanceToShowItems.mapBalances(),
@ -67,6 +69,7 @@ internal class YieldBalancesConverter(
private fun getRewardBlockType(): Pair<RewardBlockType, Boolean> {
val cryptoCurrencyStatus = cryptoCurrencyStatusProvider()
val networkId = cryptoCurrencyStatus.currency.network.id.value
val yieldBalance = cryptoCurrencyStatus.value.yieldBalance as? YieldBalance.Data
val rewards = yieldBalance?.balance?.items
?.filter { it.type == BalanceType.REWARDS && !it.amount.isZero() }
@ -74,10 +77,8 @@ internal class YieldBalancesConverter(
val isActionable = rewards?.any { it.pendingActions.isNotEmpty() } == true
val isRewardsClaimable = rewards?.isNotEmpty() == true
val isSolana = isSolana(cryptoCurrencyStatus.currency.network.id.value)
return when {
isSolana -> RewardBlockType.RewardUnavailable to false
isSolana(networkId) || isBSC(networkId) -> RewardBlockType.RewardUnavailable to false
isRewardsClaimable -> RewardBlockType.Rewards to isActionable
else -> RewardBlockType.NoRewards to false
}

View file

@ -10,10 +10,7 @@ import com.tangem.core.ui.extensions.TextReference
import com.tangem.core.ui.extensions.resourceReference
import com.tangem.core.ui.extensions.stringReference
import com.tangem.core.ui.extensions.wrappedList
import com.tangem.core.ui.format.bigdecimal.crypto
import com.tangem.core.ui.format.bigdecimal.format
import com.tangem.core.ui.format.bigdecimal.percent
import com.tangem.core.ui.utils.BigDecimalFormatter
import com.tangem.core.ui.format.bigdecimal.*
import com.tangem.domain.appcurrency.model.AppCurrency
import com.tangem.domain.staking.model.StakingAvailability
import com.tangem.domain.staking.model.StakingEntryInfo
@ -29,8 +26,10 @@ import com.tangem.feature.tokendetails.presentation.tokendetails.state.utils.get
import com.tangem.feature.tokendetails.presentation.tokendetails.viewmodels.TokenDetailsClickIntents
import com.tangem.features.staking.api.featuretoggles.StakingFeatureToggles
import com.tangem.features.tokendetails.impl.R
import com.tangem.lib.crypto.BlockchainUtils.isBSC
import com.tangem.lib.crypto.BlockchainUtils.isSolana
import com.tangem.utils.Provider
import com.tangem.utils.StringsSigns.DASH_SIGN
import com.tangem.utils.converter.Converter
import com.tangem.utils.isNullOrZero
import kotlinx.collections.immutable.persistentListOf
@ -246,11 +245,12 @@ internal class TokenDetailsLoadedBalanceConverter(
stakingCryptoAmount.format { crypto(symbol = symbol, decimals = decimals) },
),
fiatValue = stringReference(
BigDecimalFormatter.formatFiatAmount(
stakingFiatAmount,
appCurrencyProvider().code,
appCurrencyProvider().symbol,
),
stakingFiatAmount.format {
fiat(
appCurrencyProvider().code,
appCurrencyProvider().symbol,
)
},
),
rewardValue = getRewardText(status, stakingRewardAmount),
onStakeClicked = clickIntents::onStakeBannerClick,
@ -273,19 +273,20 @@ internal class TokenDetailsLoadedBalanceConverter(
}
private fun formatPriceChange(status: CryptoCurrencyStatus.Value): String {
val priceChange = status.priceChange ?: return BigDecimalFormatter.EMPTY_BALANCE_SIGN
val priceChange = status.priceChange ?: return DASH_SIGN
return priceChange.format { percent() }
}
private fun formatPrice(status: CryptoCurrencyStatus.Value, appCurrency: AppCurrency): String {
val fiatRate = status.fiatRate ?: return BigDecimalFormatter.EMPTY_BALANCE_SIGN
val fiatRate = status.fiatRate ?: return DASH_SIGN
return BigDecimalFormatter.formatFiatAmountUncapped(
fiatAmount = fiatRate,
fiatCurrencyCode = appCurrency.code,
fiatCurrencySymbol = appCurrency.symbol,
)
return fiatRate.format {
fiat(
fiatCurrencyCode = appCurrency.code,
fiatCurrencySymbol = appCurrency.symbol,
).uncapped()
}
}
private fun formatFiatAmount(
@ -294,14 +295,15 @@ internal class TokenDetailsLoadedBalanceConverter(
selectedBalanceType: BalanceType,
appCurrency: AppCurrency,
): String {
val fiatAmount = status.fiatAmount ?: return BigDecimalFormatter.EMPTY_BALANCE_SIGN
val fiatAmount = status.fiatAmount ?: return DASH_SIGN
val totalAmount = fiatAmount.getBalance(selectedBalanceType, stakingFiatAmount)
return BigDecimalFormatter.formatFiatAmount(
fiatAmount = totalAmount,
fiatCurrencyCode = appCurrency.code,
fiatCurrencySymbol = appCurrency.symbol,
)
return totalAmount.format {
fiat(
fiatCurrencyCode = appCurrency.code,
fiatCurrencySymbol = appCurrency.symbol,
)
}
}
private fun formatCryptoAmount(
@ -309,16 +311,16 @@ internal class TokenDetailsLoadedBalanceConverter(
stakingCryptoAmount: BigDecimal?,
selectedBalanceType: BalanceType,
): String {
val amount = status.value.amount ?: return BigDecimalFormatter.EMPTY_BALANCE_SIGN
val amount = status.value.amount ?: return DASH_SIGN
val totalAmount = amount.getBalance(selectedBalanceType, stakingCryptoAmount)
return totalAmount.format { crypto(status.currency) }
}
private fun getRewardText(status: CryptoCurrencyStatus, stakingRewardAmount: BigDecimal?): TextReference {
val isSolana = isSolana(status.currency.network.id.value)
val networkId = status.currency.network.id.value
val rewardBlockType = when {
isSolana -> RewardBlockType.RewardUnavailable
isSolana(networkId) || isBSC(networkId) -> RewardBlockType.RewardUnavailable
stakingRewardAmount.isNullOrZero() -> RewardBlockType.NoRewards
else -> RewardBlockType.Rewards
}
@ -327,11 +329,12 @@ internal class TokenDetailsLoadedBalanceConverter(
RewardBlockType.Rewards -> resourceReference(
R.string.staking_details_rewards_to_claim,
wrappedList(
BigDecimalFormatter.formatFiatAmount(
stakingRewardAmount,
appCurrencyProvider().code,
appCurrencyProvider().symbol,
),
stakingRewardAmount.format {
fiat(
appCurrencyProvider().code,
appCurrencyProvider().symbol,
)
},
),
)
RewardBlockType.NoRewards -> resourceReference(R.string.staking_details_no_rewards_to_claim)