Navaron Bracke

use a single try catch block for the camera permission

@@ -94,12 +94,29 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { @@ -94,12 +94,29 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
94 /// 94 ///
95 /// Throws a [MobileScannerException] if the permission is not granted. 95 /// Throws a [MobileScannerException] if the permission is not granted.
96 Future<void> _requestCameraPermission() async { 96 Future<void> _requestCameraPermission() async {
97 - final MobileScannerAuthorizationState authorizationState;  
98 -  
99 try { 97 try {
100 - authorizationState = MobileScannerAuthorizationState.fromRawValue( 98 + final MobileScannerAuthorizationState authorizationState =
  99 + MobileScannerAuthorizationState.fromRawValue(
101 await methodChannel.invokeMethod<int>('state') ?? 0, 100 await methodChannel.invokeMethod<int>('state') ?? 0,
102 ); 101 );
  102 +
  103 + switch (authorizationState) {
  104 + // Authorization was already granted, no need to request it again.
  105 + case MobileScannerAuthorizationState.authorized:
  106 + return;
  107 + // Android does not have an undetermined authorization state.
  108 + // So if the permission was denied, request it again.
  109 + case MobileScannerAuthorizationState.denied:
  110 + case MobileScannerAuthorizationState.undetermined:
  111 + final bool permissionGranted =
  112 + await methodChannel.invokeMethod<bool>('request') ?? false;
  113 +
  114 + if (!permissionGranted) {
  115 + throw const MobileScannerException(
  116 + errorCode: MobileScannerErrorCode.permissionDenied,
  117 + );
  118 + }
  119 + }
103 } on PlatformException catch (error) { 120 } on PlatformException catch (error) {
104 // If the permission state is invalid, that is an error. 121 // If the permission state is invalid, that is an error.
105 throw MobileScannerException( 122 throw MobileScannerException(
@@ -111,37 +128,6 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { @@ -111,37 +128,6 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
111 ), 128 ),
112 ); 129 );
113 } 130 }
114 -  
115 - switch (authorizationState) {  
116 - case MobileScannerAuthorizationState.authorized:  
117 - return; // Already authorized.  
118 - // Android does not have an undetermined authorization state.  
119 - // So if the permission was denied, request it again.  
120 - case MobileScannerAuthorizationState.denied:  
121 - case MobileScannerAuthorizationState.undetermined:  
122 - try {  
123 - final bool granted =  
124 - await methodChannel.invokeMethod<bool>('request') ?? false;  
125 -  
126 - if (granted) {  
127 - return; // Authorization was granted.  
128 - }  
129 -  
130 - throw const MobileScannerException(  
131 - errorCode: MobileScannerErrorCode.permissionDenied,  
132 - );  
133 - } on PlatformException catch (error) {  
134 - // If the permission state is invalid, that is an error.  
135 - throw MobileScannerException(  
136 - errorCode: MobileScannerErrorCode.genericError,  
137 - errorDetails: MobileScannerErrorDetails(  
138 - code: error.code,  
139 - details: error.details as Object?,  
140 - message: error.message,  
141 - ),  
142 - );  
143 - }  
144 - }  
145 } 131 }
146 132
147 @override 133 @override