From 557aae44acc03675dfdf512d7196ee556324e178 Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 17 Sep 2024 14:45:37 +0500 Subject: [PATCH] Updated on 2026-08-14 --- .../tangem/core/ui/utils/DecimalFormatterExt.kt | 2 +- .../operations/TokenListSortingOperations.kt | 14 ++++++++++++-- .../factory/TokenDetailsLoadedBalanceConverter.kt | 5 +++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/core/ui/src/main/java/com/tangem/core/ui/utils/DecimalFormatterExt.kt b/core/ui/src/main/java/com/tangem/core/ui/utils/DecimalFormatterExt.kt index 2d685763e3..a251b0c9ed 100644 --- a/core/ui/src/main/java/com/tangem/core/ui/utils/DecimalFormatterExt.kt +++ b/core/ui/src/main/java/com/tangem/core/ui/utils/DecimalFormatterExt.kt @@ -138,7 +138,7 @@ fun String.parseToBigDecimal(decimals: Int): BigDecimal { } } -fun BigDecimal.parseBigDecimal(decimals: Int, roundingMode: RoundingMode = RoundingMode.DOWN): String { +fun BigDecimal.parseBigDecimal(decimals: Int, roundingMode: RoundingMode = RoundingMode.HALF_UP): String { val decimalFormat = DecimalFormat().apply { decimalFormatSymbols = DecimalFormatSymbols(Locale.getDefault()) isParseBigDecimal = true diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListSortingOperations.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListSortingOperations.kt index 354252452c..7e4bc12ae2 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListSortingOperations.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListSortingOperations.kt @@ -7,7 +7,9 @@ import arrow.core.raise.either import arrow.core.raise.ensure import arrow.core.raise.ensureNotNull import arrow.core.toNonEmptyListOrNull +import com.tangem.domain.staking.model.stakekit.YieldBalance import com.tangem.domain.tokens.model.* +import com.tangem.utils.extensions.orZero import java.math.BigDecimal internal class TokenListSortingOperations( @@ -83,7 +85,7 @@ internal class TokenListSortingOperations( return if (isAnyTokenLoading) { tokens } else { - tokens.sortedByDescending { it.value.fiatAmount ?: BigDecimal.ZERO } + tokens.sortedByDescending { it.getTotalBalance() } .toNonEmptyListOrNull() ?: error("Tokens can not be empty here") } @@ -92,12 +94,20 @@ internal class TokenListSortingOperations( private fun sortGroupsByBalance(groupsWithSortedTokens: NonEmptyList): NonEmptyList { return groupsWithSortedTokens .sortedByDescending { group -> - group.currencies.sumOf { it.value.fiatAmount ?: BigDecimal.ZERO } + group.currencies.sumOf { it.getTotalBalance() } } .toNonEmptyListOrNull() ?: error("Tokens can not be empty here") } + private fun CryptoCurrencyStatus.getTotalBalance(): BigDecimal { + val yieldBalance = value.yieldBalance as? YieldBalance.Data + val totalYieldBalance = yieldBalance?.getTotalWithRewardsStakingBalance().orZero() + val totalFiatYieldBalance = totalYieldBalance.multiply(value.fiatRate.orZero()) + + return value.fiatAmount?.plus(totalFiatYieldBalance).orZero() + } + sealed class Error { object EmptyTokens : Error() diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsLoadedBalanceConverter.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsLoadedBalanceConverter.kt index 2419e894d1..190a186493 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsLoadedBalanceConverter.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsLoadedBalanceConverter.kt @@ -153,8 +153,9 @@ internal class TokenDetailsLoadedBalanceConverter( StakingBlockUM.Error(iconState = iconState) } else -> { - val stakingRewardAmount = yieldBalance?.getRewardStakingBalance() - val stakingFiatAmount = stakingCryptoAmount?.let { status.value.fiatRate?.multiply(it) } + val fiatRate = status.value.fiatRate + val stakingRewardAmount = yieldBalance?.getRewardStakingBalance()?.let { fiatRate?.multiply(it) } + val stakingFiatAmount = stakingCryptoAmount?.let { fiatRate?.multiply(it) } getStakedState( status = status,