Julian Steenbakker
Committed by GitHub

Merge pull request #231 from MichaelOrpheo/master

feat: Add permission callback
@@ -9,6 +9,9 @@ class MobileScanner extends StatefulWidget { @@ -9,6 +9,9 @@ class MobileScanner extends StatefulWidget {
9 /// The controller of the camera. 9 /// The controller of the camera.
10 final MobileScannerController? controller; 10 final MobileScannerController? controller;
11 11
  12 + /// Calls the provided [onPermissionSet] callback when the permission is set.
  13 + final Function(bool permissionGranted)? onPermissionSet;
  14 +
12 /// Function that gets called when a Barcode is detected. 15 /// Function that gets called when a Barcode is detected.
13 /// 16 ///
14 /// [barcode] The barcode object with all information about the scanned code. 17 /// [barcode] The barcode object with all information about the scanned code.
@@ -34,6 +37,7 @@ class MobileScanner extends StatefulWidget { @@ -34,6 +37,7 @@ class MobileScanner extends StatefulWidget {
34 this.controller, 37 this.controller,
35 this.fit = BoxFit.cover, 38 this.fit = BoxFit.cover,
36 this.allowDuplicates = false, 39 this.allowDuplicates = false,
  40 + this.onPermissionSet,
37 }); 41 });
38 42
39 @override 43 @override
@@ -48,7 +52,8 @@ class _MobileScannerState extends State<MobileScanner> @@ -48,7 +52,8 @@ class _MobileScannerState extends State<MobileScanner>
48 void initState() { 52 void initState() {
49 super.initState(); 53 super.initState();
50 WidgetsBinding.instance.addObserver(this); 54 WidgetsBinding.instance.addObserver(this);
51 - controller = widget.controller ?? MobileScannerController(); 55 + controller = widget.controller ??
  56 + MobileScannerController(onPermissionSet: widget.onPermissionSet);
52 if (!controller.isStarting) controller.start(); 57 if (!controller.isStarting) controller.start();
53 } 58 }
54 59
@@ -118,7 +123,8 @@ class _MobileScannerState extends State<MobileScanner> @@ -118,7 +123,8 @@ class _MobileScannerState extends State<MobileScanner>
118 } 123 }
119 } else { 124 } else {
120 if (widget.controller == null) { 125 if (widget.controller == null) {
121 - controller = MobileScannerController(); 126 + controller =
  127 + MobileScannerController(onPermissionSet: widget.onPermissionSet);
122 } else if (oldWidget.controller != widget.controller) { 128 } else if (oldWidget.controller != widget.controller) {
123 controller = widget.controller!; 129 controller = widget.controller!;
124 } 130 }
@@ -39,6 +39,7 @@ class MobileScannerController { @@ -39,6 +39,7 @@ class MobileScannerController {
39 static int? _controllerHashcode; 39 static int? _controllerHashcode;
40 StreamSubscription? events; 40 StreamSubscription? events;
41 41
  42 + Function(bool permissionGranted)? onPermissionSet;
42 final ValueNotifier<MobileScannerArguments?> args = ValueNotifier(null); 43 final ValueNotifier<MobileScannerArguments?> args = ValueNotifier(null);
43 final ValueNotifier<TorchState> torchState = ValueNotifier(TorchState.off); 44 final ValueNotifier<TorchState> torchState = ValueNotifier(TorchState.off);
44 late final ValueNotifier<CameraFacing> cameraFacingState; 45 late final ValueNotifier<CameraFacing> cameraFacingState;
@@ -66,6 +67,7 @@ class MobileScannerController { @@ -66,6 +67,7 @@ class MobileScannerController {
66 this.ratio, 67 this.ratio,
67 this.torchEnabled, 68 this.torchEnabled,
68 this.formats, 69 this.formats,
  70 + this.onPermissionSet,
69 this.autoResume = true, 71 this.autoResume = true,
70 this.returnImage = false, 72 this.returnImage = false,
71 }) { 73 }) {
@@ -156,11 +158,14 @@ class MobileScannerController { @@ -156,11 +158,14 @@ class MobileScannerController {
156 state = result 158 state = result
157 ? MobileScannerState.authorized 159 ? MobileScannerState.authorized
158 : MobileScannerState.denied; 160 : MobileScannerState.denied;
  161 + onPermissionSet?.call(result);
159 break; 162 break;
160 case MobileScannerState.denied: 163 case MobileScannerState.denied:
161 isStarting = false; 164 isStarting = false;
  165 + onPermissionSet?.call(false);
162 throw PlatformException(code: 'NO ACCESS'); 166 throw PlatformException(code: 'NO ACCESS');
163 case MobileScannerState.authorized: 167 case MobileScannerState.authorized:
  168 + onPermissionSet?.call(true);
164 break; 169 break;
165 } 170 }
166 } 171 }
@@ -192,6 +197,9 @@ class MobileScannerController { @@ -192,6 +197,9 @@ class MobileScannerController {
192 } on PlatformException catch (error) { 197 } on PlatformException catch (error) {
193 debugPrint('${error.code}: ${error.message}'); 198 debugPrint('${error.code}: ${error.message}');
194 isStarting = false; 199 isStarting = false;
  200 + if (error.code == "MobileScannerWeb") {
  201 + onPermissionSet?.call(false);
  202 + }
195 // setAnalyzeMode(AnalyzeMode.none.index); 203 // setAnalyzeMode(AnalyzeMode.none.index);
196 return; 204 return;
197 } 205 }
@@ -204,6 +212,10 @@ class MobileScannerController { @@ -204,6 +212,10 @@ class MobileScannerController {
204 hasTorch = startResult['torchable'] as bool? ?? false; 212 hasTorch = startResult['torchable'] as bool? ?? false;
205 213
206 if (kIsWeb) { 214 if (kIsWeb) {
  215 + onPermissionSet?.call(
  216 + true,
  217 + ); // If we reach this line, it means camera permission has been granted
  218 +
207 args.value = MobileScannerArguments( 219 args.value = MobileScannerArguments(
208 webId: startResult['ViewID'] as String?, 220 webId: startResult['ViewID'] as String?,
209 size: Size( 221 size: Size(
@@ -287,6 +299,7 @@ class MobileScannerController { @@ -287,6 +299,7 @@ class MobileScannerController {
287 events?.cancel(); 299 events?.cancel();
288 events = null; 300 events = null;
289 _controllerHashcode = null; 301 _controllerHashcode = null;
  302 + onPermissionSet = null;
290 } 303 }
291 barcodesController.close(); 304 barcodesController.close();
292 } 305 }