Navaron Bracke

return instead of throw when toggling torch on Android

@@ -376,7 +376,7 @@ class MobileScanner( @@ -376,7 +376,7 @@ class MobileScanner(
376 */ 376 */
377 fun toggleTorch(enableTorch: Boolean) { 377 fun toggleTorch(enableTorch: Boolean) {
378 if (camera == null) { 378 if (camera == null) {
379 - throw TorchWhenStopped() 379 + return
380 } 380 }
381 381
382 if (camera?.cameraInfo?.hasFlashUnit() == true) { 382 if (camera?.cameraInfo?.hasFlashUnit() == true) {
@@ -4,6 +4,5 @@ class NoCamera : Exception() @@ -4,6 +4,5 @@ class NoCamera : Exception()
4 class AlreadyStarted : Exception() 4 class AlreadyStarted : Exception()
5 class AlreadyStopped : Exception() 5 class AlreadyStopped : Exception()
6 class CameraError : Exception() 6 class CameraError : Exception()
7 -class TorchWhenStopped : Exception()  
8 class ZoomWhenStopped : Exception() 7 class ZoomWhenStopped : Exception()
9 class ZoomNotInRange : Exception() 8 class ZoomNotInRange : Exception()
@@ -237,12 +237,8 @@ class MobileScannerHandler( @@ -237,12 +237,8 @@ class MobileScannerHandler(
237 } 237 }
238 238
239 private fun toggleTorch(call: MethodCall, result: MethodChannel.Result) { 239 private fun toggleTorch(call: MethodCall, result: MethodChannel.Result) {
240 - try {  
241 mobileScanner!!.toggleTorch(call.arguments == 1) 240 mobileScanner!!.toggleTorch(call.arguments == 1)
242 result.success(null) 241 result.success(null)
243 - } catch (e: TorchWhenStopped) {  
244 - result.error("MobileScanner", "Called toggleTorch() while stopped!", null)  
245 - }  
246 } 242 }
247 243
248 private fun setScale(call: MethodCall, result: MethodChannel.Result) { 244 private fun setScale(call: MethodCall, result: MethodChannel.Result) {
@@ -277,7 +277,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, @@ -277,7 +277,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
277 // Turn on the torch if requested. 277 // Turn on the torch if requested.
278 if (torch) { 278 if (torch) {
279 do { 279 do {
280 - toggleTorchInternal(.on) 280 + self.toggleTorchInternal(.on)
281 } catch { 281 } catch {
282 result(FlutterError(code: error.localizedDescription, message: nil, details: nil)) 282 result(FlutterError(code: error.localizedDescription, message: nil, details: nil))
283 return 283 return
@@ -335,7 +335,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, @@ -335,7 +335,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
335 let requestedTorchMode: AVCaptureDevice.TorchMode = call.arguments as! Int == 1 ? .on : .off 335 let requestedTorchMode: AVCaptureDevice.TorchMode = call.arguments as! Int == 1 ? .on : .off
336 336
337 do { 337 do {
338 - try toggleTorchInternal(requestedTorchMode) 338 + try self.toggleTorchInternal(requestedTorchMode)
339 result(nil) 339 result(nil)
340 } catch { 340 } catch {
341 result(FlutterError(code: error.localizedDescription, message: nil, details: nil)) 341 result(FlutterError(code: error.localizedDescription, message: nil, details: nil))