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
20 changes: 19 additions & 1 deletion sample-app/shared/src/commonMain/kotlin/MainView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.publicvalue.multiplatform.qrcode.CameraPosition
import org.publicvalue.multiplatform.qrcode.CodeType
import org.publicvalue.multiplatform.qrcode.ScannerWithPermissions
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
import kotlin.time.TimeMark
import kotlin.time.TimeSource

@Composable
fun MainView() {
Expand All @@ -39,14 +44,27 @@ fun MainView() {
}) {
Text("Toggle scanner (visible: $scannerVisible)")
}
var lastCode by remember { mutableStateOf<String?>(null)}
var snackbarJob by remember { mutableStateOf<Job?>(null)}
var lastSnackbar by remember { mutableStateOf(TimeSource.Monotonic.markNow().minus(1.minutes))}
if (scannerVisible) {
val scope = rememberCoroutineScope()
ScannerWithPermissions(
modifier = Modifier.padding(16.dp),
onScanned = {
scope.launch {
if (lastCode == it) { return@ScannerWithPermissions false }
if (TimeSource.Monotonic.markNow().minus(lastSnackbar) < 1.seconds) {
return@ScannerWithPermissions false
}
snackbarJob?.cancel()
lastSnackbar = TimeSource.Monotonic.markNow()
snackbarJob = scope.launch {
snackbarHostState.showSnackbar(it, duration = SnackbarDuration.Short)
if (lastCode == it) {
lastCode = null
}
}
lastCode = it
false // continue scanning
},
types = listOf(CodeType.QR),
Expand Down
9 changes: 3 additions & 6 deletions scanner/src/iosMain/kotlin/ScannerView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ class ScannerCameraCoordinator(
}
}


fun setCurrentOrientation(newOrientation: UIDeviceOrientation) {
when(newOrientation) {
UIDeviceOrientation.UIDeviceOrientationLandscapeLeft ->
Expand All @@ -219,11 +218,9 @@ class ScannerCameraCoordinator(
}

fun onFound(code: String) {
captureSession.stopRunning()
if (!onScanned(code)) {
GlobalScope.launch(Dispatchers.Default) {
captureSession.startRunning()
}
val stopScanning = onScanned(code)
if (stopScanning) {
captureSession.stopRunning()
}
}

Expand Down