Updated on 2026-08-14

This commit is contained in:
Tangem 2020-09-14 17:02:46 +03:00
parent 00400a72dd
commit d9f6b0576e
4 changed files with 36 additions and 0 deletions

View file

@ -0,0 +1,26 @@
package com.tangem.tap.common.text
import android.text.InputFilter
import android.text.Spanned
import java.util.regex.Pattern
/**
[REDACTED_AUTHOR]
*/
class DecimalDigitsInputFilter(digitsBeforeDecimal: Int, digitsAfterDecimal: Int) : InputFilter {
private val pattern: Pattern = Pattern.compile("(([1-9]{1}[0-9]{0," + (digitsBeforeDecimal - 1) + "})?||[0]{1})((\\.[0-9]{0," + digitsAfterDecimal + "})?)||(\\.)?")
override fun filter(source: CharSequence, sourceStart: Int, sourceEnd: Int, destination: Spanned, destinationStart: Int, destinationEnd: Int): CharSequence? {
val destString = destination.toString()
val prefix = destString.substring(0, destinationStart)
val suffix = destString.substring(destinationEnd, destString.length)
val newDestination = prefix + suffix
val resultPrefix = newDestination.substring(0, destinationStart)
val resultSuffix = newDestination.substring(destinationStart, newDestination.length)
val result = resultPrefix + source.toString() + resultSuffix
val hasMatches = pattern.matcher(result).matches()
return if (hasMatches) null else ""
}
}