Skip to content

Commit a5d6c7b

Browse files
committed
Small refactorings
1 parent d7c9f22 commit a5d6c7b

4 files changed

Lines changed: 37 additions & 14 deletions

File tree

quickie/src/main/kotlin/io/github/g00fy2/quickie/QRScannerActivity.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package io.github.g00fy2.quickie
22

33
import android.Manifest.permission.CAMERA
44
import android.app.Activity
5-
import android.app.Dialog
65
import android.content.Intent
76
import android.content.pm.PackageManager
87
import android.os.Bundle
98
import android.util.Size
109
import android.view.HapticFeedbackConstants
11-
import android.view.KeyEvent
1210
import android.view.View
1311
import androidx.activity.result.contract.ActivityResultContracts
1412
import androidx.appcompat.app.AlertDialog
@@ -28,11 +26,11 @@ import com.google.mlkit.vision.barcode.common.Barcode
2826
import io.github.g00fy2.quickie.config.ParcelableScannerConfig
2927
import io.github.g00fy2.quickie.config.ScannerAction
3028
import io.github.g00fy2.quickie.config.ScannerConfig
29+
import io.github.g00fy2.quickie.config.ScannerSuccessActionProvider
3130
import io.github.g00fy2.quickie.content.QRContent
3231
import io.github.g00fy2.quickie.databinding.QuickieScannerActivityBinding
3332
import io.github.g00fy2.quickie.extensions.toParcelableContentType
3433
import io.github.g00fy2.quickie.utils.MlKitErrorHandler
35-
import kotlinx.coroutines.CoroutineScope
3634
import kotlinx.coroutines.launch
3735
import java.util.concurrent.ExecutorService
3836
import java.util.concurrent.Executors
@@ -46,7 +44,7 @@ internal class QRScannerActivity : AppCompatActivity() {
4644
private var showTorchToggle = false
4745
private var showCloseButton = false
4846
private var useFrontCamera = false
49-
private var scannerSuccessActionProvider: (suspend CoroutineScope.(QRContent) -> ScannerAction)? = null
47+
private var scannerSuccessActionProvider: ScannerSuccessActionProvider? = null
5048
private var analysisPaused = false
5149

5250
override fun onCreate(savedInstanceState: Bundle?) {
@@ -103,7 +101,6 @@ internal class QRScannerActivity : AppCompatActivity() {
103101
QRCodeAnalyzer(
104102
barcodeFormats = barcodeFormats,
105103
onSuccess = { barcode ->
106-
// it.clearAnalyzer()
107104
if (!analysisPaused) {
108105
analysisPaused = true
109106
onSuccess(barcode)
@@ -217,8 +214,9 @@ internal class QRScannerActivity : AppCompatActivity() {
217214
showTorchToggle = it.showTorchToggle
218215
useFrontCamera = it.useFrontCamera
219216
showCloseButton = it.showCloseButton
220-
scannerSuccessActionProvider = ScannerConfig.scannerSuccessActionProvider
221217
}
218+
219+
scannerSuccessActionProvider = ScannerConfig.scannerSuccessActionProvider
222220
}
223221

224222
private fun requestCameraPermissionIfMissing(onResult: ((Boolean) -> Unit)) {

quickie/src/main/kotlin/io/github/g00fy2/quickie/ScanQRCode.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ import io.github.g00fy2.quickie.QRResult.QRSuccess
1111
import io.github.g00fy2.quickie.QRResult.QRUserCanceled
1212
import io.github.g00fy2.quickie.QRScannerActivity.Companion.RESULT_ERROR
1313
import io.github.g00fy2.quickie.QRScannerActivity.Companion.RESULT_MISSING_PERMISSION
14+
import io.github.g00fy2.quickie.config.ScannerConfig
15+
import io.github.g00fy2.quickie.config.ScannerSuccessActionProvider
1416
import io.github.g00fy2.quickie.extensions.getRootException
1517
import io.github.g00fy2.quickie.extensions.toQuickieContentType
1618

17-
public class ScanQRCode : ActivityResultContract<Nothing?, QRResult>() {
19+
public class ScanQRCode : ActivityResultContract<ScannerSuccessActionProvider?, QRResult>() {
1820

19-
override fun createIntent(context: Context, input: Nothing?): Intent =
20-
Intent(context, QRScannerActivity::class.java)
21+
override fun createIntent(context: Context, input: ScannerSuccessActionProvider?): Intent {
22+
ScannerConfig.scannerSuccessActionProvider = input
23+
return Intent(context, QRScannerActivity::class.java)
24+
}
2125

2226
override fun parseResult(resultCode: Int, intent: Intent?): QRResult {
2327
return when (resultCode) {

quickie/src/main/kotlin/io/github/g00fy2/quickie/config/ScannerConfig.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public class ScannerConfig internal constructor(
8484
/**
8585
* Set a listener to be called when a barcode is detected.
8686
*/
87-
public fun setScannerSuccessActionProvider(listener: suspend CoroutineScope.(QRContent) -> ScannerAction): Builder =
87+
public fun setScannerSuccessActionProvider(listener: ScannerSuccessActionProvider): Builder =
8888
apply { scannerSuccessActionProvider = listener }
8989

9090
/**
@@ -109,6 +109,8 @@ public class ScannerConfig internal constructor(
109109
*/
110110
public fun build(func: Builder.() -> Unit): ScannerConfig = Builder().apply { func() }.build()
111111

112-
internal var scannerSuccessActionProvider: (suspend CoroutineScope.(QRContent) -> ScannerAction)? = null
112+
internal var scannerSuccessActionProvider: ScannerSuccessActionProvider? = null
113113
}
114-
}
114+
}
115+
116+
public typealias ScannerSuccessActionProvider = suspend CoroutineScope.(QRContent) -> ScannerAction

sample/src/main/kotlin/io/github/g00fy2/quickiesample/MainActivity.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,34 @@ class MainActivity : AppCompatActivity() {
3636
setContentView(binding.root)
3737
setBarcodeFormatDropdown()
3838

39+
3940
binding.qrScannerButton.setOnClickListener {
40-
scanQrCode.launch(null)
41+
var count = 0
42+
scanQrCode.launch() {
43+
delay(1000)
44+
if (it.rawValue.length != 6) {
45+
count = 0
46+
ScannerAction.Error("Invalid code")
47+
} else if (count++ == 2) {
48+
binding.qrScannerButton.text = "Scan again"
49+
ScannerAction.CloseScanner
50+
} else
51+
ScannerAction.ContinueScanning
52+
}
4153
}
4254

4355
binding.customScannerButton.setOnClickListener {
4456
var count = 0
4557
scanCustomCode.launch(
4658
ScannerConfig.build {
47-
setBarcodeFormats(listOf(BarcodeFormat.FORMAT_QR_CODE))
59+
setBarcodeFormats(listOf(selectedBarcodeFormat)) // set interested barcode formats
60+
setOverlayStringRes(R.string.scan_barcode) // string resource used for the scanner overlay
61+
setOverlayDrawableRes(R.drawable.ic_scan_barcode) // drawable resource used for the scanner overlay
62+
setHapticSuccessFeedback(false) // enable (default) or disable haptic feedback when a barcode was detected
63+
setShowTorchToggle(true) // show or hide (default) torch/flashlight toggle button
64+
setShowCloseButton(true) // show or hide (default) close button
65+
setHorizontalFrameRatio(2.2f) // set the horizontal overlay ratio (default is 1 / square frame)
66+
setUseFrontCamera(false) // use the front camera
4867
setScannerSuccessActionProvider {
4968
delay(1000)
5069
if (it.rawValue.length != 6) {

0 commit comments

Comments
 (0)