From c04abfe0bbc22e59bc87687208e8313e3d11c7b6 Mon Sep 17 00:00:00 2001 From: Tangem Date: Mon, 20 May 2024 18:08:47 +0500 Subject: [PATCH] Updated on 2026-08-14 --- .../ui/components/fields/AmountTextField.kt | 60 ++++++++++++------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/core/ui/src/main/java/com/tangem/core/ui/components/fields/AmountTextField.kt b/core/ui/src/main/java/com/tangem/core/ui/components/fields/AmountTextField.kt index 521245b940..2713162e91 100644 --- a/core/ui/src/main/java/com/tangem/core/ui/components/fields/AmountTextField.kt +++ b/core/ui/src/main/java/com/tangem/core/ui/components/fields/AmountTextField.kt @@ -28,6 +28,7 @@ import androidx.compose.ui.tooling.preview.PreviewParameterProvider import com.tangem.core.ui.components.fields.visualtransformations.AmountVisualTransformation import com.tangem.core.ui.res.TangemTheme import com.tangem.core.ui.utils.* +import java.math.BigDecimal import java.text.DecimalFormat /** @@ -123,32 +124,49 @@ fun AmountTextField( private fun prepareEnter(oldValue: String, newValue: String, decimalFormat: DecimalFormat, decimals: Int): String { val decimalSymbol = decimalFormat.decimalFormatSymbols.decimalSeparator return if (decimalFormat.isValidSymbols(newValue)) { - val parsedValue = newValue.parseBigDecimalOrNull()?.toPlainString() - ?: if (newValue.isBlank()) "" else oldValue - val replacedWithSymbol = if (parsedValue.findLast { it != decimalSymbol } != null) { - when { - parsedValue.findLast { it == COMMA_SEPARATOR } != null -> { - parsedValue.replace(COMMA_SEPARATOR, decimalSymbol) - } - parsedValue.findLast { it == POINT_SEPARATOR } != null -> { - parsedValue.replace(POINT_SEPARATOR, decimalSymbol) - } - else -> parsedValue - } - } else { - parsedValue - } - val joinedSymbol = if (newValue.endsWith(COMMA_SEPARATOR) || newValue.endsWith(POINT_SEPARATOR)) { - replacedWithSymbol.plus(decimalSymbol) - } else { - replacedWithSymbol - } - decimalFormat.getValidatedNumberWithFixedDecimals(joinedSymbol, decimals) + val parsedDecimal = newValue.parseBigDecimalOrNull() + val parsedValue = parsedDecimal?.toPlainString() ?: if (newValue.isBlank()) "" else oldValue + + val replacedWithSymbol = parsedValue.replaceDecimalSymbol(decimalSymbol) + val joinedSymbol = replacedWithSymbol.preserveDecimalSymbol(newValue, decimalSymbol) + val withPreservedZeros = joinedSymbol.preserveTrailingZeros(newValue, parsedDecimal, decimalSymbol) + decimalFormat.getValidatedNumberWithFixedDecimals(withPreservedZeros, decimals) } else { oldValue } } +private fun String.replaceDecimalSymbol(decimalSymbol: Char) = if (this.findLast { it != decimalSymbol } != null) { + when { + this.findLast { it == COMMA_SEPARATOR } != null -> { + this.replace(COMMA_SEPARATOR, decimalSymbol) + } + this.findLast { it == POINT_SEPARATOR } != null -> { + this.replace(POINT_SEPARATOR, decimalSymbol) + } + else -> this + } +} else { + this +} + +private fun String.preserveDecimalSymbol(newValue: String, decimalSymbol: Char) = if ( + newValue.endsWith(COMMA_SEPARATOR) || newValue.endsWith(POINT_SEPARATOR) +) { + this.plus(decimalSymbol) +} else { + this +} + +private fun String.preserveTrailingZeros(newValue: String, parsedDecimal: BigDecimal?, decimalSymbol: Char): String { + val trailingZeros = newValue.split(decimalSymbol).getOrNull(1)?.takeLastWhile { it == '0' }.orEmpty() + return if (parsedDecimal?.scale() == 0 && trailingZeros.isNotEmpty()) { + "$this$decimalSymbol$trailingZeros" + } else { + this.plus(trailingZeros) + } +} + private fun DecimalFormat.isValidSymbols(text: String): Boolean { return checkDecimalSeparatorDuplicate(text) }