This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Depends on: | |
| "com.google.zxing:core" | |
| "com.journeyapps:zxing-android-embedded" | |
| "com.google.accompanist:accompanist-permissions" | |
| @Composable | |
| fun BarcodeScanner( | |
| label: String = "Ler código", | |
| onDetectBarcode: (barcodes: List<String>) -> Unit, | |
| onManualInputClick: () -> Unit = {}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface OrientationLocker { | |
| fun lockToOrientation(orientation: ScreenOrientation) | |
| } | |
| enum class ScreenOrientation { | |
| PORTRAIT, | |
| LANDSCAPE, | |
| AUTO, | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun OnLifecycleEvent(block: (owner: LifecycleOwner, event: Lifecycle.Event) -> Unit) { | |
| val eventHandler = rememberUpdatedState(block) | |
| val lifecycleOwner = rememberUpdatedState(LocalLifecycleOwner.current) | |
| DisposableEffect(lifecycleOwner.value) { | |
| val lifecycle = lifecycleOwner.value.lifecycle | |
| val observer = LifecycleEventObserver { owner, event -> | |
| eventHandler.value(owner, event) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const val RAW_CPF_LENGTH = 11 | |
| const val RAW_CNPJ_LENGTH = 14 | |
| fun String.formatCPF(): String { | |
| val digits = this.filter { it.isDigit() } | |
| if (digits.length != RAW_CPF_LENGTH) return this // invalid. | |
| return buildString { | |
| append(digits.substring(0 .. 2)) | |
| append(".") | |
| append(digits.substring(3 .. 5)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Phone(val rawPhone: String) { | |
| val countryCode: String | |
| val regionCode: String | |
| val number: String | |
| init { | |
| when (rawPhone.length) { | |
| COMPLETE_CELLPHONE_LENGTH -> { | |
| countryCode = rawPhone.take(2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const val MAJORITY_AGE = 18 | |
| val isRequiredMajority = true | |
| val yearRange = Calendar.getInstance() | |
| .run { get(Calendar.YEAR) } | |
| .let { currentYear -> | |
| val firstYear = currentYear - 100 | |
| val lastYear = if (isRequiredMajority) currentYear - MAJORITY_AGE else currentYear | |
| firstYear..lastYear |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const val CENTS_TO_REALS_POINTS = 2 | |
| fun Int.centsToReals(): BigDecimal = BigDecimal(this).movePointLeft(CENTS_TO_REALS_POINTS) | |
| fun BigDecimal.realsToCents(): Int = movePointRight(CENTS_TO_REALS_POINTS).intValueExact() | |
| val localeBR = Locale("pt", "BR") | |
| val blrFormatter = NumberFormat.getCurrencyInstance(localeBR) | |
| fun Int.formatBlr() = blrFormatter.format(centsToReals()) | |
| fun BigDecimal.formatBlr() = blrFormatter.format(this) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const val RAW_PHONE_LENGTH = 11 | |
| @Composable | |
| fun ComposeField() { | |
| var phone by remember { mutableStateOf("") } | |
| val mask = remember { mask("(##) #####-####") } // See 'Masquerade.kt' Gist. | |
| OutlinedTextField( | |
| value = phone, | |
| keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), | |
| placeholder = { Text(mask.pattern) }, |
NewerOlder