Updated on 2026-08-14
This commit is contained in:
commit
0bfa06990d
3 changed files with 28 additions and 13 deletions
|
|
@ -10,9 +10,9 @@ import java.util.regex.Pattern
|
|||
class DecimalDigitsInputFilter(
|
||||
digitsBeforeDecimal: Int,
|
||||
digitsAfterDecimal: Int,
|
||||
decimalSeparator: String
|
||||
private val decimalSeparator: String
|
||||
) : InputFilter {
|
||||
private val pattern: Pattern = Pattern.compile("(([1-9]{1}[0-9]{0,${digitsBeforeDecimal -1}})?||[0]{1})((\\$decimalSeparator[0-9]{0,$digitsAfterDecimal})?)||(\\$decimalSeparator)?")
|
||||
private val pattern: Pattern = Pattern.compile("(([1-9]{1}[0-9]{0,${digitsBeforeDecimal - 1}})?||[0]{1})((\\$decimalSeparator[0-9]{0,$digitsAfterDecimal})?)||(\\$decimalSeparator)?")
|
||||
|
||||
override fun filter(source: CharSequence, sourceStart: Int, sourceEnd: Int, destination: Spanned, destinationStart: Int, destinationEnd: Int): CharSequence? {
|
||||
val destString = destination.toString()
|
||||
|
|
@ -24,7 +24,27 @@ class DecimalDigitsInputFilter(
|
|||
val resultSuffix = newDestination.substring(destinationStart, newDestination.length)
|
||||
val result = resultPrefix + source.toString() + resultSuffix
|
||||
|
||||
val hasMatches = pattern.matcher(result).matches()
|
||||
return if (hasMatches) null else ""
|
||||
return if (pattern.matcher(result).matches()) {
|
||||
null
|
||||
} else {
|
||||
val replacedWithAppropriateDecimalSeparator = setDecimalSeparator(result, decimalSeparator)
|
||||
if (pattern.matcher(replacedWithAppropriateDecimalSeparator).matches()) {
|
||||
decimalSeparator
|
||||
} else {
|
||||
""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun setDecimalSeparator(value: String, decimalSeparator: String): String {
|
||||
if (value.contains(decimalSeparator)) return value
|
||||
|
||||
return if (decimalSeparator == ".") {
|
||||
value.replace(",", decimalSeparator)
|
||||
} else {
|
||||
value.replace(".", decimalSeparator)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import com.tangem.blockchain.common.WalletManager
|
|||
import com.tangem.common.extensions.isZero
|
||||
import com.tangem.tap.common.CurrencyConverter
|
||||
import com.tangem.tap.common.entities.TapCurrency
|
||||
import com.tangem.tap.common.text.DecimalDigitsInputFilter
|
||||
import com.tangem.tap.domain.TapError
|
||||
import com.tangem.tap.store
|
||||
import org.rekotlin.StateType
|
||||
|
|
@ -143,13 +144,7 @@ data class AmountState(
|
|||
}
|
||||
|
||||
fun restoreDecimalSeparator(value: String): String {
|
||||
if (value.contains(decimalSeparator)) return value
|
||||
|
||||
return if (decimalSeparator == ".") {
|
||||
value.replace(",", decimalSeparator)
|
||||
} else {
|
||||
value.replace(".", decimalSeparator)
|
||||
}
|
||||
return DecimalDigitsInputFilter.setDecimalSeparator(value, decimalSeparator)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -144,7 +144,7 @@ class SendFragment : BaseStoreFragment(R.layout.fragment_send) {
|
|||
}
|
||||
}
|
||||
|
||||
etAmountToSend.keyListener = DigitsKeyListener.getInstance("0123456789$decimalSeparator")
|
||||
etAmountToSend.keyListener = DigitsKeyListener.getInstance("0123456789,.")
|
||||
etAmountToSend.setOnFocusChangeListener { v, hasFocus ->
|
||||
snackbarControlledByChangingFocus = true
|
||||
if (hasFocus) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue