Navaron Bracke

refactor iOS toggle torch

@@ -273,19 +273,16 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -273,19 +273,16 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
273 device = nil 273 device = nil
274 } 274 }
275 275
276 - /// Toggle the flashlight between on and off 276 + /// Toggle the flashlight between on and off.
277 func toggleTorch(_ torch: AVCaptureDevice.TorchMode) throws { 277 func toggleTorch(_ torch: AVCaptureDevice.TorchMode) throws {
278 - if (device == nil) {  
279 - throw MobileScannerError.torchWhenStopped 278 + if (device == nil || !device.hasTorch || !device.isTorchAvailable) {
  279 + return
280 } 280 }
281 - if (device.hasTorch && device.isTorchAvailable) {  
282 - do {  
283 - try device.lockForConfiguration() 281 +
  282 + if (device.torchMode != torch) {
  283 + device.lockForConfiguration()
284 device.torchMode = torch 284 device.torchMode = torch
285 device.unlockForConfiguration() 285 device.unlockForConfiguration()
286 - } catch {  
287 - throw MobileScannerError.torchError(error)  
288 - }  
289 } 286 }
290 } 287 }
291 288