Navaron Bracke

format SwiftMobileScannerPlugin.swift

@@ -14,7 +14,7 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { @@ -14,7 +14,7 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
14 14
15 /// The points for the scan window. 15 /// The points for the scan window.
16 static var scanWindow: [CGFloat]? 16 static var scanWindow: [CGFloat]?
17 - 17 +
18 private static func isBarcodeInScanWindow(barcode: Barcode, imageSize: CGSize) -> Bool { 18 private static func isBarcodeInScanWindow(barcode: Barcode, imageSize: CGSize) -> Bool {
19 let scanwindow = SwiftMobileScannerPlugin.scanWindow! 19 let scanwindow = SwiftMobileScannerPlugin.scanWindow!
20 let barcodeminX = barcode.cornerPoints![0].cgPointValue.x 20 let barcodeminX = barcode.cornerPoints![0].cgPointValue.x
@@ -24,7 +24,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { @@ -24,7 +24,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
24 let barcodeheight = barcode.cornerPoints![3].cgPointValue.y - barcodeminY 24 let barcodeheight = barcode.cornerPoints![3].cgPointValue.y - barcodeminY
25 let barcodeBox = CGRect(x: barcodeminX, y: barcodeminY, width: barcodewidth, height: barcodeheight) 25 let barcodeBox = CGRect(x: barcodeminX, y: barcodeminY, width: barcodewidth, height: barcodeheight)
26 26
27 -  
28 let minX = scanwindow[0] * imageSize.width 27 let minX = scanwindow[0] * imageSize.width
29 let minY = scanwindow[1] * imageSize.height 28 let minY = scanwindow[1] * imageSize.height
30 29
@@ -64,14 +63,13 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { @@ -64,14 +63,13 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
64 self.barcodeHandler = barcodeHandler 63 self.barcodeHandler = barcodeHandler
65 super.init() 64 super.init()
66 } 65 }
67 - 66 +
68 public static func register(with registrar: FlutterPluginRegistrar) { 67 public static func register(with registrar: FlutterPluginRegistrar) {
  68 + let channel = FlutterMethodChannel(name: "dev.steenbakker.mobile_scanner/scanner/method", binaryMessenger: registrar.messenger())
69 let instance = SwiftMobileScannerPlugin(barcodeHandler: BarcodeHandler(registrar: registrar), registry: registrar.textures()) 69 let instance = SwiftMobileScannerPlugin(barcodeHandler: BarcodeHandler(registrar: registrar), registry: registrar.textures())
70 - let methodChannel = FlutterMethodChannel(name:  
71 - "dev.steenbakker.mobile_scanner/scanner/method", binaryMessenger: registrar.messenger())  
72 - registrar.addMethodCallDelegate(instance, channel: methodChannel) 70 + registrar.addMethodCallDelegate(instance, channel: channel)
73 } 71 }
74 - 72 +
75 public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { 73 public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
76 switch call.method { 74 switch call.method {
77 case "state": 75 case "state":
@@ -96,7 +94,7 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { @@ -96,7 +94,7 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
96 result(FlutterMethodNotImplemented) 94 result(FlutterMethodNotImplemented)
97 } 95 }
98 } 96 }
99 - 97 +
100 /// Parses all parameters and starts the mobileScanner 98 /// Parses all parameters and starts the mobileScanner
101 private func start(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) { 99 private func start(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
102 let torch: Bool = (call.arguments as! Dictionary<String, Any?>)["torch"] as? Bool ?? false 100 let torch: Bool = (call.arguments as! Dictionary<String, Any?>)["torch"] as? Bool ?? false
@@ -106,18 +104,17 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { @@ -106,18 +104,17 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
106 let speed: Int = (call.arguments as! Dictionary<String, Any?>)["speed"] as? Int ?? 0 104 let speed: Int = (call.arguments as! Dictionary<String, Any?>)["speed"] as? Int ?? 0
107 let timeoutMs: Int = (call.arguments as! Dictionary<String, Any?>)["timeout"] as? Int ?? 0 105 let timeoutMs: Int = (call.arguments as! Dictionary<String, Any?>)["timeout"] as? Int ?? 0
108 self.mobileScanner.timeoutSeconds = Double(timeoutMs / 1000) 106 self.mobileScanner.timeoutSeconds = Double(timeoutMs / 1000)
109 - 107 +
110 let formatList = formats.map { format in return BarcodeFormat(rawValue: format)} 108 let formatList = formats.map { format in return BarcodeFormat(rawValue: format)}
111 var barcodeOptions: BarcodeScannerOptions? = nil 109 var barcodeOptions: BarcodeScannerOptions? = nil
112 110
113 - if (formatList.count != 0) {  
114 - var barcodeFormats: BarcodeFormat = []  
115 - for index in formats {  
116 - barcodeFormats.insert(BarcodeFormat(rawValue: index))  
117 - }  
118 - barcodeOptions = BarcodeScannerOptions(formats: barcodeFormats)  
119 - }  
120 - 111 + if (formatList.count != 0) {
  112 + var barcodeFormats: BarcodeFormat = []
  113 + for index in formats {
  114 + barcodeFormats.insert(BarcodeFormat(rawValue: index))
  115 + }
  116 + barcodeOptions = BarcodeScannerOptions(formats: barcodeFormats)
  117 + }
121 118
122 let position = facing == 0 ? AVCaptureDevice.Position.front : .back 119 let position = facing == 0 ? AVCaptureDevice.Position.front : .back
123 let detectionSpeed: DetectionSpeed = DetectionSpeed(rawValue: speed)! 120 let detectionSpeed: DetectionSpeed = DetectionSpeed(rawValue: speed)!
@@ -174,8 +171,8 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { @@ -174,8 +171,8 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
174 let scale = call.arguments as? CGFloat 171 let scale = call.arguments as? CGFloat
175 if (scale == nil) { 172 if (scale == nil) {
176 result(FlutterError(code: "MobileScanner", 173 result(FlutterError(code: "MobileScanner",
177 - message: "You must provide a scale when calling setScale!",  
178 - details: nil)) 174 + message: "You must provide a scale when calling setScale!",
  175 + details: nil))
179 return 176 return
180 } 177 }
181 do { 178 do {
@@ -216,7 +213,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { @@ -216,7 +213,6 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
216 result(nil) 213 result(nil)
217 } 214 }
218 215
219 -  
220 /// Toggles the torch 216 /// Toggles the torch
221 func updateScanWindow(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) { 217 func updateScanWindow(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
222 let scanWindowData: Array? = (call.arguments as? [String: Any])?["rect"] as? [CGFloat] 218 let scanWindowData: Array? = (call.arguments as? [String: Any])?["rect"] as? [CGFloat]