mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-05-08 09:18:39 +01:00
Improve text entry for donations.
This commit is contained in:
+14
-3
@@ -209,10 +209,11 @@ data class Boost(
|
||||
* * '\uFF10' through '\uFF19', Fullwidth digits
|
||||
*/
|
||||
val digitsGroup: String = "[\\u0030-\\u0039]|[\\u0660-\\u0669]|[\\u06F0-\\u06F9]|[\\u0966-\\u096F]|[\\uFF10-\\uFF19]"
|
||||
val zeros: String = "\\u0030|\\u0660|\\u06F0|\\u0966|\\uFF10"
|
||||
|
||||
val pattern: Pattern = "($digitsGroup)*([$separator]){0,$separatorCount}($digitsGroup){0,${currency.defaultFractionDigits}}".toPattern()
|
||||
val symbolPattern: Regex = """\s*${Regex.escape(symbol)}\s*""".toRegex()
|
||||
val leadingZeroesPattern: Regex = """^0*""".toRegex()
|
||||
val leadingZeroesPattern: Regex = """^($zeros)*""".toRegex()
|
||||
|
||||
override fun filter(
|
||||
source: CharSequence,
|
||||
@@ -252,7 +253,13 @@ data class Boost(
|
||||
} else if (!hasSymbol) {
|
||||
val formatter = NumberFormat.getCurrencyInstance()
|
||||
formatter.currency = currency
|
||||
formatter.minimumFractionDigits = 0
|
||||
|
||||
if (s.contains(separator)) {
|
||||
formatter.minimumFractionDigits = s.split(separator).last().length
|
||||
} else {
|
||||
formatter.minimumFractionDigits = 0
|
||||
}
|
||||
|
||||
formatter.maximumFractionDigits = currency.defaultFractionDigits
|
||||
|
||||
val value = s.toString().toDoubleOrNull()
|
||||
@@ -278,10 +285,14 @@ data class Boost(
|
||||
val withoutLeadingZeroes: String = try {
|
||||
NumberFormat.getInstance().apply {
|
||||
isGroupingUsed = false
|
||||
|
||||
if (s.contains(separator)) {
|
||||
minimumFractionDigits = s.split(separator).last().length
|
||||
}
|
||||
}.format(withoutSymbol.toBigDecimal()) + (if (withoutSymbol.endsWith(separator)) separator else "")
|
||||
} catch (e: NumberFormatException) {
|
||||
withoutSymbol
|
||||
}.replace(leadingZeroesPattern, "")
|
||||
}
|
||||
|
||||
if (withoutSymbol != withoutLeadingZeroes) {
|
||||
text?.removeTextChangedListener(this)
|
||||
|
||||
+54
-2
@@ -59,7 +59,7 @@ class BoostTest__MoneyFilter {
|
||||
val editable = SpannableStringBuilder("5.00")
|
||||
testSubject.afterTextChanged(editable)
|
||||
|
||||
assertEquals("5", result)
|
||||
assertEquals("5.00", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -69,7 +69,7 @@ class BoostTest__MoneyFilter {
|
||||
|
||||
testSubject.afterTextChanged(editable)
|
||||
|
||||
assertEquals("$5", editable.toString())
|
||||
assertEquals("$5.00", editable.toString())
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -179,4 +179,56 @@ class BoostTest__MoneyFilter {
|
||||
|
||||
assertNull(filterResult)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Given USD, when I enter 1dot05, then I expect 1dot05`() {
|
||||
var result = ""
|
||||
val testSubject = Boost.MoneyFilter(usd) {
|
||||
result = it
|
||||
}
|
||||
|
||||
val editable = SpannableStringBuilder("$1.05")
|
||||
testSubject.afterTextChanged(editable)
|
||||
|
||||
assertEquals("1.05", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Given USD, when I enter 0dot05, then I expect 0dot05`() {
|
||||
var result = ""
|
||||
val testSubject = Boost.MoneyFilter(usd) {
|
||||
result = it
|
||||
}
|
||||
|
||||
val editable = SpannableStringBuilder("$0.05")
|
||||
testSubject.afterTextChanged(editable)
|
||||
|
||||
assertEquals("0.05", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Given USD, when I enter dot1, then I expect 0dot1`() {
|
||||
var result = ""
|
||||
val testSubject = Boost.MoneyFilter(usd) {
|
||||
result = it
|
||||
}
|
||||
|
||||
val editable = SpannableStringBuilder("$.1")
|
||||
testSubject.afterTextChanged(editable)
|
||||
|
||||
assertEquals("0.1", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Given USD, when I enter dot0, then I expect 0dot0`() {
|
||||
var result = ""
|
||||
val testSubject = Boost.MoneyFilter(usd) {
|
||||
result = it
|
||||
}
|
||||
|
||||
val editable = SpannableStringBuilder(".0")
|
||||
testSubject.afterTextChanged(editable)
|
||||
|
||||
assertEquals("0.0", result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user