Skip to content

Instantly share code, notes, and snippets.

View Oleur's full-sized avatar
👨‍💻
Doing stuff with the droid!

Julien Salvi Oleur

👨‍💻
Doing stuff with the droid!
View GitHub Profile
@Oleur
Oleur / PhoneNumberVisualTransformation.kt
Last active December 20, 2021 16:41
Compose offsets for PhoneNumber VisualTransformation
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)
@Oleur
Oleur / PhoneNumberVisualTransformation.kt
Created December 20, 2021 15:43
Phone number formatting
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
@Oleur
Oleur / PhoneNumberFormatter.kt
Created December 16, 2021 16:23
Format Phone Number while typing with libphonenumber library
private val countryCode: String = Locale.getDefault().country
private val phoneNumberFormatter = PhoneNumberUtil.getInstance().getAsYouTypeFormatter(countryCode)
private fun getFormattedNumber(lastNonSeparator: Char, hasCursor: Boolean): String? {
return if (hasCursor) {
phoneNumberFormatter.inputDigitAndRememberPosition(lastNonSeparator)
} else {
phoneNumberFormatter.inputDigit(lastNonSeparator)
}
}
TextField(
modifier = modifier.fillMaxWidth(),
text = text,
hint = hint,
singleLine = true,
leadingIcon = null,
visualTransformation = PhoneNumberVisualTransformation(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone)
)
@Oleur
Oleur / CreditCardOffsetMapping.kt
Created December 13, 2021 18:43
Credit Card transformation offset mapping from Compose documentation.
// Making XXXX-XXXX-XXXX-XXXX string.
val creditCardOffsetMapping = object : OffsetMapping {
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
}
@Oleur
Oleur / PasswordVisualTransformation.kt
Created December 13, 2021 18:33
PasswordVisualTransformation from Jetpack Compose
class PasswordVisualTransformation(val mask: Char = '\u2022') : VisualTransformation {
override fun filter(text: AnnotatedString): TransformedText {
return TransformedText(
text = AnnotatedString(mask.toString().repeat(text.text.length)),
offsetMapping = object : OffsetMapping {
override fun originalToTransformed(offset: Int): Int = offset
override fun transformedToOriginal(offset: Int): Int = offset
}
@Oleur
Oleur / VisualTransformation.kt
Created December 13, 2021 17:53
VisualTransformation interface from Android sources
fun interface VisualTransformation {
fun filter(text: AnnotatedString): TransformedText
companion object {
@Stable
val None: VisualTransformation = VisualTransformation { text ->
TransformedText(text, OffsetMapping.Identity)
}
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
grantAssistantPermissions()
}
private fun grantAssistantPermissions() {
getAssistantPackage()?.let { assistantPackage ->
val sliceProviderUri = Uri.Builder()
class TodoProvider : SliceProvider() {
override fun onCreateSliceProvider() = true
override fun onBindSlice(sliceUri: Uri): Slice? {
val context = context ?: return null
val activityAction = createActivityAction() ?: return null
return if (sliceUri.path == "/todo") {
list(context, sliceUri, ListBuilder.INFINITY) {
header {
@Oleur
Oleur / AppActionIntent.kt
Created June 2, 2021 19:37
Handle deeplink
class MyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
intent.handleIntent()
}
}
private fun Intent.handleIntent() {
when (action) {
Intent.ACTION_VIEW -> controller.handleDeepLink(data)