diff --git a/composeApp/src/androidMain/kotlin/net/codinux/banking/ui/service/QrCodeService.android.kt b/composeApp/src/androidMain/kotlin/net/codinux/banking/ui/service/QrCodeService.android.kt index 3d84357..9982a4c 100644 --- a/composeApp/src/androidMain/kotlin/net/codinux/banking/ui/service/QrCodeService.android.kt +++ b/composeApp/src/androidMain/kotlin/net/codinux/banking/ui/service/QrCodeService.android.kt @@ -16,7 +16,6 @@ import com.google.zxing.common.HybridBinarizer import com.google.zxing.qrcode.QRCodeReader import net.codinux.banking.persistence.AndroidContext import net.codinux.banking.ui.MainActivity -import net.codinux.banking.ui.config.DI import net.codinux.log.logger import java.nio.ByteBuffer import java.util.concurrent.Executors @@ -41,12 +40,20 @@ actual object QrCodeService { return // we don't have the permission to start the camera } - val cameraProviderFuture = ProcessCameraProvider.getInstance(mainActivity) - val previewView = remember { PreviewView(mainActivity) } + setupCameraView(previewView, mainActivity, resultCallback) + + + AndroidView(factory = { previewView }, modifier = Modifier.fillMaxSize()) + } + + @Composable + private fun setupCameraView(previewView: PreviewView, mainActivity: MainActivity, resultCallback: (QrCodeReadResult) -> Unit) { + val cameraProviderFuture = ProcessCameraProvider.getInstance(mainActivity) + cameraProviderFuture.addListener({ // Used to bind the lifecycle of cameras to the lifecycle owner val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get() @@ -74,14 +81,11 @@ actual object QrCodeService { // Bind use cases to camera cameraProvider.bindToLifecycle(mainActivity, cameraSelector, preview, imageAnalyzer) - } catch(e: Exception) { + } catch (e: Exception) { log.error(e) { "Use case binding failed" } } }, ContextCompat.getMainExecutor(mainActivity)) - - - AndroidView(factory = { previewView }, modifier = Modifier.fillMaxSize()) } }