@@ -3,10 +3,6 @@ package org.wordpress.android.ui.barcodescanner
3
3
import androidx.camera.core.ImageProxy
4
4
import com.google.mlkit.vision.barcode.BarcodeScanner
5
5
import com.google.mlkit.vision.barcode.common.Barcode
6
- import kotlinx.coroutines.channels.ProducerScope
7
- import kotlinx.coroutines.channels.awaitClose
8
- import kotlinx.coroutines.flow.Flow
9
- import kotlinx.coroutines.flow.callbackFlow
10
6
import javax.inject.Inject
11
7
12
8
class GoogleMLKitCodeScanner @Inject constructor(
@@ -17,53 +13,41 @@ class GoogleMLKitCodeScanner @Inject constructor(
17
13
) : CodeScanner {
18
14
private var barcodeFound = false
19
15
@androidx.camera.core.ExperimentalGetImage
20
- override fun startScan (imageProxy : ImageProxy ): Flow <CodeScannerStatus > {
21
- return callbackFlow {
22
- val barcodeTask = barcodeScanner.process(inputImageProvider.provideImage(imageProxy))
23
- barcodeTask.addOnCompleteListener {
24
- // We must call image.close() on received images when finished using them.
25
- // Otherwise, new images may not be received or the camera may stall.
26
- imageProxy.close()
27
- }
28
- barcodeTask.addOnSuccessListener { barcodeList ->
29
- // The check for barcodeFound is done because the startScan method will be called
30
- // continuously by the library as long as we are in the scanning screen.
31
- // There will be a good chance that the same barcode gets identified multiple times and as a result
32
- // success callback will be called multiple times.
33
- if (! barcodeList.isNullOrEmpty() && ! barcodeFound) {
34
- barcodeFound = true
35
- handleScanSuccess(barcodeList.firstOrNull())
36
- this @callbackFlow.close()
37
- }
38
- }
39
- barcodeTask.addOnFailureListener { exception ->
40
- this @callbackFlow.trySend(
41
- CodeScannerStatus .Failure (
42
- error = exception.message,
43
- type = errorMapper.mapGoogleMLKitScanningErrors(exception)
44
- )
45
- )
46
- this @callbackFlow.close()
16
+ override fun startScan (imageProxy : ImageProxy , callback : CodeScannerCallback ) {
17
+ val barcodeTask = barcodeScanner.process(inputImageProvider.provideImage(imageProxy))
18
+ barcodeTask.addOnCompleteListener {
19
+ // We must call image.close() on received images when finished using them.
20
+ // Otherwise, new images may not be received or the camera may stall.
21
+ imageProxy.close()
22
+ }
23
+ barcodeTask.addOnSuccessListener { barcodeList ->
24
+ // The check for barcodeFound is done because the startScan method will be called
25
+ // continuously by the library as long as we are in the scanning screen.
26
+ // There will be a good chance that the same barcode gets identified multiple times and as a result
27
+ // success callback will be called multiple times.
28
+ if (! barcodeList.isNullOrEmpty() && ! barcodeFound) {
29
+ barcodeFound = true
30
+ callback.run (handleScanSuccess(barcodeList.firstOrNull()))
47
31
}
48
-
49
- awaitClose()
32
+ }
33
+ barcodeTask.addOnFailureListener { exception ->
34
+ callback.run (CodeScannerStatus .Failure (
35
+ error = exception.message,
36
+ type = errorMapper.mapGoogleMLKitScanningErrors(exception)
37
+ ))
50
38
}
51
39
}
52
40
53
- private fun ProducerScope<CodeScannerStatus>.handleScanSuccess (code : Barcode ? ) {
54
- code?.rawValue?.let {
55
- trySend(
56
- CodeScannerStatus .Success (
57
- it,
58
- barcodeFormatMapper.mapBarcodeFormat(code.format)
59
- )
41
+ private fun handleScanSuccess (code : Barcode ? ): CodeScannerStatus {
42
+ return code?.rawValue?.let {
43
+ CodeScannerStatus .Success (
44
+ it,
45
+ barcodeFormatMapper.mapBarcodeFormat(code.format)
60
46
)
61
47
} ? : run {
62
- trySend(
63
- CodeScannerStatus .Failure (
64
- error = " Failed to find a valid raw value!" ,
65
- type = CodeScanningErrorType .Other (Throwable (" Empty raw value" ))
66
- )
48
+ CodeScannerStatus .Failure (
49
+ error = " Failed to find a valid raw value!" ,
50
+ type = CodeScanningErrorType .Other (Throwable (" Empty raw value" ))
67
51
)
68
52
}
69
53
}
0 commit comments