43 lines
1.4 KiB
Kotlin
43 lines
1.4 KiB
Kotlin
package com.bitcointxoko.gudariwallet.ui
|
|
|
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.LaunchedEffect
|
|
import com.journeyapps.barcodescanner.ScanContract
|
|
import com.journeyapps.barcodescanner.ScanOptions
|
|
|
|
/**
|
|
* Launches the zxing-android-embedded scanner activity and calls [onScanned]
|
|
* with the raw result string, or [onDismiss] if the user cancels.
|
|
*
|
|
* Camera permission is handled internally by the scanner activity — no
|
|
* Accompanist boilerplate needed here.
|
|
*/
|
|
@Composable
|
|
fun QrScannerScreen(
|
|
onScanned: (String) -> Unit,
|
|
onDismiss: () -> Unit
|
|
) {
|
|
val launcher = rememberLauncherForActivityResult(ScanContract()) { result ->
|
|
val content = result.contents
|
|
if (content != null) {
|
|
onScanned(content)
|
|
} else {
|
|
onDismiss()
|
|
}
|
|
}
|
|
|
|
// Launch the scanner as soon as this composable enters the composition.
|
|
// The scanner activity takes over the screen; when it finishes the result
|
|
// callback above fires and we navigate away.
|
|
LaunchedEffect(Unit) {
|
|
val options = ScanOptions().apply {
|
|
setDesiredBarcodeFormats(ScanOptions.QR_CODE)
|
|
setPrompt("") // no bottom prompt text
|
|
setBeepEnabled(false) // wallet apps are usually silent
|
|
setOrientationLocked(true)
|
|
}
|
|
launcher.launch(options)
|
|
}
|
|
}
|