From 87182c8ad90a1e1a3f6b3bdeaf791479c8c0c846 Mon Sep 17 00:00:00 2001 From: Alexandre Jacinto Date: Mon, 13 Nov 2023 11:56:21 +0000 Subject: [PATCH] feat: use camera direction to decide camera facing Context: Instead of passing each field of OSBARCScanParameters in an extra through the intent, we might as well pass the whole object. To do that, it has to be made a Serializable. References: https://outsystemsrd.atlassian.net/browse/RMET-2764 --- .../barcode/controller/OSBARCController.kt | 23 +++++-------------- .../barcode/model/OSBARCScanParameters.kt | 4 +++- .../barcode/view/OSBARCScannerActivity.kt | 13 +++++++---- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/main/kotlin/com/outsystems/plugins/barcode/controller/OSBARCController.kt b/src/main/kotlin/com/outsystems/plugins/barcode/controller/OSBARCController.kt index bf30f12..d4ec498 100644 --- a/src/main/kotlin/com/outsystems/plugins/barcode/controller/OSBARCController.kt +++ b/src/main/kotlin/com/outsystems/plugins/barcode/controller/OSBARCController.kt @@ -15,13 +15,7 @@ class OSBARCController { companion object { private const val SCAN_REQUEST_CODE = 112 - private const val SCAN_INSTRUCTIONS = "SCAN_INSTRUCTIONS" - private const val CAMERA_DIRECTION = "CAMERA_DIRECTION" - private const val SCAN_ORIENTATION = "SCAN_ORIENTATION" - private const val SCAN_BUTTON = "SCAN_BUTTON" - private const val SCAN_BUTTON_TEXT = "SCAN_BUTTON_TEXT" - private const val SCAN_HINT = "SCAN_HINT" - private const val SCAN_LIBRARY = "SCAN_LIBRARY" + private const val SCAN_PARAMETERS = "SCAN_PARAMETERS" private const val SCAN_RESULT = "scanResult" private const val LOG_TAG = "OSBARCController" } @@ -33,16 +27,11 @@ class OSBARCController { * @param parameters - object that contains all the barcode parameters to be used when scanning. */ fun scanCode(activity: Activity, parameters: OSBARCScanParameters) { - val scanningIntent = Intent(activity, OSBARCScannerActivity::class.java).apply { - putExtra(SCAN_INSTRUCTIONS, parameters.scanInstructions) - putExtra(CAMERA_DIRECTION, parameters.cameraDirection) - putExtra(SCAN_ORIENTATION, parameters.scanOrientation) - putExtra(SCAN_BUTTON, parameters.scanButton) - putExtra(SCAN_BUTTON_TEXT, parameters.scanText) - putExtra(SCAN_HINT, parameters.hint) - putExtra(SCAN_LIBRARY, parameters.androidScanningLibrary) - } - activity.startActivityForResult(scanningIntent, SCAN_REQUEST_CODE) + activity.startActivityForResult( + Intent( + activity, OSBARCScannerActivity::class.java + ).putExtra(SCAN_PARAMETERS, parameters), SCAN_REQUEST_CODE + ) } /** diff --git a/src/main/kotlin/com/outsystems/plugins/barcode/model/OSBARCScanParameters.kt b/src/main/kotlin/com/outsystems/plugins/barcode/model/OSBARCScanParameters.kt index a5dc9a1..ed0244d 100644 --- a/src/main/kotlin/com/outsystems/plugins/barcode/model/OSBARCScanParameters.kt +++ b/src/main/kotlin/com/outsystems/plugins/barcode/model/OSBARCScanParameters.kt @@ -1,5 +1,7 @@ package com.outsystems.plugins.barcode.model +import java.io.Serializable + /** * Data class that represents the object with the scan parameters. */ @@ -11,4 +13,4 @@ data class OSBARCScanParameters( val scanText: String?, val hint: Int?, val androidScanningLibrary: String? -) \ No newline at end of file +) : Serializable \ No newline at end of file diff --git a/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt b/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt index 0849bf7..964dfec 100644 --- a/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt +++ b/src/main/kotlin/com/outsystems/plugins/barcode/view/OSBARCScannerActivity.kt @@ -32,6 +32,7 @@ import com.outsystems.plugins.barcode.controller.OSBARCScanLibraryFactory import com.outsystems.plugins.barcode.controller.helper.OSBARCMLKitHelper import com.outsystems.plugins.barcode.controller.helper.OSBARCZXingHelper import com.outsystems.plugins.barcode.model.OSBARCError +import com.outsystems.plugins.barcode.model.OSBARCScanParameters import com.outsystems.plugins.barcode.view.ui.theme.BarcodeScannerTheme import java.lang.Exception @@ -46,9 +47,11 @@ class OSBARCScannerActivity : ComponentActivity() { private const val SCAN_SUCCESS_RESULT_CODE = -1 private const val SCAN_RESULT = "scanResult" private const val LOG_TAG = "OSBARCScannerActivity" - private const val SCAN_LIBRARY = "SCAN_LIBRARY" + private const val SCAN_PARAMETERS = "SCAN_PARAMETERS" } + private lateinit var parameters: OSBARCScanParameters + /** * Overrides the onCreate method from Activity, setting the UI of the screen */ @@ -57,7 +60,7 @@ class OSBARCScannerActivity : ComponentActivity() { setContent { BarcodeScannerTheme { - ScanScreen() + ScanScreen(intent.extras?.getSerializable(SCAN_PARAMETERS) as OSBARCScanParameters) } } } @@ -67,7 +70,7 @@ class OSBARCScannerActivity : ComponentActivity() { * as well as creating an instance of OSBARCBarcodeAnalyzer for image analysis. */ @Composable - fun ScanScreen() { + fun ScanScreen(parameters: OSBARCScanParameters) { val lifecycleOwner = LocalLifecycleOwner.current val context = LocalContext.current @@ -102,7 +105,7 @@ class OSBARCScannerActivity : ComponentActivity() { val previewView = PreviewView(context) val preview = Preview.Builder().build() val selector = CameraSelector.Builder() - .requireLensFacing(CameraSelector.LENS_FACING_BACK) // temporary + .requireLensFacing(if (parameters.cameraDirection == 2) CameraSelector.LENS_FACING_FRONT else CameraSelector.LENS_FACING_BACK) .build() preview.setSurfaceProvider(previewView.surfaceProvider) val imageAnalysis = ImageAnalysis.Builder() @@ -112,7 +115,7 @@ class OSBARCScannerActivity : ComponentActivity() { ContextCompat.getMainExecutor(context), OSBARCBarcodeAnalyzer( OSBARCScanLibraryFactory.createScanLibraryWrapper( - intent.extras?.getString(SCAN_LIBRARY) ?: "", + parameters.androidScanningLibrary ?: "", OSBARCZXingHelper(), OSBARCMLKitHelper() ), // temporary