From 7ca9946f476d616b430e12cd54106e6557356175 Mon Sep 17 00:00:00 2001 From: Tangem Date: Fri, 18 Oct 2024 12:20:15 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../converters/AmountStateConverter.kt | 7 +- .../common/ui/amountScreen/ui/AmountBlock.kt | 11 +- .../common/ui/amountScreen/ui/AmountField.kt | 8 +- .../common/ui/notifications/NotificationUM.kt | 16 +- .../ui/notifications/NotificationsFactory.kt | 26 +-- .../ui/tokens/TokenItemStateConverter.kt | 10 +- .../inputrow/InputRowImageSelector.kt | 5 +- .../bigdecimal/BigDecimalCryptoFormat.kt | 33 +++- .../bigdecimal/BigDecimalSimpleFormat.kt | 34 ++++ .../core/ui/utils/BigDecimalFormatter.kt | 159 ------------------ .../bigdecimal/BigDecimalCryptoFormatTest.kt | 34 ++++ .../utils/CryptoCurrencyFormatExtensions.kt | 43 ----- .../usecase/SendTransactionUseCase.kt | 5 +- .../impl/model/MarketsTokenDetailsModel.kt | 16 +- .../impl/model/formatter/Formatters.kt | 9 +- .../block/impl/model/TokenMarketBlockModel.kt | 7 +- .../converters/MarketsTokenItemConverter.kt | 6 +- .../SendRecipientHistoryListConverter.kt | 8 +- .../presentation/ui/SendNavigationButtons.kt | 17 +- .../ui/fee/SendSpeedSelectorItem.kt | 15 +- .../impl/presentation/ui/send/FeeBlock.kt | 15 +- .../state/converters/BalanceItemConverter.kt | 7 +- .../RewardsValidatorStateConverter.kt | 9 +- .../converters/YieldBalancesConverter.kt | 7 +- .../SetInitialDataStateTransformer.kt | 26 +-- .../AmountRequirementStateTransformer.kt | 11 +- .../ShowApprovalBottomSheetTransformer.kt | 10 +- .../ui/StakingClaimRewardsValidatorContent.kt | 8 +- .../ui/StakingInitialInfoContent.kt | 8 +- .../ui/StakingValidatorListContent.kt | 5 +- .../presentation/ui/block/StakingFeeBlock.kt | 15 +- .../presentation/ui/block/ValidatorBlock.kt | 5 +- .../feature/swap/domain/models/SwapAmount.kt | 5 - .../swap/domain/models/ui/AmountFormatter.kt | 11 +- .../feature/swap/domain/SwapInteractorImpl.kt | 2 +- .../swap/converters/TokensDataConverter.kt | 10 +- .../feature/swap/di/SwapSingletonModule.kt | 18 ++ .../swap/di/impl/DefaultAmountFormatter.kt | 18 ++ .../tangem/feature/swap/ui/StateBuilder.kt | 24 ++- .../tangem/feature/swap/utils/SwapUtils.kt | 7 + .../feature/swap/viewmodels/SwapViewModel.kt | 2 +- ...TokenDetailsBalanceSelectStateConverter.kt | 4 +- .../TokenDetailsLoadedBalanceConverter.kt | 17 +- .../TokenDetailsNotificationConverter.kt | 24 +-- ...enDetailsSwapTransactionsStateConverter.kt | 12 +- ...tailsTxHistoryTransactionStateConverter.kt | 5 +- .../domain/WalletAdditionalInfoFactory.kt | 11 +- .../SetBalancesAndLimitsTransformer.kt | 18 +- .../BalancesAndLimitsBottomSheetConverter.kt | 9 +- .../SingleWalletMarketPriceConverter.kt | 4 +- .../converter/TxHistoryItemStateConverter.kt | 5 +- .../VisaTxDetailsBottomSheetConverter.kt | 8 +- .../VisaTxHistoryItemStateConverter.kt | 8 +- 53 files changed, 355 insertions(+), 462 deletions(-) create mode 100644 core/ui/src/main/java/com/tangem/core/ui/format/bigdecimal/BigDecimalSimpleFormat.kt delete mode 100644 core/utils/src/main/java/com/tangem/utils/CryptoCurrencyFormatExtensions.kt create mode 100644 features/swap/presentation/src/main/java/com/tangem/feature/swap/di/SwapSingletonModule.kt create mode 100644 features/swap/presentation/src/main/java/com/tangem/feature/swap/di/impl/DefaultAmountFormatter.kt diff --git a/common/ui/src/main/java/com/tangem/common/ui/amountScreen/converters/AmountStateConverter.kt b/common/ui/src/main/java/com/tangem/common/ui/amountScreen/converters/AmountStateConverter.kt index 410a23b468..3445c4859f 100644 --- a/common/ui/src/main/java/com/tangem/common/ui/amountScreen/converters/AmountStateConverter.kt +++ b/common/ui/src/main/java/com/tangem/common/ui/amountScreen/converters/AmountStateConverter.kt @@ -3,13 +3,14 @@ package com.tangem.common.ui.amountScreen.converters import com.tangem.common.ui.R import com.tangem.common.ui.amountScreen.AmountScreenClickIntents import com.tangem.common.ui.amountScreen.converters.field.AmountFieldConverter -import com.tangem.common.ui.amountScreen.models.AmountState import com.tangem.common.ui.amountScreen.models.AmountSegmentedButtonsConfig +import com.tangem.common.ui.amountScreen.models.AmountState import com.tangem.core.ui.components.currency.icon.converter.CryptoCurrencyToIconStateConverter 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.utils.BigDecimalFormatter.formatCryptoAmount +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.utils.BigDecimalFormatter.formatFiatAmount import com.tangem.domain.appcurrency.model.AppCurrency import com.tangem.domain.tokens.model.CryptoCurrencyStatus @@ -49,7 +50,7 @@ class AmountStateConverter( val appCurrency = appCurrencyProvider() val status = cryptoCurrencyStatusProvider() val fiat = formatFiatAmount(status.value.fiatAmount, appCurrency.code, appCurrency.symbol) - val crypto = formatCryptoAmount(status.value.amount, status.currency.symbol, status.currency.decimals) + val crypto = status.value.amount.format { crypto(status.currency) } val noFeeRate = status.value.fiatRate.isNullOrZero() return AmountState.Data( diff --git a/common/ui/src/main/java/com/tangem/common/ui/amountScreen/ui/AmountBlock.kt b/common/ui/src/main/java/com/tangem/common/ui/amountScreen/ui/AmountBlock.kt index b2903b20cc..bcac8bb21b 100644 --- a/common/ui/src/main/java/com/tangem/common/ui/amountScreen/ui/AmountBlock.kt +++ b/common/ui/src/main/java/com/tangem/common/ui/amountScreen/ui/AmountBlock.kt @@ -19,16 +19,20 @@ import com.tangem.common.ui.amountScreen.models.AmountState import com.tangem.common.ui.amountScreen.preview.AmountStatePreviewData import com.tangem.core.ui.components.ResizableText import com.tangem.core.ui.components.currency.icon.CurrencyIcon -import com.tangem.core.ui.res.TangemThemePreview +import com.tangem.core.ui.format.bigdecimal.anyDecimals +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.res.TangemTheme +import com.tangem.core.ui.res.TangemThemePreview import com.tangem.core.ui.utils.BigDecimalFormatter +import java.math.BigDecimal @Composable fun AmountBlock(amountState: AmountState, isClickDisabled: Boolean, isEditingDisabled: Boolean, onClick: () -> Unit) { if (amountState !is AmountState.Data) return val amount = amountState.amountTextField - val cryptoAmount = BigDecimalFormatter.formatWithSymbol(amount.value, amount.cryptoAmount.currencySymbol) + val cryptoAmount = formatWithSymbol(amount.value, amount.cryptoAmount.currencySymbol) val fiatAmount = BigDecimalFormatter.formatFiatAmount( fiatAmount = amount.fiatAmount.value, fiatCurrencySymbol = amount.fiatAmount.currencySymbol, @@ -77,6 +81,9 @@ fun AmountBlock(amountState: AmountState, isClickDisabled: Boolean, isEditingDis } } +fun formatWithSymbol(amount: String, symbol: String) = + BigDecimal.ZERO.format { crypto(symbol, 0).anyDecimals() }.replace("0", amount) + // region Preview @Preview @Preview(uiMode = Configuration.UI_MODE_NIGHT_YES) diff --git a/common/ui/src/main/java/com/tangem/common/ui/amountScreen/ui/AmountField.kt b/common/ui/src/main/java/com/tangem/common/ui/amountScreen/ui/AmountField.kt index d8347d67af..c74f183aa6 100644 --- a/common/ui/src/main/java/com/tangem/common/ui/amountScreen/ui/AmountField.kt +++ b/common/ui/src/main/java/com/tangem/common/ui/amountScreen/ui/AmountField.kt @@ -24,6 +24,8 @@ import com.tangem.core.ui.components.fields.AmountTextField import com.tangem.core.ui.components.fields.visualtransformations.AmountVisualTransformation import com.tangem.core.ui.extensions.TextReference import com.tangem.core.ui.extensions.resolveReference +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.res.TangemTheme import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.core.ui.utils.rememberDecimalFormat @@ -92,11 +94,7 @@ private fun AmountSecondary(amountField: AmountFieldModel, appCurrencyCode: Stri ), ) { val text = if (amountField.isFiatValue) { - BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = secondaryAmount.value, - cryptoCurrency = secondaryAmount.currencySymbol, - decimals = secondaryAmount.decimals, - ) + secondaryAmount.value.format { crypto(secondaryAmount.currencySymbol, secondaryAmount.decimals) } } else { BigDecimalFormatter.formatFiatAmount( fiatAmount = secondaryAmount.value, diff --git a/common/ui/src/main/java/com/tangem/common/ui/notifications/NotificationUM.kt b/common/ui/src/main/java/com/tangem/common/ui/notifications/NotificationUM.kt index accc615136..8ca24539b5 100644 --- a/common/ui/src/main/java/com/tangem/common/ui/notifications/NotificationUM.kt +++ b/common/ui/src/main/java/com/tangem/common/ui/notifications/NotificationUM.kt @@ -6,7 +6,9 @@ import com.tangem.core.ui.components.notifications.NotificationConfig import com.tangem.core.ui.extensions.TextReference import com.tangem.core.ui.extensions.resourceReference import com.tangem.core.ui.extensions.wrappedList -import com.tangem.core.ui.utils.BigDecimalFormatter +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format +import com.tangem.core.ui.format.bigdecimal.shorted import java.math.BigDecimal sealed class NotificationUM(val config: NotificationConfig) { @@ -267,8 +269,8 @@ sealed class NotificationUM(val config: NotificationConfig) { subtitle = resourceReference( R.string.koinos_insufficient_mana_to_send_koin_description, formatArgs = wrappedList( - BigDecimalFormatter.formatCryptoAmountShorted(mana, "", Blockchain.Koinos.decimals()), - BigDecimalFormatter.formatCryptoAmountShorted(maxMana, "", Blockchain.Koinos.decimals()), + mana.format { crypto("", Blockchain.Koinos.decimals()).shorted() }, + maxMana.format { crypto("", Blockchain.Koinos.decimals()).shorted() }, ), ), ) @@ -286,11 +288,9 @@ sealed class NotificationUM(val config: NotificationConfig) { subtitle = resourceReference( R.string.koinos_mana_exceeds_koin_balance_description, formatArgs = wrappedList( - BigDecimalFormatter.formatCryptoAmount( - availableKoinForTransfer, - Blockchain.Koinos.currency, - Blockchain.Koinos.decimals(), - ), + availableKoinForTransfer.format { + crypto(Blockchain.Koinos.currency, Blockchain.Koinos.decimals()) + }, ), ), buttonState = NotificationConfig.ButtonsState.PrimaryButtonConfig( diff --git a/common/ui/src/main/java/com/tangem/common/ui/notifications/NotificationsFactory.kt b/common/ui/src/main/java/com/tangem/common/ui/notifications/NotificationsFactory.kt index 36c15ab3c6..51df42310b 100644 --- a/common/ui/src/main/java/com/tangem/common/ui/notifications/NotificationsFactory.kt +++ b/common/ui/src/main/java/com/tangem/common/ui/notifications/NotificationsFactory.kt @@ -6,7 +6,9 @@ import com.tangem.common.ui.R import com.tangem.common.ui.amountScreen.models.AmountFieldModel import com.tangem.common.ui.amountScreen.utils.getFiatString import com.tangem.core.ui.extensions.networkIconResId -import com.tangem.core.ui.utils.BigDecimalFormatter +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format +import com.tangem.core.ui.format.bigdecimal.uncapped import com.tangem.core.ui.utils.parseBigDecimal import com.tangem.domain.appcurrency.model.AppCurrency import com.tangem.domain.tokens.model.CryptoCurrency @@ -65,10 +67,9 @@ object NotificationsFactory { if (!isAccountFunded && reserveAmount != null && reserveAmount > sendingAmount) { add( NotificationUM.Error.ReserveAmount( - BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = sendingAmount, - cryptoCurrency = cryptoCurrency, - ), + sendingAmount.format { + crypto(cryptoCurrency) + }, ), ) } @@ -87,10 +88,7 @@ object NotificationsFactory { NotificationUM.Error.TransactionLimitError( cryptoCurrency = cryptoCurrency.name, utxoLimit = utxoLimit.maxLimit.toPlainString(), - amountLimit = BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = utxoLimit.maxAmount, - cryptoCurrency = cryptoCurrency, - ), + amountLimit = utxoLimit.maxAmount.format { crypto(cryptoCurrency) }, onConfirmClick = { onReduceClick( utxoLimit.maxAmount, @@ -124,10 +122,7 @@ object NotificationsFactory { if (existentialDeposit != null && diff >= BigDecimal.ZERO && existentialDeposit > diff) { add( NotificationUM.Error.ExistentialDeposit( - deposit = BigDecimalFormatter.formatCryptoAmountUncapped( - cryptoAmount = existentialDeposit, - cryptoCurrency = cryptoCurrency, - ), + deposit = existentialDeposit.format { crypto(cryptoCurrency).uncapped() }, onConfirmClick = { onReduceClick( existentialDeposit, @@ -155,10 +150,7 @@ object NotificationsFactory { if (isFeeCoverage) { add( NotificationUM.Warning.FeeCoverageNotification( - cryptoAmount = BigDecimalFormatter.formatCryptoAmountUncapped( - cryptoAmount = cryptoDiff, - cryptoCurrency = cryptoCurrency, - ), + cryptoAmount = cryptoDiff.format { crypto(cryptoCurrency).uncapped() }, fiatAmount = getFiatString( value = cryptoDiff, rate = fiatRate, diff --git a/common/ui/src/main/java/com/tangem/common/ui/tokens/TokenItemStateConverter.kt b/common/ui/src/main/java/com/tangem/common/ui/tokens/TokenItemStateConverter.kt index 964b1a0929..913b07dd84 100644 --- a/common/ui/src/main/java/com/tangem/common/ui/tokens/TokenItemStateConverter.kt +++ b/common/ui/src/main/java/com/tangem/common/ui/tokens/TokenItemStateConverter.kt @@ -4,6 +4,9 @@ import com.tangem.core.ui.components.currency.icon.converter.CryptoCurrencyToIco import com.tangem.core.ui.components.marketprice.PriceChangeType import com.tangem.core.ui.components.marketprice.utils.PriceChangeConverter import com.tangem.core.ui.components.token.state.TokenItemState +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.domain.appcurrency.model.AppCurrency import com.tangem.domain.staking.model.stakekit.YieldBalance @@ -80,7 +83,7 @@ class TokenItemStateConverter( private fun CryptoCurrencyStatus.getFormattedAmount(): String { val amount = value.amount?.plus(getStakedBalance()) ?: return DASH_SIGN - return BigDecimalFormatter.formatCryptoAmount(amount, currency.symbol, currency.decimals) + return amount.format { crypto(currency) } } private fun CryptoCurrencyStatus.getFormattedFiatAmount(): String { @@ -167,10 +170,7 @@ class TokenItemStateConverter( return if (fiatRate != null && priceChange != null) { TokenItemState.SubtitleState.CryptoPriceContent( price = fiatRate.getFormattedCryptoPrice(appCurrency), - priceChangePercent = BigDecimalFormatter.formatPercent( - percent = priceChange, - useAbsoluteValue = true, - ), + priceChangePercent = priceChange.format { percent() }, type = priceChange.getPriceChangeType(), ) } else { diff --git a/core/ui/src/main/java/com/tangem/core/ui/components/inputrow/InputRowImageSelector.kt b/core/ui/src/main/java/com/tangem/core/ui/components/inputrow/InputRowImageSelector.kt index a9921a83b5..e94c8d5191 100644 --- a/core/ui/src/main/java/com/tangem/core/ui/components/inputrow/InputRowImageSelector.kt +++ b/core/ui/src/main/java/com/tangem/core/ui/components/inputrow/InputRowImageSelector.kt @@ -24,9 +24,10 @@ import com.tangem.core.ui.extensions.TextReference import com.tangem.core.ui.extensions.annotatedReference import com.tangem.core.ui.extensions.combinedReference import com.tangem.core.ui.extensions.resourceReference +import com.tangem.core.ui.format.bigdecimal.format +import com.tangem.core.ui.format.bigdecimal.percent import com.tangem.core.ui.res.TangemTheme import com.tangem.core.ui.res.TangemThemePreview -import com.tangem.core.ui.utils.BigDecimalFormatter import java.math.BigDecimal /** @@ -98,7 +99,7 @@ private fun InputRowImageSelectorPreview( append(" ") withStyle(style = SpanStyle(color = TangemTheme.colors.text.accent)) { append( - BigDecimalFormatter.formatPercent(BigDecimal.ZERO, true), + BigDecimal.ZERO.format { percent() }, ) } }, diff --git a/core/ui/src/main/java/com/tangem/core/ui/format/bigdecimal/BigDecimalCryptoFormat.kt b/core/ui/src/main/java/com/tangem/core/ui/format/bigdecimal/BigDecimalCryptoFormat.kt index 6973261be0..eea3130209 100644 --- a/core/ui/src/main/java/com/tangem/core/ui/format/bigdecimal/BigDecimalCryptoFormat.kt +++ b/core/ui/src/main/java/com/tangem/core/ui/format/bigdecimal/BigDecimalCryptoFormat.kt @@ -120,6 +120,26 @@ fun BigDecimalCryptoFormat.uncapped() = BigDecimalFormat { value -> ) } +/** + * Format for displaying crypto amounts with a fixed number of decimals. + */ +fun BigDecimalCryptoFormat.anyDecimals(maxDecimals: Int = decimals, minDecimals: Int = decimals) = + BigDecimalFormat { value -> + val formatter = NumberFormat.getCurrencyInstance(locale).apply { + currency = usdCurrency + maximumFractionDigits = maxDecimals + minimumFractionDigits = minDecimals + isGroupingUsed = true + roundingMode = RoundingMode.HALF_UP + } + + formatter.format(value) + .replaceFiatSymbolWithCrypto( + fiatCurrencySymbol = usdCurrency.getSymbol(locale), + cryptoCurrencySymbol = symbol, + ) + } + /** * Format for displaying fees. * If the fee is less than the threshold, it will be displayed as a fixed value "<0.000001 BTC", " { val withoutSymbol = str.dropLast(fiatCurrencySymbol.length) + + if (cryptoCurrencySymbol.isBlank()) { + return withoutSymbol + } + val last = withoutSymbol.lastOrNull() ?: return cryptoCurrencySymbol append(withoutSymbol) @@ -200,12 +225,16 @@ internal fun String.replaceFiatSymbolWithCrypto( append(CURRENCY_SPACE) } - append(cryptoCurrencySymbol) - val withoutSymbol = str.drop(fiatCurrencySymbol.length) val first = withoutSymbol.firstOrNull() ?: return cryptoCurrencySymbol + if (cryptoCurrencySymbol.isBlank()) { + return withoutSymbol + } + + append(cryptoCurrencySymbol) + if (first.isNotWhitespace()) { append(CURRENCY_SPACE) } diff --git a/core/ui/src/main/java/com/tangem/core/ui/format/bigdecimal/BigDecimalSimpleFormat.kt b/core/ui/src/main/java/com/tangem/core/ui/format/bigdecimal/BigDecimalSimpleFormat.kt new file mode 100644 index 0000000000..f6c5067d65 --- /dev/null +++ b/core/ui/src/main/java/com/tangem/core/ui/format/bigdecimal/BigDecimalSimpleFormat.kt @@ -0,0 +1,34 @@ +package com.tangem.core.ui.format.bigdecimal + +import java.math.BigDecimal +import java.math.RoundingMode +import java.text.NumberFormat +import java.util.Locale + +open class BigDecimalSimpleFormat( + val decimals: Int, + val locale: Locale = Locale.getDefault(), +) : BigDecimalFormat { + + override fun invoke(value: BigDecimal): String = default()(value) +} + +// == Initializers == + +fun BigDecimalFormatScope.simple(decimals: Int, locale: Locale = Locale.getDefault()) = BigDecimalSimpleFormat( + decimals = decimals, + locale = locale, +) + +// == Formatters == + +fun BigDecimalSimpleFormat.default() = BigDecimalFormat { value -> + val formatter = NumberFormat.getInstance(locale).apply { + maximumFractionDigits = decimals + minimumFractionDigits = 0 + isGroupingUsed = true + roundingMode = RoundingMode.HALF_UP + } + + formatter.format(value) +} \ No newline at end of file diff --git a/core/ui/src/main/java/com/tangem/core/ui/utils/BigDecimalFormatter.kt b/core/ui/src/main/java/com/tangem/core/ui/utils/BigDecimalFormatter.kt index fd6a353bb1..744a2d9bf3 100644 --- a/core/ui/src/main/java/com/tangem/core/ui/utils/BigDecimalFormatter.kt +++ b/core/ui/src/main/java/com/tangem/core/ui/utils/BigDecimalFormatter.kt @@ -1,6 +1,5 @@ package com.tangem.core.ui.utils -import com.tangem.domain.tokens.model.CryptoCurrency import com.tangem.utils.StringsSigns.DASH_SIGN import com.tangem.utils.StringsSigns.LOWER_SIGN import com.tangem.utils.StringsSigns.TILDE_SIGN @@ -16,10 +15,8 @@ object BigDecimalFormatter { const val EMPTY_BALANCE_SIGN = DASH_SIGN private const val CAN_BE_LOWER_SIGN = LOWER_SIGN - private val FORMAT_THRESHOLD = BigDecimal("0.01") private val FIAT_FORMAT_THRESHOLD = BigDecimal("0.01") - private val CRYPTO_FEE_FORMAT_THRESHOLD = BigDecimal("0.000001") private const val FIAT_MARKET_DEFAULT_DIGITS = 2 private const val FIAT_MARKET_EXTENDED_DIGITS = 6 @@ -27,138 +24,6 @@ object BigDecimalFormatter { private val usdCurrency = Currency.getInstance("USD") - @Deprecated("Use BigDecimal.format") - fun formatCryptoAmount( - cryptoAmount: BigDecimal?, - cryptoCurrency: String, - decimals: Int, - locale: Locale = Locale.getDefault(), - ): String { - if (cryptoAmount == null) return EMPTY_BALANCE_SIGN - - val formatter = NumberFormat.getNumberInstance(locale).apply { - maximumFractionDigits = decimals.coerceAtMost(maximumValue = 8) - minimumFractionDigits = 2 - isGroupingUsed = true - roundingMode = RoundingMode.HALF_UP - } - - return formatter.format(cryptoAmount).let { - if (cryptoCurrency.isEmpty()) { - it - } else { - it + "\u2009$cryptoCurrency" - } - } - } - - @Deprecated("Use BigDecimal.format") - fun formatCryptoAmountShorted( - cryptoAmount: BigDecimal?, - cryptoCurrency: String, - decimals: Int, - locale: Locale = Locale.getDefault(), - ): String { - if (cryptoAmount == null) return EMPTY_BALANCE_SIGN - - val formatter = if (cryptoAmount.isMoreThanThreshold()) { - NumberFormat.getNumberInstance(locale).apply { - maximumFractionDigits = 2 - minimumFractionDigits = 2 - isGroupingUsed = true - roundingMode = RoundingMode.HALF_UP - } - } else { - NumberFormat.getNumberInstance(locale).apply { - maximumFractionDigits = decimals.coerceAtMost(maximumValue = 6) - minimumFractionDigits = 2 - isGroupingUsed = true - roundingMode = RoundingMode.DOWN - } - } - - return formatter.format(cryptoAmount).let { - if (cryptoCurrency.isEmpty()) { - it - } else { - it + "\u2009$cryptoCurrency" - } - } - } - - @Deprecated("Use BigDecimal.format") - fun formatCryptoAmountUncapped( - cryptoAmount: BigDecimal?, - cryptoCurrency: CryptoCurrency, - locale: Locale = Locale.getDefault(), - ): String { - if (cryptoAmount == null) return EMPTY_BALANCE_SIGN - - val formatter = NumberFormat.getNumberInstance(locale).apply { - maximumFractionDigits = cryptoCurrency.decimals - minimumFractionDigits = 2 - isGroupingUsed = true - roundingMode = RoundingMode.HALF_UP - } - - return formatter.format(cryptoAmount).let { - if (cryptoCurrency.symbol.isEmpty()) { - it - } else { - it + "\u2009${cryptoCurrency.symbol}" - } - } - } - - @Deprecated("Use BigDecimal.format") - fun formatCryptoFeeAmount( - cryptoAmount: BigDecimal?, - cryptoCurrency: String, - decimals: Int, - canBeLower: Boolean = false, - locale: Locale = Locale.getDefault(), - ): String { - if (cryptoAmount == null) return EMPTY_BALANCE_SIGN - - val formatter = NumberFormat.getNumberInstance(locale).apply { - maximumFractionDigits = decimals.coerceAtMost(maximumValue = 6) - minimumFractionDigits = 2 - isGroupingUsed = true - roundingMode = RoundingMode.HALF_UP - } - - val amountFormatted = if (cryptoAmount.checkCryptoThreshold()) { - buildString { - append(CAN_BE_LOWER_SIGN) - append( - formatter.format(CRYPTO_FEE_FORMAT_THRESHOLD), - ) - } - } else { - buildString { - if (canBeLower) { - append(CAN_BE_LOWER_SIGN) - } - append(formatter.format(cryptoAmount)) - } - } - - return if (cryptoCurrency.isEmpty()) { - amountFormatted - } else { - amountFormatted + "\u2009$cryptoCurrency" - } - } - - @Deprecated("Use BigDecimal.format") - fun formatCryptoAmount( - cryptoAmount: BigDecimal?, - cryptoCurrency: CryptoCurrency, - locale: Locale = Locale.getDefault(), - ): String { - return formatCryptoAmount(cryptoAmount, cryptoCurrency.symbol, cryptoCurrency.decimals, locale) - } - @Deprecated("Use BigDecimal.format") fun formatFiatAmount( fiatAmount: BigDecimal?, @@ -266,28 +131,6 @@ object BigDecimalFormatter { } } - @Deprecated("Use BigDecimal.format") - fun formatPercent( - percent: BigDecimal, - useAbsoluteValue: Boolean, - locale: Locale = Locale.getDefault(), - maxFractionDigits: Int = 2, - minFractionDigits: Int = 2, - ): String { - val formatter = NumberFormat.getPercentInstance(locale).apply { - maximumFractionDigits = maxFractionDigits - minimumFractionDigits = minFractionDigits - roundingMode = RoundingMode.HALF_UP - } - val value = if (useAbsoluteValue) percent.abs() else percent - - return formatter.format(value) - } - - fun formatWithSymbol(amount: String, symbol: String) = "$amount\u2009$symbol" - - private fun BigDecimal.isMoreThanThreshold() = this > FORMAT_THRESHOLD - private fun getCurrency(code: String): Currency { return runCatching { Currency.getInstance(code) } .getOrElse { e -> @@ -301,6 +144,4 @@ object BigDecimalFormatter { } private fun BigDecimal.checkFiatThreshold() = this > BigDecimal.ZERO && this < FIAT_FORMAT_THRESHOLD - - private fun BigDecimal.checkCryptoThreshold() = this > BigDecimal.ZERO && this < CRYPTO_FEE_FORMAT_THRESHOLD } \ No newline at end of file diff --git a/core/ui/src/test/kotlin/com/tangem/core/ui/format/bigdecimal/BigDecimalCryptoFormatTest.kt b/core/ui/src/test/kotlin/com/tangem/core/ui/format/bigdecimal/BigDecimalCryptoFormatTest.kt index cc6fa5e40e..a073607b8b 100644 --- a/core/ui/src/test/kotlin/com/tangem/core/ui/format/bigdecimal/BigDecimalCryptoFormatTest.kt +++ b/core/ui/src/test/kotlin/com/tangem/core/ui/format/bigdecimal/BigDecimalCryptoFormatTest.kt @@ -370,4 +370,38 @@ internal class BigDecimalCryptoFormatTest { Truth.assertThat(formatted) .isEqualTo("0.13".addSymbolWithSpaceLeft(symbol)) } + + // === anyDecimals() === + + @Test + fun `anyDecimals smoke`() { + val testValue = BigDecimal("0.123412341234") + + val formatted = testValue.format { + crypto( + symbol = symbol, + decimals = 5, + locale = testLocale, + ).anyDecimals() + } + + Truth.assertThat(formatted) + .isEqualTo("0.12341".addSymbolWithSpaceLeft(symbol)) + } + + @Test + fun `anyDecimals zero`() { + val testValue = BigDecimal("0.123412341234") + + val formatted = testValue.format { + crypto( + symbol = symbol, + decimals = 0, + locale = testLocale, + ).anyDecimals() + } + + Truth.assertThat(formatted) + .isEqualTo("0".addSymbolWithSpaceLeft(symbol)) + } } \ No newline at end of file diff --git a/core/utils/src/main/java/com/tangem/utils/CryptoCurrencyFormatExtensions.kt b/core/utils/src/main/java/com/tangem/utils/CryptoCurrencyFormatExtensions.kt deleted file mode 100644 index ce1a6d2470..0000000000 --- a/core/utils/src/main/java/com/tangem/utils/CryptoCurrencyFormatExtensions.kt +++ /dev/null @@ -1,43 +0,0 @@ -package com.tangem.utils - -import java.math.BigDecimal -import java.math.RoundingMode -import java.text.DecimalFormat -import java.text.NumberFormat -import java.util.Locale - -// todo determine where to place this extensions -fun BigDecimal.toFormattedString( - decimals: Int, - roundingMode: RoundingMode = RoundingMode.DOWN, - locale: Locale = Locale.getDefault(), -): String { - val formatter = NumberFormat.getInstance(locale) as? DecimalFormat - val df = formatter?.apply { - maximumFractionDigits = decimals - minimumFractionDigits = 0 - isGroupingUsed = true - this.roundingMode = roundingMode - } - return df?.format(this) ?: this.toPlainString() -} - -@Suppress("MagicNumber") -fun BigDecimal.toFormattedCurrencyString( - decimals: Int, - currency: String? = null, - roundingMode: RoundingMode = RoundingMode.DOWN, - limitNumberOfDecimals: Boolean = true, -): String { - val decimalsForRounding = if (limitNumberOfDecimals) { - if (decimals > 8) 8 else decimals - } else { - decimals - } - val formattedAmount = this.toFormattedString( - decimals = decimalsForRounding, - roundingMode = roundingMode, - ) - val formattedCurrency = currency?.let { " $it" } ?: "" - return "$formattedAmount$formattedCurrency" -} \ No newline at end of file diff --git a/domain/transaction/src/main/java/com/tangem/domain/transaction/usecase/SendTransactionUseCase.kt b/domain/transaction/src/main/java/com/tangem/domain/transaction/usecase/SendTransactionUseCase.kt index 3fb6ba5bc1..3d3f3063c6 100644 --- a/domain/transaction/src/main/java/com/tangem/domain/transaction/usecase/SendTransactionUseCase.kt +++ b/domain/transaction/src/main/java/com/tangem/domain/transaction/usecase/SendTransactionUseCase.kt @@ -11,6 +11,8 @@ import com.tangem.blockchain.extensions.Result import com.tangem.blockchain.network.ResultChecker import com.tangem.common.core.TangemSdkError import com.tangem.core.ui.extensions.wrappedList +import com.tangem.core.ui.format.bigdecimal.format +import com.tangem.core.ui.format.bigdecimal.simple import com.tangem.domain.card.repository.CardSdkConfigRepository import com.tangem.domain.common.TapWorkarounds.isStart2Coin import com.tangem.domain.common.TapWorkarounds.isTangemTwins @@ -24,7 +26,6 @@ import com.tangem.domain.transaction.error.SendTransactionError.Companion.USER_C import com.tangem.domain.walletmanager.WalletManagersFacade import com.tangem.domain.wallets.models.UserWallet import com.tangem.sdk.extensions.localizedDescriptionRes -import com.tangem.utils.toFormattedString class SendTransactionUseCase( private val demoConfig: DemoConfig, @@ -120,7 +121,7 @@ class SendTransactionUseCase( is BlockchainSdkError.WrappedTangemError -> parseWrappedError(error) is BlockchainSdkError.CreateAccountUnderfunded -> { val minAmount = error.minReserve - val minValue = minAmount.value?.toFormattedString(minAmount.decimals).orEmpty() + val minValue = minAmount.value?.format { simple(minAmount.decimals) }.orEmpty() SendTransactionError.CreateAccountUnderfunded(minValue) } else -> { diff --git a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/details/impl/model/MarketsTokenDetailsModel.kt b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/details/impl/model/MarketsTokenDetailsModel.kt index b956de8357..3300e07216 100644 --- a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/details/impl/model/MarketsTokenDetailsModel.kt +++ b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/details/impl/model/MarketsTokenDetailsModel.kt @@ -17,6 +17,8 @@ import com.tangem.core.ui.components.marketprice.PriceChangeType import com.tangem.core.ui.event.consumedEvent import com.tangem.core.ui.extensions.TextReference import com.tangem.core.ui.extensions.resourceReference +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.domain.appcurrency.GetSelectedAppCurrencyUseCase import com.tangem.domain.appcurrency.model.AppCurrency @@ -172,12 +174,7 @@ internal class MarketsTokenDetailsModel @Inject constructor( fiatCurrencySymbol = currentAppCurrency.value.symbol, ), dateTimeText = resourceReference(R.string.common_today), - priceChangePercentText = params.token.tokenQuotes.h24Percent?.let { - BigDecimalFormatter.formatPercent( - percent = it, - useAbsoluteValue = true, - ) - }, + priceChangePercentText = params.token.tokenQuotes.h24Percent?.format { percent() }, priceChangeType = params.token.tokenQuotes.h24Percent.percentChangeType(), iconUrl = params.token.imageUrl, chartState = MarketsTokenDetailsUM.ChartState( @@ -472,12 +469,7 @@ internal class MarketsTokenDetailsModel @Inject constructor( ) } ?: currentQuotes.value.getPercentByInterval(currentState.selectedInterval) - val percentText = percent?.let { - BigDecimalFormatter.formatPercent( - percent = it, - useAbsoluteValue = true, - ) - } ?: "" + val percentText = percent?.format { percent() } ?: "" state.update { stateToUpdate -> stateToUpdate.copy( diff --git a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/details/impl/model/formatter/Formatters.kt b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/details/impl/model/formatter/Formatters.kt index 4c185c04e3..890606701f 100644 --- a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/details/impl/model/formatter/Formatters.kt +++ b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/details/impl/model/formatter/Formatters.kt @@ -2,6 +2,8 @@ package com.tangem.features.markets.details.impl.model.formatter import com.tangem.common.ui.charts.state.MarketChartLook import com.tangem.core.ui.components.marketprice.PriceChangeType +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.domain.appcurrency.model.AppCurrency import com.tangem.domain.markets.PriceChangeInterval @@ -28,12 +30,7 @@ internal fun TokenQuotes.getFormattedPercentByInterval(interval: PriceChangeInte PriceChangeInterval.ALL_TIME -> allTimeChangePercent } - return percent?.let { - BigDecimalFormatter.formatPercent( - percent = it, - useAbsoluteValue = true, - ) - } ?: "" + return percent?.format { percent() } ?: "" } internal fun TokenQuotes.getPercentByInterval(interval: PriceChangeInterval): BigDecimal? { diff --git a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/token/block/impl/model/TokenMarketBlockModel.kt b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/token/block/impl/model/TokenMarketBlockModel.kt index d0ca34298a..35346aeb94 100644 --- a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/token/block/impl/model/TokenMarketBlockModel.kt +++ b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/token/block/impl/model/TokenMarketBlockModel.kt @@ -11,6 +11,8 @@ import com.tangem.core.decompose.model.Model import com.tangem.core.decompose.model.ParamsContainer import com.tangem.core.decompose.navigation.Router import com.tangem.core.ui.components.marketprice.PriceChangeType +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.domain.appcurrency.GetSelectedAppCurrencyUseCase import com.tangem.domain.appcurrency.model.AppCurrency @@ -91,10 +93,7 @@ internal class TokenMarketBlockModel @Inject constructor( // TODO get currency from quotes use case [REDACTED_TASK_KEY] fiatCurrencySymbol = currentAppCurrency.value.symbol, ), - h24Percent = BigDecimalFormatter.formatPercent( - percent = res.priceChange, - useAbsoluteValue = true, - ), + h24Percent = res.priceChange.format { percent() }, priceChangeType = PriceChangeType.fromBigDecimal(res.priceChange), ) } diff --git a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/tokenlist/impl/model/converters/MarketsTokenItemConverter.kt b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/tokenlist/impl/model/converters/MarketsTokenItemConverter.kt index f4d62e02de..41fc02dedd 100644 --- a/features/markets/impl/src/main/kotlin/com/tangem/features/markets/tokenlist/impl/model/converters/MarketsTokenItemConverter.kt +++ b/features/markets/impl/src/main/kotlin/com/tangem/features/markets/tokenlist/impl/model/converters/MarketsTokenItemConverter.kt @@ -8,6 +8,7 @@ import com.tangem.core.ui.components.marketprice.PriceChangeType import com.tangem.core.ui.format.bigdecimal.compact import com.tangem.core.ui.format.bigdecimal.fiat 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.domain.appcurrency.model.AppCurrency import com.tangem.domain.markets.TokenMarket @@ -149,9 +150,6 @@ internal class MarketsTokenItemConverter( TrendInterval.M1 -> tokenQuotesShort.monthChangePercent } - return BigDecimalFormatter.formatPercent( - percent = percent, - useAbsoluteValue = true, - ) + return percent.format { percent() } } } \ No newline at end of file diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/recipient/SendRecipientHistoryListConverter.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/recipient/SendRecipientHistoryListConverter.kt index 8a8576775e..946bcbb17c 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/recipient/SendRecipientHistoryListConverter.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/state/recipient/SendRecipientHistoryListConverter.kt @@ -4,6 +4,8 @@ import com.tangem.common.extensions.isZero import com.tangem.core.ui.extensions.TextReference 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.utils.DateTimeFormatters import com.tangem.core.ui.utils.toDateFormatWithTodayYesterday import com.tangem.core.ui.utils.toTimeFormat @@ -17,7 +19,6 @@ import com.tangem.features.send.impl.presentation.state.recipient.utils.RECENT_K import com.tangem.features.send.impl.presentation.state.recipient.utils.emptyListState import com.tangem.utils.Provider import com.tangem.utils.converter.Converter -import com.tangem.utils.toFormattedCurrencyString import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.toPersistentList @@ -76,10 +77,7 @@ internal class SendRecipientHistoryListConverter( } private fun TxHistoryItem.getAmount(cryptoCurrency: CryptoCurrency): String { - return amount.toFormattedCurrencyString( - currency = cryptoCurrency.symbol, - decimals = cryptoCurrency.decimals, - ) + return amount.format { crypto(cryptoCurrency) } } private fun TxHistoryItem.extractTimestamp(): TextReference { diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/ui/SendNavigationButtons.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/ui/SendNavigationButtons.kt index 864a75afa0..4c59863b0c 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/ui/SendNavigationButtons.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/ui/SendNavigationButtons.kt @@ -32,6 +32,9 @@ import com.tangem.core.ui.components.buttons.common.TangemButtonIconPosition import com.tangem.core.ui.components.buttons.common.TangemButtonsDefaults import com.tangem.core.ui.components.keyboardAsState import com.tangem.core.ui.extensions.* +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.fee +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.res.TangemTheme import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.features.send.impl.presentation.state.SendStates @@ -228,12 +231,14 @@ private fun SendStates.FeeState.getFiatValue() = if (isFeeConvertibleToFiat) { ) } else { val amount = fee?.amount - BigDecimalFormatter.formatCryptoFeeAmount( - cryptoAmount = amount?.value, - cryptoCurrency = amount?.currencySymbol.orEmpty(), - decimals = amount?.decimals ?: 0, - canBeLower = isFeeApproximate, - ) + amount?.value.format { + crypto( + decimals = amount?.decimals ?: 0, + symbol = amount?.currencySymbol.orEmpty(), + ).fee( + canBeLower = isFeeApproximate, + ) + } } private fun getButtonData( diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/ui/fee/SendSpeedSelectorItem.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/ui/fee/SendSpeedSelectorItem.kt index 712f915547..6774c2572d 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/ui/fee/SendSpeedSelectorItem.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/ui/fee/SendSpeedSelectorItem.kt @@ -16,6 +16,9 @@ import com.tangem.core.ui.components.RectangleShimmer import com.tangem.core.ui.components.SpacerWMax import com.tangem.core.ui.components.rows.SelectorRowItem import com.tangem.core.ui.extensions.stringReference +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.fee +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.res.TangemTheme import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.core.ui.utils.parseToBigDecimal @@ -53,12 +56,12 @@ internal fun SendSpeedSelectorItem( onSelect = onSelect, modifier = modifier, preDot = stringReference( - BigDecimalFormatter.formatCryptoFeeAmount( - cryptoAmount = amount?.value, - cryptoCurrency = amount?.currencySymbol.orEmpty(), - decimals = amount?.decimals ?: 0, - canBeLower = state.isFeeApproximate, - ), + amount?.value.format { + crypto( + symbol = amount?.currencySymbol.orEmpty(), + decimals = amount?.decimals ?: 0, + ).fee(canBeLower = state.isFeeApproximate) + }, ), postDot = if (state.isFeeConvertibleToFiat) { getFiatReference(amount?.value, state.rate, state.appCurrency) diff --git a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/ui/send/FeeBlock.kt b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/ui/send/FeeBlock.kt index b396811b81..4d5830190e 100644 --- a/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/ui/send/FeeBlock.kt +++ b/features/send/impl/src/main/java/com/tangem/features/send/impl/presentation/ui/send/FeeBlock.kt @@ -18,6 +18,9 @@ import com.tangem.common.ui.amountScreen.utils.getFiatReference import com.tangem.core.ui.components.RectangleShimmer import com.tangem.core.ui.components.rows.SelectorRowItem import com.tangem.core.ui.extensions.stringReference +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.fee +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.res.TangemTheme import com.tangem.core.ui.res.TangemThemePreview import com.tangem.core.ui.utils.BigDecimalFormatter @@ -62,12 +65,12 @@ internal fun FeeBlock(feeState: SendStates.FeeState, isClickDisabled: Boolean, o titleRes = title, iconRes = icon, preDot = stringReference( - BigDecimalFormatter.formatCryptoFeeAmount( - cryptoAmount = feeAmount?.value, - cryptoCurrency = feeAmount?.currencySymbol.orEmpty(), - decimals = feeAmount?.decimals ?: 0, - canBeLower = feeState.isFeeApproximate, - ), + feeAmount?.value.format { + crypto( + symbol = feeAmount?.currencySymbol.orEmpty(), + decimals = feeAmount?.decimals ?: 0, + ).fee(canBeLower = feeState.isFeeApproximate) + }, ), postDot = if (feeState.isFeeConvertibleToFiat) { getFiatReference(feeAmount?.value, feeState.rate, feeState.appCurrency) diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/converters/BalanceItemConverter.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/converters/BalanceItemConverter.kt index e08e0a26d1..3268482b57 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/converters/BalanceItemConverter.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/converters/BalanceItemConverter.kt @@ -1,6 +1,8 @@ package com.tangem.features.staking.impl.presentation.state.converters import com.tangem.core.ui.extensions.* +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.core.ui.utils.parseBigDecimal import com.tangem.domain.appcurrency.model.AppCurrency @@ -47,10 +49,7 @@ internal class BalanceItemConverter( cryptoValue = cryptoAmount.parseBigDecimal(cryptoCurrency.decimals), cryptoDecimal = cryptoAmount, cryptoAmount = stringReference( - BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = cryptoAmount, - cryptoCurrency = cryptoCurrency, - ), + cryptoAmount.format { crypto(cryptoCurrency) }, ), fiatAmount = stringReference( BigDecimalFormatter.formatFiatAmount( diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/converters/RewardsValidatorStateConverter.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/converters/RewardsValidatorStateConverter.kt index aca5b46db7..b03ba279ed 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/converters/RewardsValidatorStateConverter.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/converters/RewardsValidatorStateConverter.kt @@ -1,6 +1,8 @@ package com.tangem.features.staking.impl.presentation.state.converters import com.tangem.core.ui.extensions.stringReference +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.core.ui.utils.parseBigDecimal import com.tangem.domain.appcurrency.model.AppCurrency @@ -65,10 +67,9 @@ internal class RewardsValidatorStateConverter( val appCurrency = appCurrencyProvider() val cryptoCurrency = cryptoCurrencyStatus.currency val cryptoAmount = stringReference( - BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = cryptoValue, - cryptoCurrency = cryptoCurrency, - ), + cryptoValue.format { + crypto(cryptoCurrency) + }, ) val fiatAmount = stringReference( BigDecimalFormatter.formatFiatAmount( diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/converters/YieldBalancesConverter.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/converters/YieldBalancesConverter.kt index 53674f1c51..00bc11c717 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/converters/YieldBalancesConverter.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/converters/YieldBalancesConverter.kt @@ -1,6 +1,8 @@ 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.format import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.domain.appcurrency.model.AppCurrency import com.tangem.domain.staking.model.stakekit.* @@ -41,10 +43,7 @@ internal class YieldBalancesConverter( } val (type, isActionable) = getRewardBlockType() InnerYieldBalanceState.Data( - rewardsCrypto = BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = cryptoRewardsValue, - cryptoCurrency = cryptoCurrency, - ), + rewardsCrypto = cryptoRewardsValue.format { crypto(cryptoCurrency) }, rewardsFiat = BigDecimalFormatter.formatFiatAmount( fiatAmount = fiatRewardsValue, fiatCurrencyCode = appCurrency.code, diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/transformers/SetInitialDataStateTransformer.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/transformers/SetInitialDataStateTransformer.kt index b962abda5b..c3ba140e75 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/transformers/SetInitialDataStateTransformer.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/transformers/SetInitialDataStateTransformer.kt @@ -6,8 +6,10 @@ import com.tangem.common.ui.amountScreen.models.AmountState import com.tangem.core.ui.components.currency.icon.converter.CryptoCurrencyToIconStateConverter import com.tangem.core.ui.components.list.RoundedListWithDividersItemData import com.tangem.core.ui.extensions.* +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.pullToRefresh.PullToRefreshConfig -import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.domain.appcurrency.model.AppCurrency import com.tangem.domain.staking.model.stakekit.BalanceItem import com.tangem.domain.staking.model.stakekit.Yield @@ -126,11 +128,7 @@ internal class SetInitialDataStateTransformer( id = R.string.staking_details_available, startText = TextReference.Res(R.string.staking_details_available), endText = TextReference.Str( - value = BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = cryptoCurrencyStatus.value.amount, - cryptoCurrency = cryptoCurrencyStatus.currency.symbol, - decimals = cryptoCurrencyStatus.currency.decimals, - ), + value = cryptoCurrencyStatus.value.amount.format { crypto(cryptoCurrencyStatus.currency) }, ), isEndTextHideable = true, ) @@ -156,11 +154,7 @@ internal class SetInitialDataStateTransformer( val minimumCryptoAmount = yield.args.enter.args[Yield.Args.ArgType.AMOUNT]?.minimum ?: return null if (!isPolkadot(cryptoCurrencyStatus.currency.network.id.value)) return null - val formattedAmount = BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = minimumCryptoAmount, - cryptoCurrency = cryptoCurrencyStatus.currency.symbol, - decimals = cryptoCurrencyStatus.currency.decimals, - ) + val formattedAmount = minimumCryptoAmount.format { crypto(cryptoCurrencyStatus.currency) } return RoundedListWithDividersItemData( id = R.string.staking_details_minimum_requirement, @@ -218,14 +212,8 @@ internal class SetInitialDataStateTransformer( val minApr = aprValues.min() val maxApr = aprValues.max() - val formattedMinApr = BigDecimalFormatter.formatPercent( - percent = minApr, - useAbsoluteValue = true, - ).remove("%") - val formattedMaxApr = BigDecimalFormatter.formatPercent( - percent = maxApr, - useAbsoluteValue = true, - ) + val formattedMinApr = minApr.format { percent() }.remove("%") + val formattedMaxApr = maxApr.format { percent() } if (maxApr - minApr < EQUALITY_THRESHOLD) { return stringReference("$formattedMinApr%") diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/transformers/amount/AmountRequirementStateTransformer.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/transformers/amount/AmountRequirementStateTransformer.kt index 284f9b239b..88ac2c09f5 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/transformers/amount/AmountRequirementStateTransformer.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/transformers/amount/AmountRequirementStateTransformer.kt @@ -7,7 +7,8 @@ import com.tangem.core.ui.extensions.TextReference import com.tangem.core.ui.extensions.isNullOrEmpty import com.tangem.core.ui.extensions.resourceReference import com.tangem.core.ui.extensions.wrappedList -import com.tangem.core.ui.utils.BigDecimalFormatter +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.utils.parseBigDecimal import com.tangem.domain.staking.model.stakekit.AddressArgument import com.tangem.domain.staking.model.stakekit.Yield @@ -55,11 +56,9 @@ internal class AmountRequirementStateTransformer( isRequirementError -> resourceReference( R.string.staking_amount_requirement_error, wrappedList( - BigDecimalFormatter.formatCryptoAmount( - amountRequirements.minimum, - cryptoCurrencyStatus.currency.symbol, - cryptoCurrencyStatus.currency.decimals, - ), + amountRequirements.minimum.format { + crypto(cryptoCurrencyStatus.currency) + }, ), ) isIntegerOnlyError -> resourceReference( diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/transformers/approval/ShowApprovalBottomSheetTransformer.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/transformers/approval/ShowApprovalBottomSheetTransformer.kt index ca32d6c1be..ea5f081cfd 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/transformers/approval/ShowApprovalBottomSheetTransformer.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/state/transformers/approval/ShowApprovalBottomSheetTransformer.kt @@ -5,6 +5,8 @@ import com.tangem.common.ui.bottomsheet.permission.state.* import com.tangem.core.ui.components.bottomsheets.TangemBottomSheetConfig import com.tangem.core.ui.extensions.resourceReference 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.utils.BigDecimalFormatter import com.tangem.domain.appcurrency.model.AppCurrency import com.tangem.domain.tokens.model.CryptoCurrencyStatus @@ -33,11 +35,9 @@ internal class ShowApprovalBottomSheetTransformer( val walletAddress = cryptoCurrencyValue.networkAddress?.defaultAddress?.value.orEmpty() val validatorAddress = validatorState.chosenValidator.address - val feeCryptoValue = BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = fee.amount.value, - cryptoCurrency = fee.amount.currencySymbol, - decimals = fee.amount.decimals, - ) + val feeCryptoValue = fee.amount.value.format { + crypto(fee.amount.currencySymbol, fee.amount.decimals) + } val feeFiatValue = BigDecimalFormatter.formatFiatAmount( fiatAmount = feeCryptoCurrencyStatus?.value?.fiatRate?.multiply(fee.amount.value), fiatCurrencyCode = appCurrencyProvider().code, diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/StakingClaimRewardsValidatorContent.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/StakingClaimRewardsValidatorContent.kt index fecd9f9cb6..e42c4e7c6c 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/StakingClaimRewardsValidatorContent.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/StakingClaimRewardsValidatorContent.kt @@ -12,8 +12,9 @@ import androidx.compose.ui.Modifier import com.tangem.core.ui.components.inputrow.InputRowImageInfo import com.tangem.core.ui.decorations.roundedShapeItemDecoration import com.tangem.core.ui.extensions.* +import com.tangem.core.ui.format.bigdecimal.format +import com.tangem.core.ui.format.bigdecimal.percent import com.tangem.core.ui.res.TangemTheme -import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.features.staking.impl.R import com.tangem.features.staking.impl.presentation.state.StakingStates import com.tangem.features.staking.impl.presentation.viewmodel.StakingClickIntents @@ -41,10 +42,7 @@ internal fun StakingClaimRewardsValidatorContent( annotatedReference { appendSpace() appendColored( - text = BigDecimalFormatter.formatPercent( - percent = item.validator?.apr.orZero(), - useAbsoluteValue = true, - ), + text = item.validator?.apr.orZero().format { percent() }, color = TangemTheme.colors.text.accent, ) }, diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/StakingInitialInfoContent.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/StakingInitialInfoContent.kt index 4a5d106633..2895d3619c 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/StakingInitialInfoContent.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/StakingInitialInfoContent.kt @@ -41,11 +41,12 @@ import com.tangem.core.ui.components.inputrow.InputRowImageInfo import com.tangem.core.ui.components.list.roundedListWithDividersItems import com.tangem.core.ui.decorations.roundedShapeItemDecoration import com.tangem.core.ui.extensions.* +import com.tangem.core.ui.format.bigdecimal.format +import com.tangem.core.ui.format.bigdecimal.percent import com.tangem.core.ui.pullToRefresh.PullToRefreshConfig import com.tangem.core.ui.res.TangemColorPalette import com.tangem.core.ui.res.TangemTheme import com.tangem.core.ui.res.TangemThemePreview -import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.domain.staking.model.stakekit.BalanceType import com.tangem.domain.staking.model.stakekit.RewardBlockType import com.tangem.features.staking.impl.R @@ -311,10 +312,7 @@ private fun BalanceState.getAprText() = combinedReference( annotatedReference { appendSpace() appendColored( - text = BigDecimalFormatter.formatPercent( - percent = validator?.apr.orZero(), - useAbsoluteValue = true, - ), + text = validator?.apr.orZero().format { percent() }, color = TangemTheme.colors.text.accent, ) }, diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/StakingValidatorListContent.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/StakingValidatorListContent.kt index 692f5b3050..1bcd5c9c60 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/StakingValidatorListContent.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/StakingValidatorListContent.kt @@ -24,9 +24,10 @@ import androidx.compose.ui.unit.dp import com.tangem.core.ui.components.inputrow.InputRowImageSelector import com.tangem.core.ui.decorations.roundedShapeItemDecoration import com.tangem.core.ui.extensions.* +import com.tangem.core.ui.format.bigdecimal.format +import com.tangem.core.ui.format.bigdecimal.percent import com.tangem.core.ui.res.TangemTheme import com.tangem.core.ui.res.TangemThemePreview -import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.features.staking.impl.R import com.tangem.features.staking.impl.presentation.state.StakingStates import com.tangem.features.staking.impl.presentation.state.previewdata.ValidatorStatePreviewData @@ -67,7 +68,7 @@ internal fun StakingValidatorListContent( annotatedReference { appendSpace() appendColored( - text = BigDecimalFormatter.formatPercent(item.apr.orZero(), true), + text = item.apr.orZero().format { percent() }, color = TangemTheme.colors.text.accent, ) }, diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/block/StakingFeeBlock.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/block/StakingFeeBlock.kt index e666de9908..8817faede7 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/block/StakingFeeBlock.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/block/StakingFeeBlock.kt @@ -21,6 +21,9 @@ import com.tangem.common.ui.amountScreen.utils.getFiatReference import com.tangem.core.ui.components.RectangleShimmer import com.tangem.core.ui.components.rows.SelectorRowItem import com.tangem.core.ui.extensions.stringReference +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.fee +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.res.TangemTheme import com.tangem.core.ui.res.TangemThemePreview import com.tangem.core.ui.utils.BigDecimalFormatter @@ -56,12 +59,12 @@ internal fun StakingFeeBlock(feeState: FeeState, isTransactionSent: Boolean) { titleRes = R.string.common_fee_selector_option_market, iconRes = R.drawable.ic_bird_24, preDot = stringReference( - BigDecimalFormatter.formatCryptoFeeAmount( - cryptoAmount = feeAmount?.value, - cryptoCurrency = feeAmount?.currencySymbol.orEmpty(), - decimals = feeAmount?.decimals ?: 0, - canBeLower = feeState.isFeeApproximate, - ), + feeAmount?.value.format { + crypto( + symbol = feeAmount?.currencySymbol.orEmpty(), + decimals = feeAmount?.decimals ?: 0, + ).fee(canBeLower = feeState.isFeeApproximate) + }, ), postDot = if (feeState.isFeeConvertibleToFiat) { getFiatReference(feeAmount?.value, feeState.rate, feeState.appCurrency) diff --git a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/block/ValidatorBlock.kt b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/block/ValidatorBlock.kt index 2da1751f34..3d5cc0310a 100644 --- a/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/block/ValidatorBlock.kt +++ b/features/staking/impl/src/main/java/com/tangem/features/staking/impl/presentation/ui/block/ValidatorBlock.kt @@ -12,8 +12,9 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import com.tangem.core.ui.components.inputrow.InputRowImageInfo import com.tangem.core.ui.extensions.* +import com.tangem.core.ui.format.bigdecimal.format +import com.tangem.core.ui.format.bigdecimal.percent import com.tangem.core.ui.res.TangemTheme -import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.features.staking.impl.R import com.tangem.features.staking.impl.presentation.state.StakingStates import com.tangem.features.staking.impl.presentation.ui.ValidatorImagePlaceholder @@ -43,7 +44,7 @@ internal fun ValidatorBlock(validatorState: StakingStates.ValidatorState, isClic append(resourceReference(R.string.staking_details_apr).resolveReference()) appendSpace() appendColored( - text = BigDecimalFormatter.formatPercent(state.chosenValidator.apr.orZero(), true), + text = state.chosenValidator.apr.orZero().format { percent() }, color = TangemTheme.colors.text.accent, ) }, diff --git a/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/SwapAmount.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/SwapAmount.kt index 4b451b4dc3..aca86a3d52 100644 --- a/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/SwapAmount.kt +++ b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/SwapAmount.kt @@ -1,6 +1,5 @@ package com.tangem.feature.swap.domain.models -import com.tangem.utils.toFormattedString import java.math.BigDecimal import java.math.RoundingMode @@ -16,10 +15,6 @@ data class SwapAmount( } } -fun SwapAmount.formatToUIRepresentation(): String { - return value.toFormattedString(decimals = decimals) -} - fun createFromAmountWithoutOffset(amountWithoutOffset: String, decimals: Int): SwapAmount { return SwapAmount( value = requireNotNull( diff --git a/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/AmountFormatter.kt b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/AmountFormatter.kt index 01914d161b..73a7999a48 100644 --- a/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/AmountFormatter.kt +++ b/features/swap/domain/models/src/main/java/com/tangem/feature/swap/domain/models/ui/AmountFormatter.kt @@ -1,10 +1,9 @@ package com.tangem.feature.swap.domain.models.ui import com.tangem.feature.swap.domain.models.SwapAmount -import com.tangem.utils.toFormattedCurrencyString import java.math.BigDecimal -class AmountFormatter { +interface AmountFormatter { /** * Use to convert crypto amount [SwapAmount] to UI representation @@ -13,9 +12,7 @@ class AmountFormatter { * @param currency currency symbol * @return formatted [String] */ - fun formatSwapAmountToUI(swapAmount: SwapAmount, currency: String): String { - return swapAmount.value.toFormattedCurrencyString(swapAmount.decimals, currency) - } + fun formatSwapAmountToUI(swapAmount: SwapAmount, currency: String): String /** * Use to convert ONLY crypto amount [BigDecimal] to UI representation @@ -25,7 +22,5 @@ class AmountFormatter { * @param currency * @return formatted [String] */ - fun formatBigDecimalAmountToUI(amount: BigDecimal, decimals: Int, currency: String? = null): String { - return amount.toFormattedCurrencyString(decimals, currency) - } + fun formatBigDecimalAmountToUI(amount: BigDecimal, decimals: Int, currency: String? = null): String } \ No newline at end of file diff --git a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt index b77df21551..63b9c43970 100644 --- a/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt +++ b/features/swap/domain/src/main/java/com/tangem/feature/swap/domain/SwapInteractorImpl.kt @@ -71,6 +71,7 @@ internal class SwapInteractorImpl @AssistedInject constructor( private val validateTransactionUseCase: ValidateTransactionUseCase, private val estimateFeeUseCase: EstimateFeeUseCase, private val getUserWalletUseCase: GetUserWalletUseCase, + private val amountFormatter: AmountFormatter, @Assisted private val userWalletId: UserWalletId, ) : SwapInteractor { @@ -79,7 +80,6 @@ internal class SwapInteractorImpl @AssistedInject constructor( } private val swapCurrencyConverter = SwapCurrencyConverter() - private val amountFormatter = AmountFormatter() private val hundredPercent = BigInteger("100") private val userWallet diff --git a/features/swap/presentation/src/main/java/com/tangem/feature/swap/converters/TokensDataConverter.kt b/features/swap/presentation/src/main/java/com/tangem/feature/swap/converters/TokensDataConverter.kt index 390b8e7c7e..49a1dbc25b 100644 --- a/features/swap/presentation/src/main/java/com/tangem/feature/swap/converters/TokensDataConverter.kt +++ b/features/swap/presentation/src/main/java/com/tangem/feature/swap/converters/TokensDataConverter.kt @@ -2,6 +2,8 @@ package com.tangem.feature.swap.converters import com.tangem.core.ui.components.currency.icon.CurrencyIconState import com.tangem.core.ui.extensions.* +import com.tangem.core.ui.format.bigdecimal.crypto +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.tokens.model.CryptoCurrency @@ -103,11 +105,9 @@ class TokensDataConverter( } private fun formatCryptoAmount(cryptoCurrencyStatus: CryptoCurrencyStatus): String { - return BigDecimalFormatter.formatCryptoAmount( - cryptoCurrencyStatus.value.amount, - cryptoCurrencyStatus.currency.symbol, - cryptoCurrencyStatus.currency.decimals, - ) + return cryptoCurrencyStatus.value.amount.format { + crypto(cryptoCurrencyStatus.currency) + } } private fun formatFiatAmount(cryptoCurrencyStatus: CryptoCurrencyStatus, appCurrency: AppCurrency): String { diff --git a/features/swap/presentation/src/main/java/com/tangem/feature/swap/di/SwapSingletonModule.kt b/features/swap/presentation/src/main/java/com/tangem/feature/swap/di/SwapSingletonModule.kt new file mode 100644 index 0000000000..d265dc06e2 --- /dev/null +++ b/features/swap/presentation/src/main/java/com/tangem/feature/swap/di/SwapSingletonModule.kt @@ -0,0 +1,18 @@ +package com.tangem.feature.swap.di + +import com.tangem.feature.swap.di.impl.DefaultAmountFormatter +import com.tangem.feature.swap.domain.models.ui.AmountFormatter +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent + +@Module +@InstallIn(SingletonComponent::class) +class SwapSingletonModule { + + @Provides + fun provideAmountFormatter(): AmountFormatter { + return DefaultAmountFormatter() + } +} \ No newline at end of file diff --git a/features/swap/presentation/src/main/java/com/tangem/feature/swap/di/impl/DefaultAmountFormatter.kt b/features/swap/presentation/src/main/java/com/tangem/feature/swap/di/impl/DefaultAmountFormatter.kt new file mode 100644 index 0000000000..91d8dd1821 --- /dev/null +++ b/features/swap/presentation/src/main/java/com/tangem/feature/swap/di/impl/DefaultAmountFormatter.kt @@ -0,0 +1,18 @@ +package com.tangem.feature.swap.di.impl + +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format +import com.tangem.feature.swap.domain.models.SwapAmount +import com.tangem.feature.swap.domain.models.ui.AmountFormatter +import java.math.BigDecimal + +class DefaultAmountFormatter : AmountFormatter { + + override fun formatSwapAmountToUI(swapAmount: SwapAmount, currency: String): String { + return swapAmount.value.format { crypto(symbol = currency, decimals = swapAmount.decimals) } + } + + override fun formatBigDecimalAmountToUI(amount: BigDecimal, decimals: Int, currency: String?): String { + return amount.format { crypto(symbol = currency.orEmpty(), decimals = decimals) } + } +} \ No newline at end of file diff --git a/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt b/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt index 96ae124966..6fbbf57b82 100644 --- a/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt +++ b/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/StateBuilder.kt @@ -10,6 +10,10 @@ import com.tangem.core.ui.components.notifications.NotificationConfig import com.tangem.core.ui.event.consumedEvent import com.tangem.core.ui.event.triggeredEvent import com.tangem.core.ui.extensions.* +import com.tangem.core.ui.format.bigdecimal.anyDecimals +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format +import com.tangem.core.ui.format.bigdecimal.uncapped import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.domain.appcurrency.model.AppCurrency import com.tangem.domain.tokens.model.CryptoCurrency @@ -19,12 +23,12 @@ import com.tangem.feature.swap.converters.TokensDataConverter import com.tangem.feature.swap.domain.models.ExpressDataError import com.tangem.feature.swap.domain.models.SwapAmount import com.tangem.feature.swap.domain.models.domain.* -import com.tangem.feature.swap.domain.models.formatToUIRepresentation import com.tangem.feature.swap.domain.models.ui.* import com.tangem.feature.swap.models.* import com.tangem.feature.swap.models.states.* import com.tangem.feature.swap.models.states.events.SwapEvent import com.tangem.feature.swap.presentation.R +import com.tangem.feature.swap.utils.formatToUIRepresentation import com.tangem.feature.swap.utils.getExpressErrorMessage import com.tangem.feature.swap.utils.getExpressErrorTitle import com.tangem.feature.swap.viewmodels.SwapProcessDataState @@ -469,10 +473,8 @@ internal class StateBuilder( domainWarning: Warning.ExistentialDepositWarning, ): SwapWarning { val fromCurrency = quoteModel.fromTokenInfo.cryptoCurrencyStatus.currency - val deposit = BigDecimalFormatter.formatCryptoAmountUncapped( - cryptoAmount = domainWarning.existentialDeposit, - cryptoCurrency = fromCurrency, - ) + val deposit = domainWarning.existentialDeposit.format { crypto(fromCurrency).uncapped() } + return SwapWarning.GeneralError( NotificationConfig( title = resourceReference(R.string.send_notification_existential_deposit_title), @@ -1546,8 +1548,12 @@ internal class StateBuilder( toTokenInfo.cryptoCurrencyStatus.currency.decimals, ) val fromCurrencySymbol = fromTokenInfo.cryptoCurrencyStatus.currency.symbol - val toCurrencySymbol = toTokenInfo.cryptoCurrencyStatus.currency.symbol - val rateString = "1 $fromCurrencySymbol ≈ $rate $toCurrencySymbol" + val rateString = buildString { + append(BigDecimal.ONE.format { crypto(symbol = fromCurrencySymbol, decimals = 0).anyDecimals() }) + append(" ≈ ") + append(rate.format { crypto(toTokenInfo.cryptoCurrencyStatus.currency) }) + } + // val rateString = "1 $fromCurrencySymbol ≈ $rate $toCurrencySymbol" val badge = if (isRecommended) { ProviderState.AdditionalBadge.Recommended } else if (isNeedBestRateBadge && isBestRate) { @@ -1628,7 +1634,7 @@ internal class StateBuilder( private fun CryptoCurrencyStatus.getFormattedAmount(isNeedSymbol: Boolean): String { val amount = value.amount ?: return DASH_SIGN val symbol = if (isNeedSymbol) currency.symbol else "" - return BigDecimalFormatter.formatCryptoAmount(amount, symbol, currency.decimals) + return amount.format { crypto(symbol, currency.decimals) } } @Suppress("UnusedPrivateMember") @@ -1646,7 +1652,7 @@ internal class StateBuilder( } private fun SwapAmount.getFormattedCryptoAmount(token: CryptoCurrency): String { - return BigDecimalFormatter.formatCryptoAmount(value, token.symbol, token.decimals) + return value.format { crypto(token) } } private fun BigDecimal.calculateRate(to: BigDecimal, decimals: Int): BigDecimal { diff --git a/features/swap/presentation/src/main/java/com/tangem/feature/swap/utils/SwapUtils.kt b/features/swap/presentation/src/main/java/com/tangem/feature/swap/utils/SwapUtils.kt index e288546e37..44c918c906 100644 --- a/features/swap/presentation/src/main/java/com/tangem/feature/swap/utils/SwapUtils.kt +++ b/features/swap/presentation/src/main/java/com/tangem/feature/swap/utils/SwapUtils.kt @@ -3,7 +3,10 @@ package com.tangem.feature.swap.utils import com.tangem.core.ui.extensions.TextReference import com.tangem.core.ui.extensions.resourceReference import com.tangem.core.ui.extensions.wrappedList +import com.tangem.core.ui.format.bigdecimal.format +import com.tangem.core.ui.format.bigdecimal.simple import com.tangem.feature.swap.domain.models.ExpressDataError +import com.tangem.feature.swap.domain.models.SwapAmount import com.tangem.feature.swap.presentation.R internal fun getExpressErrorMessage(expressDataError: ExpressDataError): TextReference { @@ -38,4 +41,8 @@ internal fun getExpressErrorTitle(expressDataError: ExpressDataError): TextRefer is ExpressDataError.UnknownError -> resourceReference(R.string.common_error) else -> resourceReference(R.string.warning_express_refresh_required_title) } +} + +internal fun SwapAmount.formatToUIRepresentation(): String { + return value.format { simple(decimals = decimals) } } \ No newline at end of file diff --git a/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt b/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt index 24e23b5336..0003e45237 100644 --- a/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt +++ b/features/swap/presentation/src/main/java/com/tangem/feature/swap/viewmodels/SwapViewModel.kt @@ -40,7 +40,6 @@ import com.tangem.feature.swap.domain.models.ExpressDataError import com.tangem.feature.swap.domain.models.ExpressException import com.tangem.feature.swap.domain.models.SwapAmount import com.tangem.feature.swap.domain.models.domain.* -import com.tangem.feature.swap.domain.models.formatToUIRepresentation import com.tangem.feature.swap.domain.models.ui.* import com.tangem.feature.swap.models.SwapStateHolder import com.tangem.feature.swap.models.SwapWarning @@ -49,6 +48,7 @@ import com.tangem.feature.swap.presentation.R import com.tangem.feature.swap.router.SwapNavScreen import com.tangem.feature.swap.router.SwapRouter import com.tangem.feature.swap.ui.StateBuilder +import com.tangem.feature.swap.utils.formatToUIRepresentation import com.tangem.utils.Provider import com.tangem.utils.coroutines.* import com.tangem.utils.isNullOrZero diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsBalanceSelectStateConverter.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsBalanceSelectStateConverter.kt index 0625d38565..4a76d6f1c9 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsBalanceSelectStateConverter.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsBalanceSelectStateConverter.kt @@ -1,5 +1,7 @@ package com.tangem.feature.tokendetails.presentation.tokendetails.state.factory +import com.tangem.core.ui.format.bigdecimal.crypto +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 @@ -72,6 +74,6 @@ internal class TokenDetailsBalanceSelectStateConverter( val amount = status.value.amount ?: return BigDecimalFormatter.EMPTY_BALANCE_SIGN val totalAmount = amount.getBalance(selectedBalanceType, stakingCryptoAmount) - return BigDecimalFormatter.formatCryptoAmount(totalAmount, status.currency.symbol, status.currency.decimals) + return totalAmount.format { crypto(status.currency) } } } \ No newline at end of file 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 4ddc9c283e..ae45ac9884 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 @@ -10,6 +10,9 @@ 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.domain.appcurrency.model.AppCurrency import com.tangem.domain.staking.model.StakingAvailability @@ -215,10 +218,7 @@ internal class TokenDetailsLoadedBalanceConverter( stakingEntryInfo: StakingEntryInfo, iconState: IconState, ): StakingBlockUM.StakeAvailable { - val apr = BigDecimalFormatter.formatPercent( - percent = stakingEntryInfo.apr, - useAbsoluteValue = true, - ) + val apr = stakingEntryInfo.apr.format { percent() } return StakingBlockUM.StakeAvailable( titleText = resourceReference( id = R.string.token_details_staking_block_title, @@ -243,7 +243,7 @@ internal class TokenDetailsLoadedBalanceConverter( cryptoAmount = stakingCryptoAmount, fiatAmount = stakingFiatAmount, cryptoValue = stringReference( - BigDecimalFormatter.formatCryptoAmount(stakingCryptoAmount, symbol, decimals), + stakingCryptoAmount.format { crypto(symbol = symbol, decimals = decimals) }, ), fiatValue = stringReference( BigDecimalFormatter.formatFiatAmount( @@ -275,10 +275,7 @@ internal class TokenDetailsLoadedBalanceConverter( private fun formatPriceChange(status: CryptoCurrencyStatus.Value): String { val priceChange = status.priceChange ?: return BigDecimalFormatter.EMPTY_BALANCE_SIGN - return BigDecimalFormatter.formatPercent( - percent = priceChange, - useAbsoluteValue = true, - ) + return priceChange.format { percent() } } private fun formatPrice(status: CryptoCurrencyStatus.Value, appCurrency: AppCurrency): String { @@ -315,7 +312,7 @@ internal class TokenDetailsLoadedBalanceConverter( val amount = status.value.amount ?: return BigDecimalFormatter.EMPTY_BALANCE_SIGN val totalAmount = amount.getBalance(selectedBalanceType, stakingCryptoAmount) - return BigDecimalFormatter.formatCryptoAmount(totalAmount, status.currency.symbol, status.currency.decimals) + return totalAmount.format { crypto(status.currency) } } private fun getRewardText(status: CryptoCurrencyStatus, stakingRewardAmount: BigDecimal?): TextReference { diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsNotificationConverter.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsNotificationConverter.kt index 0722f5a792..86aae8128f 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsNotificationConverter.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsNotificationConverter.kt @@ -2,8 +2,10 @@ package com.tangem.feature.tokendetails.presentation.tokendetails.state.factory import com.tangem.blockchain.common.Blockchain import com.tangem.blockchainsdk.utils.fromNetworkId -import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.core.ui.extensions.resourceReference +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format +import com.tangem.core.ui.format.bigdecimal.shorted import com.tangem.domain.tokens.model.CryptoCurrency import com.tangem.domain.tokens.model.warnings.CryptoCurrencyWarning import com.tangem.domain.tokens.model.warnings.HederaWarnings @@ -78,11 +80,9 @@ internal class TokenDetailsNotificationConverter( CryptoCurrencyWarning.SomeNetworksUnreachable -> NetworksUnreachable is CryptoCurrencyWarning.SomeNetworksNoAccount -> NetworksNoAccount( network = warning.amountCurrency.name, - amount = BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = warning.amountToCreateAccount, - cryptoCurrency = "", - decimals = warning.amountCurrency.decimals, - ), + amount = warning.amountToCreateAccount.format { + crypto(symbol = "", decimals = warning.amountCurrency.decimals) + }, symbol = warning.amountCurrency.symbol, ) is CryptoCurrencyWarning.TopUpWithoutReserve -> TopUpWithoutReserve @@ -105,11 +105,7 @@ internal class TokenDetailsNotificationConverter( ) is HederaWarnings.AssociateWarningWithFee -> HederaAssociateWarning( currency = warning.currency, - fee = BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = warning.fee, - cryptoCurrency = "", - decimals = warning.feeCurrencyDecimals, - ), + fee = warning.fee.format { crypto(symbol = "", decimals = warning.feeCurrencyDecimals) }, feeCurrencySymbol = warning.feeCurrencySymbol, onAssociateClick = clickIntents::onAssociateClick, ) @@ -127,11 +123,7 @@ internal class TokenDetailsNotificationConverter( } private fun formatMana(amount: BigDecimal): String { - return BigDecimalFormatter.formatCryptoAmountShorted( - cryptoAmount = amount, - cryptoCurrency = "", - decimals = Blockchain.Koinos.decimals(), - ) + return amount.format { crypto("", Blockchain.Koinos.decimals()).shorted() } } // workaround for networks that users have misunderstanding diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsSwapTransactionsStateConverter.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsSwapTransactionsStateConverter.kt index 6a8a7087ed..ca04809b0a 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsSwapTransactionsStateConverter.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/TokenDetailsSwapTransactionsStateConverter.kt @@ -3,6 +3,8 @@ package com.tangem.feature.tokendetails.presentation.tokendetails.state.factory import com.tangem.core.analytics.api.AnalyticsEventHandler import com.tangem.core.ui.components.currency.icon.converter.CryptoCurrencyToIconStateConverter import com.tangem.core.ui.extensions.TextReference +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.core.ui.utils.toDateFormatWithTodayYesterday import com.tangem.core.ui.utils.toTimeFormat @@ -80,17 +82,11 @@ internal class TokenDetailsSwapTransactionsStateConverter( activeStatus = transaction.status?.status, notification = notifications, toCryptoCurrency = toCryptoCurrency, - toCryptoAmount = BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = toAmount, - cryptoCurrency = toCryptoCurrency, - ), + toCryptoAmount = toAmount.format { crypto(toCryptoCurrency) }, toFiatAmount = getFiatAmount(toFiatAmount), toCurrencyIcon = iconStateConverter.convert(toCryptoCurrency), fromCryptoCurrency = fromCryptoCurrency, - fromCryptoAmount = BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = fromAmount, - cryptoCurrency = fromCryptoCurrency, - ), + fromCryptoAmount = fromAmount.format { crypto(fromCryptoCurrency) }, fromFiatAmount = getFiatAmount(fromFiatAmount), fromCurrencyIcon = iconStateConverter.convert(fromCryptoCurrency), showProviderLink = showProviderLink, diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsTxHistoryTransactionStateConverter.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsTxHistoryTransactionStateConverter.kt index 0d0b59e987..4756f994ca 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsTxHistoryTransactionStateConverter.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsTxHistoryTransactionStateConverter.kt @@ -7,6 +7,8 @@ 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.utils.toTimeFormat import com.tangem.domain.txhistory.models.TxHistoryItem import com.tangem.domain.txhistory.models.TxHistoryItem.* @@ -16,7 +18,6 @@ import com.tangem.utils.StringsSigns.MINUS import com.tangem.utils.StringsSigns.PLUS import com.tangem.utils.converter.Converter import com.tangem.utils.toBriefAddressFormat -import com.tangem.utils.toFormattedCurrencyString internal class TokenDetailsTxHistoryTransactionStateConverter( private val symbol: String, @@ -129,6 +130,6 @@ internal class TokenDetailsTxHistoryTransactionStateConverter( this.amount.isZero() -> "" else -> if (isOutgoing) MINUS else PLUS } - return prefix + amount.toFormattedCurrencyString(currency = symbol, decimals = decimals) + return prefix + amount.format { crypto(symbol = symbol, decimals = decimals) } } } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/WalletAdditionalInfoFactory.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/WalletAdditionalInfoFactory.kt index 3de82a88d3..f258bf29b6 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/WalletAdditionalInfoFactory.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/domain/WalletAdditionalInfoFactory.kt @@ -4,7 +4,8 @@ import com.tangem.core.ui.extensions.TextReference import com.tangem.core.ui.extensions.plus import com.tangem.core.ui.extensions.stringReference import com.tangem.core.ui.extensions.wrappedList -import com.tangem.core.ui.utils.BigDecimalFormatter +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.domain.common.util.cardTypesResolver import com.tangem.domain.common.util.getCardsCount import com.tangem.domain.wallets.models.UserWallet @@ -97,13 +98,7 @@ internal object WalletAdditionalInfoFactory { WalletAdditionalInfo(hideable = false, content = TextReference.Res(R.string.common_locked)) } else { val blockchain = scanResponse.cardTypesResolver.getBlockchain() - val amount = currencyAmount?.let { - BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = it, - cryptoCurrency = blockchain.currency, - decimals = blockchain.decimals(), - ) - } + val amount = currencyAmount?.format { crypto(blockchain.currency, blockchain.decimals()) } WalletAdditionalInfo(hideable = true, content = TextReference.Str(value = amount.orEmpty())) } diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/SetBalancesAndLimitsTransformer.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/SetBalancesAndLimitsTransformer.kt index d4f7d3db34..af365a24d7 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/SetBalancesAndLimitsTransformer.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/SetBalancesAndLimitsTransformer.kt @@ -3,6 +3,8 @@ package com.tangem.feature.wallet.presentation.wallet.state.transformers import arrow.core.Either import arrow.core.getOrElse import com.tangem.core.ui.extensions.stringReference +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.domain.common.util.getCardsCount import com.tangem.domain.visa.model.VisaCurrency @@ -41,11 +43,9 @@ internal class SetBalancesAndLimitsTransformer( } private fun getContentBlockState(visaCurrency: VisaCurrency) = BalancesAndLimitsBlockState.Content( - availableBalance = BigDecimalFormatter.formatCryptoAmount( - visaCurrency.limits.remainingOtp, - visaCurrency.symbol, - visaCurrency.decimals, - ), + availableBalance = visaCurrency.limits.remainingOtp.format { + crypto(visaCurrency.symbol, visaCurrency.decimals) + }, limitDays = Days.daysBetween(DateTime.now(), visaCurrency.limits.expirationDate).days.inc(), isEnabled = true, onClick = clickIntents::onBalancesAndLimitsClick, @@ -72,11 +72,9 @@ internal class SetBalancesAndLimitsTransformer( imageResId = imageResId, onRenameClick = onRenameClick, onDeleteClick = onDeleteClick, - balance = BigDecimalFormatter.formatCryptoAmount( - visaCurrency.balances.available, - visaCurrency.symbol, - visaCurrency.decimals, - ), + balance = visaCurrency.balances.available.format { + crypto(visaCurrency.symbol, visaCurrency.decimals) + }, cardCount = userWallet.getCardsCount(), ) } diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/BalancesAndLimitsBottomSheetConverter.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/BalancesAndLimitsBottomSheetConverter.kt index 146e196a65..dc965b29e2 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/BalancesAndLimitsBottomSheetConverter.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/BalancesAndLimitsBottomSheetConverter.kt @@ -1,6 +1,7 @@ package com.tangem.feature.wallet.presentation.wallet.state.transformers.converter -import com.tangem.core.ui.utils.BigDecimalFormatter +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.utils.DateTimeFormatters import com.tangem.domain.visa.model.VisaCurrency import com.tangem.feature.wallet.presentation.wallet.state.model.BalancesAndLimitsBottomSheetConfig @@ -15,11 +16,7 @@ internal class BalancesAndLimitsBottomSheetConverter( ) : Converter { override fun convert(value: VisaCurrency): BalancesAndLimitsBottomSheetConfig { - fun formatAmount(amount: BigDecimal): String = BigDecimalFormatter.formatCryptoAmount( - amount, - cryptoCurrency = value.symbol, - decimals = value.decimals, - ) + fun formatAmount(amount: BigDecimal): String = amount.format { crypto(value.symbol, value.decimals) } val otpLimit = value.limits.remainingOtp.let(::formatAmount) val noOtpLimit = value.limits.remainingNoOtp.let(::formatAmount) diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/SingleWalletMarketPriceConverter.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/SingleWalletMarketPriceConverter.kt index 8d710656dc..3611aa8119 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/SingleWalletMarketPriceConverter.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/SingleWalletMarketPriceConverter.kt @@ -4,6 +4,8 @@ import com.tangem.core.ui.components.marketprice.MarketPriceBlockState import com.tangem.core.ui.components.marketprice.PriceChangeState import com.tangem.core.ui.components.marketprice.PriceChangeType import com.tangem.core.ui.components.marketprice.utils.PriceChangeConverter +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.domain.appcurrency.model.AppCurrency import com.tangem.domain.tokens.model.CryptoCurrencyStatus @@ -57,7 +59,7 @@ internal class SingleWalletMarketPriceConverter( private fun formatPriceChange(status: CryptoCurrencyStatus.Value): String { val priceChange = status.priceChange ?: return BigDecimalFormatter.EMPTY_BALANCE_SIGN - return BigDecimalFormatter.formatPercent(percent = priceChange, useAbsoluteValue = true) + return priceChange.format { percent() } } private fun getPriceChangeType(status: CryptoCurrencyStatus.Value): PriceChangeType { diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/TxHistoryItemStateConverter.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/TxHistoryItemStateConverter.kt index 2d162a17c0..461f74999f 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/TxHistoryItemStateConverter.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/TxHistoryItemStateConverter.kt @@ -6,6 +6,8 @@ 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.utils.toTimeFormat import com.tangem.domain.txhistory.models.TxHistoryItem import com.tangem.domain.txhistory.models.TxHistoryItem.* @@ -15,7 +17,6 @@ import com.tangem.utils.StringsSigns.MINUS import com.tangem.utils.StringsSigns.PLUS import com.tangem.utils.converter.Converter import com.tangem.utils.toBriefAddressFormat -import com.tangem.utils.toFormattedCurrencyString internal class TxHistoryItemStateConverter( private val symbol: String, @@ -129,6 +130,6 @@ internal class TxHistoryItemStateConverter( this.amount.isZero() -> "" else -> if (isOutgoing) MINUS else PLUS } - return prefix + amount.toFormattedCurrencyString(currency = symbol, decimals = decimals) + return prefix + amount.format { crypto(symbol = symbol, decimals = decimals) } } } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/VisaTxDetailsBottomSheetConverter.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/VisaTxDetailsBottomSheetConverter.kt index 6e8158da13..e655973c16 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/VisaTxDetailsBottomSheetConverter.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/VisaTxDetailsBottomSheetConverter.kt @@ -1,6 +1,8 @@ package com.tangem.feature.wallet.presentation.wallet.state.transformers.converter import com.tangem.core.ui.extensions.capitalize +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.core.ui.utils.DateTimeFormatters import com.tangem.domain.visa.model.VisaCurrency @@ -66,11 +68,7 @@ internal class VisaTxDetailsBottomSheetConverter( } private fun formatNetworkAmount(amount: BigDecimal): String { - return BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = amount, - cryptoCurrency = visaCurrency.symbol, - decimals = visaCurrency.decimals, - ) + return amount.format { crypto(visaCurrency.symbol, visaCurrency.decimals) } } private fun formatFiatAmount(amount: BigDecimal, fiatCurrency: Currency): String { diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/VisaTxHistoryItemStateConverter.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/VisaTxHistoryItemStateConverter.kt index 60ac94068a..fe50fdf537 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/VisaTxHistoryItemStateConverter.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/transformers/converter/VisaTxHistoryItemStateConverter.kt @@ -3,6 +3,8 @@ package com.tangem.feature.wallet.presentation.wallet.state.transformers.convert import com.tangem.core.ui.components.transactions.state.TransactionState import com.tangem.core.ui.extensions.capitalize import com.tangem.core.ui.extensions.stringReference +import com.tangem.core.ui.format.bigdecimal.crypto +import com.tangem.core.ui.format.bigdecimal.format import com.tangem.core.ui.utils.BigDecimalFormatter import com.tangem.core.ui.utils.DateTimeFormatters import com.tangem.domain.visa.model.VisaCurrency @@ -25,11 +27,7 @@ internal class VisaTxHistoryItemStateConverter( return TransactionState.Content( txHash = value.id, - amount = BigDecimalFormatter.formatCryptoAmount( - cryptoAmount = value.amount, - cryptoCurrency = visaCurrency.symbol, - decimals = visaCurrency.decimals, - ), + amount = value.amount.format { crypto(visaCurrency.symbol, visaCurrency.decimals) }, // Show tx fiat amount instead of tx time time = BigDecimalFormatter.formatFiatAmount( fiatAmount = value.fiatAmount,