Open rungxanh1995 opened 2 months ago
At the moment, I'm using 0
as the fallback index to reformat the input text to not crash the app, as follow:
// PhoneNumberTransformation.kt
@Suppress("AvoidMutableCollections", "AvoidVarsExceptWithDelegate")
private fun reformat(s: CharSequence, cursor: Int): Transformation {
phoneNumberFormatter.clear()
val curIndex = cursor - 1
var formatted: String? = null
var lastNonSeparator = 0.toChar()
var hasCursor = false
s.forEachIndexed { index, char ->
if (PhoneNumberUtils.isNonSeparator(char)) {
if (lastNonSeparator.code != 0) {
formatted = getFormattedNumber(lastNonSeparator, hasCursor)
hasCursor = false
}
lastNonSeparator = char
}
if (index == curIndex) {
hasCursor = true
}
}
if (lastNonSeparator.code != 0) {
formatted = getFormattedNumber(lastNonSeparator, hasCursor)
}
val originalToTransformed = mutableListOf<Int>()
val transformedToOriginal = mutableListOf<Int>()
var specialCharsCount = 0
formatted?.forEachIndexed { index, char ->
if (!PhoneNumberUtils.isNonSeparator(char)) {
specialCharsCount++
} else {
originalToTransformed.add(index)
}
// transformedToOriginal.add(index - specialCharsCount) // Disabled this line
transformedToOriginal.add(maxOf(index - specialCharsCount, 0)) // -> Changed to this line
}
originalToTransformed.add(originalToTransformed.maxOrNull()?.plus(1) ?: 0)
transformedToOriginal.add(transformedToOriginal.maxOrNull()?.plus(1) ?: 0)
return Transformation(formatted, originalToTransformed, transformedToOriginal)
}
Describe the bug Recently, entering phone number to the text field is crashing the app.
For example: I want to enter a Canadian phone number
+14165550000
. The code will crash as soon as I've entered "4165550" into the text field.Important note: The code runs well on its own, without any extra dependencies or dependency version upgrades. Then I integrated the library to my project, updated dependency versions and made some necessary edits, it's when the issue arise. The visual transformation logic no longer works in a reliable manner, crashing the app as offset mapping is now flawed.
Refer to my Github fork commits to see the steps I made to edit visual transformation logic to ensure the app doesn't crash.
To Reproduce Steps to reproduce the behavior:
4165550
Expected behavior The app shouldn't have crashed, but instead handle visually transforming the input as before.
Smartphone (please complete the following information):
Additional context Refer to the stack trace error below