This file contains 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
/** | |
* This method requires a LinearLayoutManager | |
*/ | |
val RecyclerView.visibleFlow: Flow<List<Int>> | |
get() = callbackFlow<List<Int>> { | |
val scrollListener = object : RecyclerView.OnScrollListener() { | |
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { | |
if (dx == 0 && dy == 0) { |
This file contains 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
private fun bindCardDetails(card: CardDetails) { | |
owner.text = card.owner | |
number.text = card.number | |
date.text = "${card.expirationMonth}/${card.expirationYear}" | |
} |
This file contains 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
private fun bindUseCases(cameraProvider: ProcessCameraProvider) { | |
val preview = buildPreview() | |
val takePicture = buildTakePicture() | |
val cameraSelector = buildCameraSelector() | |
cameraProvider.bindToLifecycle(this, cameraSelector, preview, takePicture) | |
button.setOnClickListener { | |
lifecycle.coroutineScope.launchWhenResumed { | |
val imageProxy = takePicture.takePicture(executor) |
This file contains 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
private val useCase = ExtractDataUseCase(TextRecognition.getClient()) |
This file contains 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
private fun extractExpiration(lines: List<String>): Pair<String?, String?> { | |
val expirationLine = extractExpirationLine(lines) | |
val month = expirationLine?.substring(startIndex = 0, endIndex = 2) | |
val year = expirationLine?.substring(startIndex = 3) | |
return Pair(month, year) | |
} |
This file contains 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
private fun extractExpirationLine(lines: List<String>) = | |
lines.flatMap { it.split(" ") } | |
.firstOrNull { (it.length == 5 || it.length == 7) && it[2] == '/' } |
This file contains 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
private fun extractNumber(lines: List<String>): String? { | |
return lines.firstOrNull { line -> | |
val subNumbers = line.split(" ") | |
subNumbers.isNotEmpty() && subNumbers.flatMap { it.asIterable() }.all { it.isDigit() } | |
} | |
} |
This file contains 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
private fun extractOwner(lines: List<String>): String? { | |
return lines | |
.filter { it.contains(" ") } | |
.filter { line -> line.asIterable().none { char -> char.isDigit() } } | |
.maxBy { it.length } | |
} |
This file contains 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
object Extractor { | |
fun extractData(input: String): CardDetails { | |
val lines = input.split("\n") | |
val owner = extractOwner(lines) | |
val number = extractNumber(lines) | |
val (month, year) = extractExpiration(lines) | |
return CardDetails( | |
owner = owner, | |
number = number, |
This file contains 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
data class CardDetails( | |
val owner: String?, | |
val number: String?, | |
val expirationMonth: String?, | |
val expirationYear: String? | |
) |
NewerOlder