Home > database >  Compose VisualTransformation PhoneNumber separate each 3 numbers by space
Compose VisualTransformation PhoneNumber separate each 3 numbers by space

Time:05-01

Am trying to make a visual transformation in TextField composable

Example 01111111111 -> 011 111 111 11

Note: I want to append space after each 3 numbers for any length of numbers

I Figured a formula to calculate the offset of original to transformed but I don't can't figure how to reverse that

val phoneNumberOffsetTranslator = object : OffsetMapping {
    override fun originalToTransformed(offset: Int): Int {
        return (i % 3)   (4 * (i.div(3)))
    }

    override fun transformedToOriginal(offset: Int): Int {
        // I couldn't figure a formula to do that
        return TODO()
    }
}

Example outputs of the formula in originalToTransformed()

0 -> 0
1 -> 1
2 -> 2

3 -> 4
4 -> 5
5 -> 6

6 -> 8
7 -> 9
8 -> 10
9 -> 12
10 -> 13

CodePudding user response:

It's pretty simple:

override fun transformedToOriginal(offset: Int): Int {
    return i - i.div(4)
}
  • Related