@@ -25,20 +25,16 @@ import androidx.compose.runtime.Composable
25
25
import androidx.compose.runtime.Immutable
26
26
import androidx.compose.runtime.Stable
27
27
import androidx.compose.runtime.remember
28
- import androidx.compose.runtime.rememberCoroutineScope
29
28
import io.github.g00fy2.quickie.QRResult
30
29
import io.github.g00fy2.quickie.ScanCustomCode
31
30
import io.github.g00fy2.quickie.config.BarcodeFormat
32
31
import io.github.g00fy2.quickie.config.ScannerConfig
33
- import kotlinx.coroutines.launch
34
32
import ru.tech.imageresizershrinker.core.resources.R
35
- import ru.tech.imageresizershrinker.core.ui.utils.provider.LocalComponentActivity
36
- import ru.tech.imageresizershrinker.core.ui.widget.other.LocalToastHostState
37
- import ru.tech.imageresizershrinker.core.ui.widget.other.showFailureToast
33
+ import ru.tech.imageresizershrinker.core.ui.utils.provider.rememberLocalEssentials
38
34
39
- private class QrCodeScannerImpl (
35
+ private class BarcodeScannerImpl (
40
36
private val scannerLauncher : ManagedActivityResultLauncher <ScannerConfig , QRResult >
41
- ) : QrCodeScanner {
37
+ ) : BarcodeScanner {
42
38
43
39
override fun scan () {
44
40
val config = ScannerConfig .build {
@@ -57,37 +53,26 @@ private class QrCodeScannerImpl(
57
53
58
54
@Stable
59
55
@Immutable
60
- interface QrCodeScanner {
56
+ interface BarcodeScanner {
61
57
fun scan ()
62
58
}
63
59
64
60
65
61
@Composable
66
- fun rememberQrCodeScanner (
67
- onSuccess : (String ) -> Unit
68
- ): QrCodeScanner {
69
- val scope = rememberCoroutineScope()
70
- val toastHostState = LocalToastHostState .current
71
- val context = LocalComponentActivity .current
62
+ fun rememberBarcodeScanner (
63
+ onSuccess : (rawCode: String ) -> Unit
64
+ ): BarcodeScanner {
65
+ val essentials = rememberLocalEssentials()
72
66
73
67
val scannerLauncher = rememberLauncherForActivityResult(ScanCustomCode ()) { result ->
74
68
when (result) {
75
- is QRResult .QRError -> {
76
- scope.launch {
77
- toastHostState.showFailureToast(
78
- context = context,
79
- throwable = result.exception
80
- )
81
- }
82
- }
69
+ is QRResult .QRError -> essentials.showFailureToast(result.exception)
83
70
84
71
QRResult .QRMissingPermission -> {
85
- scope.launch {
86
- toastHostState.showToast(
87
- message = context.getString(R .string.grant_camera_permission_to_scan_qr_code),
88
- icon = Icons .Outlined .CameraAlt
89
- )
90
- }
72
+ essentials.showToast(
73
+ messageSelector = { getString(R .string.grant_camera_permission_to_scan_qr_code) },
74
+ icon = Icons .Outlined .CameraAlt
75
+ )
91
76
}
92
77
93
78
is QRResult .QRSuccess -> {
@@ -100,6 +85,6 @@ fun rememberQrCodeScanner(
100
85
101
86
102
87
return remember(scannerLauncher) {
103
- QrCodeScannerImpl (scannerLauncher)
88
+ BarcodeScannerImpl (scannerLauncher)
104
89
}
105
90
}
0 commit comments