Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ import kotlinx.coroutines.SupervisorJob
internal abstract class BaseInteractor(
val interactorScope: POCloseableCoroutineScope =
POCloseableCoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
)
) {

open fun clear() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.processout.sdk.core.POFailure.Code.Cancelled
import com.processout.sdk.core.POFailure.Code.Generic
import com.processout.sdk.core.ProcessOutResult
import com.processout.sdk.core.logger.POLogger
import com.processout.sdk.core.onFailure
import com.processout.sdk.core.onSuccess
import com.processout.sdk.ui.base.BaseInteractor
import com.processout.sdk.ui.card.scanner.CardScannerCompletion.*
import com.processout.sdk.ui.card.scanner.CardScannerEvent.*
Expand All @@ -23,7 +25,7 @@ internal class CardScannerInteractor(
) : BaseInteractor() {

private companion object {
const val INIT_DELAY_MS = 500L
const val INIT_DELAY_MS = 400L
}

private val _completion = MutableStateFlow<CardScannerCompletion>(Awaiting)
Expand All @@ -37,25 +39,33 @@ internal class CardScannerInteractor(

init {
POLogger.info("Starting card scanner.")
collectSessionState()
collectRecognizedCards()
interactorScope.launch {
delay(INIT_DELAY_MS)
_sideEffects.send(CameraPermissionRequest)
}
}

private fun initState() = CardScannerInteractorState(
loading = false,
isCameraPermissionGranted = false,
isTorchEnabled = false,
currentCard = null
)

private fun collectSessionState() {
interactorScope.launch {
cardRecognitionSession.isReady.collect { isReady ->
_state.update { it.copy(loading = !isReady) }
if (isReady) {
delay(INIT_DELAY_MS)
_sideEffects.send(CameraPermissionRequest)
}
}
}
}

fun onEvent(event: CardScannerEvent) {
when (event) {
is CameraPermissionResult -> handle(event)
is ImageAnalysis -> interactorScope.launch {
cardRecognitionSession.recognize(event.imageProxy)
}
is ImageAnalysis -> cardRecognitionSession.recognize(event.imageProxy)
is TorchToggle -> {
POLogger.debug("Torch toggle: ${event.isEnabled}.")
_state.update { it.copy(isTorchEnabled = event.isEnabled) }
Expand Down Expand Up @@ -91,8 +101,12 @@ internal class CardScannerInteractor(
}
}
interactorScope.launch(Dispatchers.Main.immediate) {
cardRecognitionSession.mostFrequentCard.collect { card ->
_completion.update { Success(card) }
cardRecognitionSession.result.collect { result ->
result.onSuccess { card ->
_completion.update { Success(card) }
}.onFailure { failure ->
cancel(failure)
}
}
}
}
Expand All @@ -101,4 +115,8 @@ internal class CardScannerInteractor(
POLogger.info("Cancelled: %s", failure)
_completion.update { Failure(failure) }
}

override fun clear() {
cardRecognitionSession.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.processout.sdk.ui.card.scanner
import com.processout.sdk.ui.card.scanner.recognition.POScannedCard

internal data class CardScannerInteractorState(
val loading: Boolean,
val isCameraPermissionGranted: Boolean,
val isTorchEnabled: Boolean,
val currentCard: POScannedCard?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,14 @@ private fun CameraPreview(
onRelease = { cameraController.unbind() }
)
} else {
Box(modifier.background(Color.Black))
Box(
modifier = modifier.background(Color.Black),
contentAlignment = Alignment.Center
) {
if (state.loading) {
POCircularProgressIndicator.Large(color = Color.White)
}
}
}
ScannedCard(
card = state.currentCard,
Expand Down Expand Up @@ -394,7 +401,8 @@ internal object CardScannerScreen {
width = border.widthDp.dp,
color = colorResource(id = border.colorResId)
),
overlayColor = colorResource(id = overlayColorResId)
overlayColor = overlayColorResId?.let { colorResource(id = it) }
?: defaultCameraPreview.overlayColor
)

private val defaultCard: CardStyle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal class CardScannerViewModel(
configuration = configuration,
interactor = CardScannerInteractor(
cardRecognitionSession = CardRecognitionSession(
app = app,
numberDetector = CardNumberDetector(),
expirationDetector = CardExpirationDetector(),
cardholderNameDetector = CardholderNameDetector(),
Expand All @@ -57,12 +58,13 @@ internal class CardScannerViewModel(
private fun map(state: CardScannerInteractorState) =
with(configuration) {
CardScannerViewModelState(
loading = state.loading,
isCameraPermissionGranted = state.isCameraPermissionGranted,
title = title ?: app.getString(R.string.po_card_scanner_title),
description = description ?: app.getString(R.string.po_card_scanner_description),
currentCard = state.currentCard,
torchAction = torchAction(state.isTorchEnabled),
cancelAction = cancelButton?.toAction(),
isCameraPermissionGranted = state.isCameraPermissionGranted
cancelAction = cancelButton?.toAction()
)
}

Expand Down Expand Up @@ -97,4 +99,8 @@ internal class CardScannerViewModel(
)
}
)

override fun onCleared() {
interactor.clear()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import com.processout.sdk.ui.card.scanner.recognition.POScannedCard
import com.processout.sdk.ui.core.state.POActionState

internal data class CardScannerViewModelState(
val loading: Boolean,
val isCameraPermissionGranted: Boolean,
val title: String,
val description: String,
val currentCard: POScannedCard?,
val torchAction: POActionState,
val cancelAction: POActionState?,
val isCameraPermissionGranted: Boolean
val cancelAction: POActionState?
)
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ data class POCardScannerConfiguration(
data class CameraPreviewStyle(
val border: POBorderStyle,
@ColorRes
val overlayColorResId: Int
val overlayColorResId: Int? = null
) : Parcelable

/**
Expand Down
Loading