Skip to content

Commit

Permalink
feat: support localization on order preview screen
Browse files Browse the repository at this point in the history
  • Loading branch information
HashEngineering committed Apr 16, 2024
1 parent 7b4575a commit b21b300
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
16 changes: 15 additions & 1 deletion common/src/main/java/org/dash/wallet/common/util/GenericUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.dash.wallet.common.util

import android.os.Build
import android.os.LocaleList
import org.bitcoinj.utils.MonetaryFormat
import java.math.BigDecimal
import java.math.RoundingMode
import java.text.DecimalFormat
Expand Down Expand Up @@ -128,7 +129,7 @@ object GenericUtils {
return currency?.defaultFractionDigits ?: 0
}

fun stringToBigDecimal(value: String): BigDecimal {
private fun stringToBigDecimal(value: String): BigDecimal {
return try {
val format = NumberFormat.getNumberInstance(getDeviceLocale())
val number = format.parse(value)
Expand All @@ -145,4 +146,17 @@ object GenericUtils {
value.toBigDecimal().setScale(scale, RoundingMode.HALF_UP)
}
}

private val dashFormat: MonetaryFormat = MonetaryFormat().withLocale(getDeviceLocale())
.noCode().minDecimals(8).optionalDecimals()
private val fiatFormat: MonetaryFormat = MonetaryFormat().withLocale(getDeviceLocale())
.noCode().minDecimals(getCurrencyDigits()).optionalDecimals()

fun toLocalizedString(value: BigDecimal, isCrypto: Boolean, currencyCode: String): String {
return if (isCrypto) {
dashFormat.format(value.toCoin())
} else {
fiatFormat.format(value.toFiat(currencyCode))
}.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import org.dash.wallet.integrations.maya.databinding.FragmentMayaConversionPrevi
import org.dash.wallet.integrations.maya.model.CurrencyInputType
import org.dash.wallet.integrations.maya.model.SwapTradeUIModel
import org.dash.wallet.integrations.maya.ui.dialogs.MayaResultDialog
import java.math.BigDecimal
import java.math.RoundingMode

@AndroidEntryPoint
Expand Down Expand Up @@ -238,7 +239,7 @@ class MayaConversionPreviewFragment : Fragment(R.layout.fragment_maya_conversion

val isCurrencyCodeFirst = GenericUtils.isCurrencySymbolFirst()
val inputCurrencySymbol = GenericUtils.currencySymbol(this.inputCurrency)
val inputAmount = this.amount.dash.setScale(8, RoundingMode.HALF_UP).toString()
val inputAmount = this.amount.dash.setScale(8, RoundingMode.HALF_UP)

// binding.contentOrderReview.inputAccount.text = getString(
// R.string.fiat_balance_with_currency,
Expand All @@ -250,18 +251,18 @@ class MayaConversionPreviewFragment : Fragment(R.layout.fragment_maya_conversion
inputAmount,
inputCurrencySymbol,
isCurrencyCodeFirst,
true
true, false
)

val outputAmount = this.amount.crypto.setScale(8, RoundingMode.HALF_UP).toString()
val outputAmount = this.amount.crypto.setScale(8, RoundingMode.HALF_UP)
val outputCurrency = this.amount.cryptoCode

setValueWithCurrencyCodeOrSymbol(
binding.contentOrderReview.outputAccount,
outputAmount,
outputCurrency,
isCurrencyCodeFirst,
false
false, false
)

val currencySymbol = GenericUtils.currencySymbol(this.amount.anchoredCurrencyCode)
Expand All @@ -270,38 +271,41 @@ class MayaConversionPreviewFragment : Fragment(R.layout.fragment_maya_conversion
} else {
8
}
val purchaseAmount = this.amount.anchoredValue.setScale(digits, RoundingMode.HALF_UP).toString()
val purchaseAmount = this.amount.anchoredValue.setScale(digits, RoundingMode.HALF_UP)

setValueWithCurrencyCodeOrSymbol(
binding.contentOrderReview.purchaseAmount,
purchaseAmount,
currencySymbol,
isCurrencyCodeFirst,
amount.anchoredCurrencyCode == Constants.DASH_CURRENCY
amount.anchoredCurrencyCode == Constants.DASH_CURRENCY,
amount.anchoredType == CurrencyInputType.Fiat
)

val feeCurrencySymbol = GenericUtils.currencySymbol(this.feeAmount.anchoredCurrencyCode)
val feeAmount = this.feeAmount.anchoredValue.setScale(digits, RoundingMode.HALF_UP).toString()
val feeAmount = this.feeAmount.anchoredValue.setScale(digits, RoundingMode.HALF_UP)

setValueWithCurrencyCodeOrSymbol(
binding.contentOrderReview.mayaFeeAmount,
feeAmount,
feeCurrencySymbol,
isCurrencyCodeFirst,
amount.anchoredCurrencyCode == Constants.DASH_CURRENCY
amount.anchoredCurrencyCode == Constants.DASH_CURRENCY,
amount.anchoredType == CurrencyInputType.Fiat
)

val totalAmount = (this.amount.anchoredValue + this.feeAmount.anchoredValue).setScale(
digits,
RoundingMode.HALF_UP
).toString()
)

setValueWithCurrencyCodeOrSymbol(
binding.contentOrderReview.totalAmount,
totalAmount,
feeCurrencySymbol,
isCurrencyCodeFirst,
amount.anchoredCurrencyCode == Constants.DASH_CURRENCY
amount.anchoredCurrencyCode == Constants.DASH_CURRENCY,
amount.anchoredType == CurrencyInputType.Fiat
)
binding.contentOrderReview.inputAccountIcon
.load(GenericUtils.getCoinIcon(this.inputCurrency.lowercase())) {
Expand All @@ -322,33 +326,21 @@ class MayaConversionPreviewFragment : Fragment(R.layout.fragment_maya_conversion

private fun setValueWithCurrencyCodeOrSymbol(
textView: TextView,
value: String,
value: BigDecimal,
currencyCode: String,
isCurrencySymbolFirst: Boolean,
isDash: Boolean,
isFiat: Boolean,
iconSize: Int = 12
) {
val context = textView.context
val scale = resources.displayMetrics.scaledDensity
val maxTextWidth = binding.contentOrderReview.inputAccount.width
var spannableString = SpannableString(value) // Space for the icon
val valueString = GenericUtils.toLocalizedString(value, isDash || !isFiat, currencyCode)
var spannableString = SpannableString(valueString) // Space for the icon

// show Dash Icon if DASH is the primary currency
if (isDash) {
// TODO: adjust for dark mode

// val roomLeft = maxTextWidth - textView.paint.measureText("$value ") - (iconSize * scale)
// val sizeRelative = if (roomLeft < 0) {
// val ratio = min(1.0f, (maxTextWidth + roomLeft) / maxTextWidth)
// if (ratio == Float.NEGATIVE_INFINITY) {
// 1.0f
// } else {
// ratio
// }
// } else {
// 1.0f
// }
// val sizeSpan = RelativeSizeSpan(sizeRelative)
val drawable = ContextCompat.getDrawable(context, org.dash.wallet.common.R.drawable.ic_dash_d_black)?.apply {
setBounds(0, 0, (iconSize * scale).toInt(), (iconSize * scale).toInt())
}
Expand All @@ -359,11 +351,11 @@ class MayaConversionPreviewFragment : Fragment(R.layout.fragment_maya_conversion
}
imageSpan?.let {
if (GenericUtils.isCurrencySymbolFirst()) {
spannableString = SpannableString(" $value")
spannableString = SpannableString(" $valueString")
spannableString.setSpan(it, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
// spannableString.setSpan(sizeSpan, 1, spannableString.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
} else {
spannableString = SpannableString("$value ")
spannableString = SpannableString("$valueString ")
val len = spannableString.length
spannableString.setSpan(it, len - 1, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
// spannableString.setSpan(sizeSpan, 0, len - 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
Expand All @@ -373,8 +365,8 @@ class MayaConversionPreviewFragment : Fragment(R.layout.fragment_maya_conversion
spannableString = SpannableString(
getString(
R.string.fiat_balance_with_currency,
if (isCurrencySymbolFirst) currencyCode else value,
if (isCurrencySymbolFirst) value else currencyCode
if (isCurrencySymbolFirst) currencyCode else valueString,
if (isCurrencySymbolFirst) valueString else currencyCode
)
)
// val roomLeft = maxTextWidth - textView.paint.measureText("$value")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class ConvertViewViewModel @Inject constructor(
fun checkEnteredAmountValue(checkSendingConditions: Boolean): SwapValueErrorType {
val coin = try {
if (dashToCrypto.value == true) {
Coin.parseCoin(maxForDashWalletAmount)
Coin.parseCoin(maxForDashWalletAmount.replace(',', '.'))
} else {
maxForDashCoinBaseAccount
}
Expand Down

0 comments on commit b21b300

Please sign in to comment.