From 1a73ce8390a17e0f4e2223f2f6f5b0f188b78d08 Mon Sep 17 00:00:00 2001 From: Tangem Date: Fri, 14 Jul 2023 13:05:56 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../tangem/tap/common/extensions/Specific.kt | 32 ++++++++++++++++--- .../tap/features/wallet/ui/BalanceWidget.kt | 8 ++--- .../wallet/ui/WalletDetailsFragment.kt | 6 ++-- .../wallet/ui/adapters/WalletAdapter.kt | 4 +-- .../wallet/ui/utils/WalletDataOperations.kt | 6 ++-- 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/common/extensions/Specific.kt b/app/src/main/java/com/tangem/tap/common/extensions/Specific.kt index bea9728880..5f259977e0 100644 --- a/app/src/main/java/com/tangem/tap/common/extensions/Specific.kt +++ b/app/src/main/java/com/tangem/tap/common/extensions/Specific.kt @@ -24,13 +24,17 @@ fun BigDecimal.toFormattedString( df.decimalFormatSymbols = symbols df.maximumFractionDigits = decimals df.minimumFractionDigits = 0 - df.isGroupingUsed = false + df.isGroupingUsed = true df.roundingMode = roundingMode return df.format(this) } +/** + * To formatted crypto currency string + * Specific method because there is no crypto currency codes in Locale + */ @Suppress("MagicNumber") -fun BigDecimal.toFormattedCurrencyString( +fun BigDecimal.toFormattedCryptoCurrencyString( decimals: Int, currency: String, roundingMode: RoundingMode = RoundingMode.DOWN, @@ -41,16 +45,34 @@ fun BigDecimal.toFormattedCurrencyString( } else { decimals } + try { + val locale = Locale.getDefault() + val formatter = NumberFormat.getCurrencyInstance(locale) + // first create currency instance for "USD" with Locale default to replace currency to crypto later + Currency.getInstance("USD")?.let { currencyTmp -> + formatter.currency = currencyTmp + formatter.maximumFractionDigits = decimalsForRounding + formatter.minimumFractionDigits = 0 + formatter.isGroupingUsed = true + formatter.roundingMode = roundingMode + // cause formatter created for USD, replace Currency with Crypto symbol on right by Locale place + return formatter.format(this).replace(currencyTmp.getSymbol(locale), "$currency ") + } + } catch (e: IllegalArgumentException) { + Timber.e(e, "can't parse currency") + } + // if something went wrong - use old way to format val formattedAmount = this.toFormattedString( decimals = decimalsForRounding, roundingMode = roundingMode, + locale = Locale.getDefault(), ) - return "$formattedAmount $currency" + return "$formattedAmount $currency " } fun BigDecimal.toFiatRateString(fiatCurrencyName: String, fiatCode: String): String { try { - val formatter = NumberFormat.getCurrencyInstance() + val formatter = NumberFormat.getCurrencyInstance(Locale.getDefault()) Currency.getInstance(fiatCode)?.let { currency -> formatter.currency = currency formatter.maximumFractionDigits = 2 @@ -91,7 +113,7 @@ fun BigDecimal.toFormattedFiatValue( formatWithSpaces: Boolean = false, ): String { try { - val formatter = NumberFormat.getCurrencyInstance() + val formatter = NumberFormat.getCurrencyInstance(Locale.getDefault()) Currency.getInstance(fiatCode)?.let { currency -> formatter.currency = currency formatter.maximumFractionDigits = 2 diff --git a/app/src/main/java/com/tangem/tap/features/wallet/ui/BalanceWidget.kt b/app/src/main/java/com/tangem/tap/features/wallet/ui/BalanceWidget.kt index 23aa7d9d6e..3cab6df274 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/ui/BalanceWidget.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/ui/BalanceWidget.kt @@ -4,7 +4,7 @@ import androidx.annotation.IdRes import com.tangem.tap.common.extensions.hide import com.tangem.tap.common.extensions.show import com.tangem.tap.domain.model.WalletDataModel -import com.tangem.tap.features.wallet.ui.utils.getFormattedAmount +import com.tangem.tap.features.wallet.ui.utils.getFormattedCryptoAmount import com.tangem.tap.features.wallet.ui.utils.getFormattedFiatAmount import com.tangem.tap.store import com.tangem.wallet.R @@ -102,8 +102,8 @@ class BalanceWidget( groupBaseCurrency.show() tvCurrency.text = tokenWalletData?.currency?.currencySymbol tvBaseCurrency.text = data.currency.currencyName - tvAmount.text = if (showAmount) tokenWalletData?.getFormattedAmount() else "" - tvBaseAmount.text = if (showAmount) data.getFormattedAmount() else "" + tvAmount.text = if (showAmount) tokenWalletData?.getFormattedCryptoAmount() else "" + tvBaseAmount.text = if (showAmount) data.getFormattedCryptoAmount() else "" if (showAmount) { tvFiatAmount.show() tvFiatAmount.text = tokenWalletData?.getFormattedFiatAmount(store.state.globalState.appCurrency) @@ -113,7 +113,7 @@ class BalanceWidget( private fun showBalanceWithoutToken(data: WalletDataModel, showAmount: Boolean) = with(binding.lBalance) { groupBaseCurrency.hide() tvCurrency.text = data.currency.currencyName - tvAmount.text = if (showAmount) data.getFormattedAmount() else "" + tvAmount.text = if (showAmount) data.getFormattedCryptoAmount() else "" if (showAmount) { tvFiatAmount.show() tvFiatAmount.text = data.getFormattedFiatAmount(store.state.globalState.appCurrency) diff --git a/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletDetailsFragment.kt b/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletDetailsFragment.kt index c6cad8e1bc..1c0c5027f2 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletDetailsFragment.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletDetailsFragment.kt @@ -59,7 +59,7 @@ import com.tangem.tap.features.wallet.ui.images.load import com.tangem.tap.features.wallet.ui.test.TestWallet import com.tangem.tap.features.wallet.ui.utils.assembleWarnings import com.tangem.tap.features.wallet.ui.utils.getAvailableActions -import com.tangem.tap.features.wallet.ui.utils.getFormattedAmount +import com.tangem.tap.features.wallet.ui.utils.getFormattedCryptoAmount import com.tangem.tap.features.wallet.ui.utils.getFormattedFiatAmount import com.tangem.tap.features.wallet.ui.utils.isAvailableToBuy import com.tangem.tap.features.wallet.ui.utils.isAvailableToSell @@ -425,7 +425,7 @@ class WalletDetailsFragment : Fragment(R.layout.fragment_wallet_details), SafeSt lBalance.root.show() lBalance.groupBalance.show() lBalance.tvError.hide() - lBalance.tvAmount.text = walletData.getFormattedAmount() + lBalance.tvAmount.text = walletData.getFormattedCryptoAmount() lBalance.tvFiatAmount.text = walletData.getFormattedFiatAmount(store.state.globalState.appCurrency) lBalance.tvStatus.setLoadingStatus(R.string.wallet_balance_loading) } @@ -437,7 +437,7 @@ class WalletDetailsFragment : Fragment(R.layout.fragment_wallet_details), SafeSt lBalance.root.show() lBalance.groupBalance.show() lBalance.tvError.hide() - lBalance.tvAmount.text = walletData.getFormattedAmount() + lBalance.tvAmount.text = walletData.getFormattedCryptoAmount() lBalance.tvFiatAmount.text = walletData.getFormattedFiatAmount(store.state.globalState.appCurrency) when (status) { is WalletDataModel.VerifiedOnline, diff --git a/app/src/main/java/com/tangem/tap/features/wallet/ui/adapters/WalletAdapter.kt b/app/src/main/java/com/tangem/tap/features/wallet/ui/adapters/WalletAdapter.kt index 4aec238877..e0a15c7e4b 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/ui/adapters/WalletAdapter.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/ui/adapters/WalletAdapter.kt @@ -15,7 +15,7 @@ import com.tangem.tap.common.extensions.show import com.tangem.tap.domain.model.WalletDataModel import com.tangem.tap.features.wallet.redux.WalletAction import com.tangem.tap.features.wallet.ui.images.load -import com.tangem.tap.features.wallet.ui.utils.getFormattedAmount +import com.tangem.tap.features.wallet.ui.utils.getFormattedCryptoAmount import com.tangem.tap.features.wallet.ui.utils.getFormattedFiatAmount import com.tangem.tap.features.wallet.ui.utils.getFormattedFiatRate import com.tangem.tap.store @@ -85,7 +85,7 @@ class WalletAdapter : ListAdapter