Julian Steenbakker

bug: add check to resume start call

@@ -50,7 +50,7 @@ class _MobileScannerState extends State<MobileScanner> @@ -50,7 +50,7 @@ class _MobileScannerState extends State<MobileScanner>
50 void didChangeAppLifecycleState(AppLifecycleState state) { 50 void didChangeAppLifecycleState(AppLifecycleState state) {
51 switch (state) { 51 switch (state) {
52 case AppLifecycleState.resumed: 52 case AppLifecycleState.resumed:
53 - controller.start(); 53 + if (!controller.isStarting) controller.start();
54 break; 54 break;
55 case AppLifecycleState.inactive: 55 case AppLifecycleState.inactive:
56 case AppLifecycleState.paused: 56 case AppLifecycleState.paused:
@@ -102,12 +102,18 @@ class MobileScannerController { @@ -102,12 +102,18 @@ class MobileScannerController {
102 // } 102 // }
103 103
104 // List<BarcodeFormats>? formats = _defaultBarcodeFormats, 104 // List<BarcodeFormats>? formats = _defaultBarcodeFormats,
  105 + bool isStarting = false;
  106 +
105 /// Start barcode scanning. This will first check if the required permissions 107 /// Start barcode scanning. This will first check if the required permissions
106 /// are set. 108 /// are set.
107 Future<void> start() async { 109 Future<void> start() async {
108 ensure('startAsync'); 110 ensure('startAsync');
109 - 111 + if (isStarting) {
  112 + throw Exception('mobile_scanner: Called start() while already starting.');
  113 + }
  114 + isStarting = true;
110 // setAnalyzeMode(AnalyzeMode.barcode.index); 115 // setAnalyzeMode(AnalyzeMode.barcode.index);
  116 +
111 // Check authorization status 117 // Check authorization status
112 MobileScannerState state = 118 MobileScannerState state =
113 MobileScannerState.values[await methodChannel.invokeMethod('state')]; 119 MobileScannerState.values[await methodChannel.invokeMethod('state')];
@@ -118,6 +124,7 @@ class MobileScannerController { @@ -118,6 +124,7 @@ class MobileScannerController {
118 result ? MobileScannerState.authorized : MobileScannerState.denied; 124 result ? MobileScannerState.authorized : MobileScannerState.denied;
119 break; 125 break;
120 case MobileScannerState.denied: 126 case MobileScannerState.denied:
  127 + isStarting = false;
121 throw PlatformException(code: 'NO ACCESS'); 128 throw PlatformException(code: 'NO ACCESS');
122 case MobileScannerState.authorized: 129 case MobileScannerState.authorized:
123 break; 130 break;
@@ -138,11 +145,13 @@ class MobileScannerController { @@ -138,11 +145,13 @@ class MobileScannerController {
138 'start', arguments); 145 'start', arguments);
139 } on PlatformException catch (error) { 146 } on PlatformException catch (error) {
140 debugPrint('${error.code}: ${error.message}'); 147 debugPrint('${error.code}: ${error.message}');
  148 + isStarting = false;
141 // setAnalyzeMode(AnalyzeMode.none.index); 149 // setAnalyzeMode(AnalyzeMode.none.index);
142 return; 150 return;
143 } 151 }
144 152
145 if (startResult == null) { 153 if (startResult == null) {
  154 + isStarting = false;
146 throw PlatformException(code: 'INITIALIZATION ERROR'); 155 throw PlatformException(code: 'INITIALIZATION ERROR');
147 } 156 }
148 157
@@ -151,6 +160,7 @@ class MobileScannerController { @@ -151,6 +160,7 @@ class MobileScannerController {
151 textureId: startResult['textureId'], 160 textureId: startResult['textureId'],
152 size: toSize(startResult['size']), 161 size: toSize(startResult['size']),
153 hasTorch: hasTorch); 162 hasTorch: hasTorch);
  163 + isStarting = false;
154 } 164 }
155 165
156 Future<void> stop() async { 166 Future<void> stop() async {