TP-59496 fix index out of bound exception in number comma transformation (#383)

This commit is contained in:
Hitesh Kumar
2024-03-11 19:35:09 +05:30
committed by GitHub
parent f388bf2a1a
commit 0324859b6f

View File

@@ -11,6 +11,7 @@ import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.input.OffsetMapping
import androidx.compose.ui.text.input.TransformedText
import androidx.compose.ui.text.input.VisualTransformation
import com.navi.uitron.utils.EMPTY
import com.navi.uitron.utils.toDoubleWithSafe
class NumberCommaTransformation : VisualTransformation {
@@ -19,14 +20,18 @@ class NumberCommaTransformation : VisualTransformation {
val transformation = reformat(text.text)
return TransformedText(
AnnotatedString(transformation.formatted ?: ""),
AnnotatedString(transformation.formatted ?: EMPTY),
object : OffsetMapping {
override fun originalToTransformed(offset: Int): Int {
return transformation.originalToTransformed[offset]
return transformation.originalToTransformed.getOrNull(offset)
?: transformation.formatted?.length
?: 0
}
override fun transformedToOriginal(offset: Int): Int {
return transformation.transformedToOriginal[offset]
return transformation.transformedToOriginal.getOrNull(offset)
?: transformation.formatted?.length
?: 0
}
}
)