Navaron Bracke

unify MacOS error codes for FlutterError(); fix barcode error forat and don't se…

…nd a FlutterError through the sink
... ... @@ -14,7 +14,7 @@ struct MobileScannerErrorCodes {
// because it uses the error message from the undelying error.
static let BARCODE_ERROR = "MOBILE_SCANNER_BARCODE_ERROR"
// The error code 'CAMERA_ERROR' does not have an error message,
// because it uses the error message from the underlying error.
// because it uses the error message from the underlying error.
static let CAMERA_ERROR = "MOBILE_SCANNER_CAMERA_ERROR"
static let NO_CAMERA_ERROR = "MOBILE_SCANNER_NO_CAMERA_ERROR"
static let NO_CAMERA_ERROR_MESSAGE = "No cameras available."
... ...
... ... @@ -131,7 +131,10 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
if error != nil {
DispatchQueue.main.async {
self?.sink?(FlutterError(code: "MobileScanner", message: error?.localizedDescription, details: nil))
self?.sink?([
"name": MobileScannerErrorCodes.BARCODE_ERROR,
"data": error?.localizedDescription,
])
}
return
}
... ... @@ -180,9 +183,12 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
}
try imageRequestHandler.perform([barcodeRequest])
} catch let e {
} catch let error {
DispatchQueue.main.async {
self?.sink?(FlutterError(code: "MobileScanner", message: e.localizedDescription, details: nil))
self?.sink?([
"name": MobileScannerErrorCodes.BARCODE_ERROR,
"data": error.localizedDescription,
])
}
}
}
... ... @@ -262,8 +268,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
func start(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
if (device != nil || captureSession != nil) {
result(FlutterError(code: "MobileScanner",
message: "Called start() while already started!",
result(FlutterError(code: MobileScannerErrorCodes.ALREADY_STARTED_ERROR,
message: MobileScannerErrorCodes.ALREADY_STARTED_ERROR_MESSAGE,
details: nil))
return
}
... ... @@ -294,8 +300,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
}
if (device == nil) {
result(FlutterError(code: "MobileScanner",
message: "No camera found or failed to open camera!",
result(FlutterError(code: MobileScannerErrorCodes.NO_CAMERA_ERROR,
message: MobileScannerErrorCodes.NO_CAMERA_ERROR_MESSAGE,
details: nil))
return
}
... ... @@ -313,7 +319,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
let input = try AVCaptureDeviceInput(device: device)
captureSession!.addInput(input)
} catch {
result(FlutterError(code: "MobileScanner", message: error.localizedDescription, details: nil))
result(FlutterError(
code: MobileScannerErrorCodes.CAMERA_ERROR,
message: error.localizedDescription, details: nil))
return
}
captureSession!.sessionPreset = AVCaptureSession.Preset.photo
... ...