Navaron Bracke

fix early returns in iOS implementation of mobile scanner

@@ -37,23 +37,29 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { @@ -37,23 +37,29 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
37 37
38 init(barcodeHandler: BarcodeHandler, registry: FlutterTextureRegistry) { 38 init(barcodeHandler: BarcodeHandler, registry: FlutterTextureRegistry) {
39 self.mobileScanner = MobileScanner(registry: registry, mobileScannerCallback: { barcodes, error, image in 39 self.mobileScanner = MobileScanner(registry: registry, mobileScannerCallback: { barcodes, error, image in
40 - if barcodes != nil {  
41 - let barcodesMap: [Any?] = barcodes!.compactMap { barcode in  
42 - if (MobileScannerPlugin.scanWindow != nil) {  
43 - if (MobileScannerPlugin.isBarcodeInScanWindow(barcode: barcode, imageSize: image.size)) {  
44 - return barcode.data  
45 - } else {  
46 - return nil  
47 - }  
48 - } else {  
49 - return barcode.data  
50 - } 40 + if error != nil {
  41 + barcodeHandler.publishEvent(["name": "error", "data": error!.localizedDescription])
  42 + return
  43 + }
  44 +
  45 + if barcodes == nil {
  46 + return
  47 + }
  48 +
  49 + let barcodesMap: [Any?] = barcodes!.compactMap { barcode in
  50 + if (MobileScannerPlugin.scanWindow == nil) {
  51 + return barcode.data
51 } 52 }
52 - if (!barcodesMap.isEmpty) {  
53 - barcodeHandler.publishEvent(["name": "barcode", "data": barcodesMap, "image": FlutterStandardTypedData(bytes: image.jpegData(compressionQuality: 0.8)!), "width": image.size.width, "height": image.size.height]) 53 +
  54 + if (MobileScannerPlugin.isBarcodeInScanWindow(barcode: barcode, imageSize: image.size)) {
  55 + return barcode.data
54 } 56 }
55 - } else if (error != nil){  
56 - barcodeHandler.publishEvent(["name": "error", "data": error!.localizedDescription]) 57 +
  58 + return nil
  59 + }
  60 +
  61 + if (!barcodesMap.isEmpty) {
  62 + barcodeHandler.publishEvent(["name": "barcode", "data": barcodesMap, "image": FlutterStandardTypedData(bytes: image.jpegData(compressionQuality: 0.8)!), "width": image.size.width, "height": image.size.height])
57 } 63 }
58 }, torchModeChangeCallback: { torchState in 64 }, torchModeChangeCallback: { torchState in
59 barcodeHandler.publishEvent(["name": "torchState", "data": torchState]) 65 barcodeHandler.publishEvent(["name": "torchState", "data": torchState])
@@ -257,12 +263,14 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { @@ -257,12 +263,14 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
257 DispatchQueue.main.async { 263 DispatchQueue.main.async {
258 result(nil) 264 result(nil)
259 } 265 }
260 - } else {  
261 - let barcodesMap: [Any?] = barcodes!.compactMap { barcode in barcode.data }  
262 266
263 - DispatchQueue.main.async {  
264 - result(["name": "barcode", "data": barcodesMap])  
265 - } 267 + return
  268 + }
  269 +
  270 + let barcodesMap: [Any?] = barcodes!.compactMap { barcode in barcode.data }
  271 +
  272 + DispatchQueue.main.async {
  273 + result(["name": "barcode", "data": barcodesMap])
266 } 274 }
267 }) 275 })
268 } 276 }