Thread
#compose
    a

    allan.conda

    2 years ago
    Anyone know how can I access compose samples?
    Timo Drick

    Timo Drick

    2 years ago
    I think the easiest way is to go to the documentation. There the samples are visible as code samples. Other way is to checkout the source. But yes would be nice when the samples are also accessible in android studio during development
    v

    Val Salamakha

    2 years ago
    class CreditCardVisualTransformation: VisualTransformation {
        override fun filter(text: AnnotatedString): TransformedText {
            val trimmed = if (text.text.length >= 16) text.text.substring(0..15) else text.text
            var out = ""
            for (i in trimmed.indices) {
                out += trimmed[i]
                if (i % 4 == 3 && i != 15) out += "-"
            }
    
            /**
             * The offset translator should ignore the hyphen characters, so conversion from
             *  original offset to transformed text works like
             *  - The 4th char of the original text is 5th char in the transformed text.
             *  - The 13th char of the original text is 15th char in the transformed text.
             *  Similarly, the reverse conversion works like
             *  - The 5th char of the transformed text is 4th char in the original text.
             *  - The 12th char of the transformed text is 10th char in the original text.
             */
    
            return TransformedText(AnnotatedString(out),
                object : OffsetMap {
                    override fun originalToTransformed(offset: Int): Int {
                        if (offset <= 3) return offset
                        if (offset <= 7) return offset + 1
                        if (offset <= 11) return offset + 2
                        if (offset <= 16) return offset + 3
                        return 19
                    }
    
                    override fun transformedToOriginal(offset: Int): Int {
                        if (offset <= 4) return offset
                        if (offset <= 9) return offset - 1
                        if (offset <= 14) return offset - 2
                        if (offset <= 19) return offset - 3
                        return 16
                    }
                })
        }
    }