Skip to content

Commit

Permalink
Fix: #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Yerin committed May 15, 2023
1 parent 2961538 commit 651399e
Showing 8 changed files with 48 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
.DS_Store
.atom/
.buildlog/
.vscode/
.history
.svn/
migrate_working_dir/
2 changes: 2 additions & 0 deletions .pubignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tags

9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 0.0.3

* Fix: https://github.com/roman-yerin/native_qr/issues/1
* Exceptions thrown are now uppercased and unified within platforms

1. CANCELED: scanning operation has been canceled
1. ERROR: some error happened
1. UNKNOWN: God knows what happened and you highly likely will never see this type of error

## 0.0.2

* Fix: IOS isSupported and isAvailable flags checked before scan start.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -62,10 +62,18 @@ Android implementation uses [GmsBarcodeScanner](https://developers.google.com/an
</application>
```

## Error handling

The plugin either returnes the scanned data or throws an exception that is one of the following.

1. **CANCELED**: scanning operation has been canceled
1. **ERROR**: some error happened
1. **UNKNOWN**: God knows what happened and you highly likely will never see this type of error

## Limitations and known issues

1. Only QR codes supported (can be changed in case it is needed, open a feature request on Github)
2. IOS implementation since 16.0 (which is 81% of the market share as for March, 2023)
2. IOS implementation since 16.0 (which is >90% of the market share as for May, 2023)
3. Only string representation of QR encoded data is provided
4. The result will be only one QR in the camera's field of view, which one is unpredictable
5. IOS multiscene apps unsupported, the plugin uses the first scene to present the camera view
4 changes: 2 additions & 2 deletions android/src/main/kotlin/team/ion/native_qr/NativeQrPlugin.kt
Original file line number Diff line number Diff line change
@@ -39,11 +39,11 @@ class NativeQrPlugin: FlutterPlugin, MethodCallHandler {
}
.addOnCanceledListener {
// Task canceled
result.error("canceled", null, null)
result.error("CANCELED", null, null)
}
.addOnFailureListener { e ->
// Task failed with an exception
result.error("canceled", null, e)
result.error("ERROR", null, e)
}
}
else -> result.notImplemented()
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
@@ -121,7 +121,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.1"
version: "0.0.3"
path:
dependency: transitive
description:
33 changes: 23 additions & 10 deletions ios/Classes/NativeQrPlugin.swift
Original file line number Diff line number Diff line change
@@ -6,12 +6,17 @@ extension NSNotification.Name {
static var QrCodeFound: Notification.Name {
return .init(rawValue: "QrCodeFound")
}
static var ScanCanceled: Notification.Name {
return .init(rawValue: "ScanCanceled")
}
}


@available(iOS 16.0, *)
public class NativeQrPlugin: UIViewController, FlutterPlugin, DataScannerViewControllerDelegate {
private var observer : NSObjectProtocol?
private var successObserver : NSObjectProtocol?
private var cancelObserver : NSObjectProtocol?

private var viewController = DataScannerViewController(
recognizedDataTypes: [.barcode(symbologies: [.qr])],
qualityLevel: .balanced,
@@ -51,13 +56,16 @@ public class NativeQrPlugin: UIViewController, FlutterPlugin, DataScannerViewCon
}

private func cleanupAndClose(){
viewController.stopScanning()
viewController.dismiss(animated: true)
NotificationCenter.default.removeObserver(observer!)
DispatchQueue.main.async { [weak self] in
self?.viewController.stopScanning()
self?.viewController.dismiss(animated: true)
}
NotificationCenter.default.removeObserver(successObserver!)
NotificationCenter.default.removeObserver(cancelObserver!)
}

@objc func closeButtonPressed() {
cleanupAndClose()
NotificationCenter.default.post(name: .ScanCanceled, object: nil)
}

@MainActor private func getQrCode() -> Bool {
@@ -88,20 +96,20 @@ public class NativeQrPlugin: UIViewController, FlutterPlugin, DataScannerViewCon
switch call.method {
case "getQrCode":
if(!DataScannerViewController.isSupported){
result(FlutterError(code: "UNSUPPORTED", message: "DataScannerViewController is upsupported on this device", details: nil))
result(FlutterError(code: "ERROR", message: "DataScannerViewController is upsupported on this device", details: nil))
break
}
if(!DataScannerViewController.isAvailable){
result(FlutterError(code: "UNAVAILABLE", message: "DataScannerViewController is unavailable now", details: nil))
result(FlutterError(code: "ERROR", message: "DataScannerViewController is unavailable now", details: nil))
break
}
observer = NotificationCenter.default.addObserver(forName: .QrCodeFound, object: nil, queue: OperationQueue()) { [weak self] msg in
successObserver = NotificationCenter.default.addObserver(forName: .QrCodeFound, object: nil, queue: OperationQueue()) { [weak self] msg in
let item = msg.object as! RecognizedItem

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
self!.cleanupAndClose()
}

switch item {
case .barcode(let code):
result(code.payloadStringValue);
@@ -112,8 +120,13 @@ public class NativeQrPlugin: UIViewController, FlutterPlugin, DataScannerViewCon
}

}
cancelObserver = NotificationCenter.default.addObserver(forName: .ScanCanceled, object: nil, queue: OperationQueue()) { [weak self] msg in
self!.cleanupAndClose()
result(FlutterError(code: "CANCELED", message: "Qr code scanning canceled", details: nil))
}

if(!getQrCode()){
result(FlutterError(code: "CANTSTART", message: "DataScannerViewController can't start scanning", details: nil))
result(FlutterError(code: "ERROR", message: "DataScannerViewController can't start scanning", details: nil))
}
break;
default:
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: native_qr
description: The simplest QR code scanner for Flutter ever.
version: 0.0.2
version: 0.0.3
homepage: https://github.com/roman-yerin/native_qr
repository: https://github.com/roman-yerin/native_qr
platforms:

1 comment on commit 651399e

@ralph-bergmann
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to How to build a package in Dart you have to increase the major number of the version number because it is an API breaking change ;-)

Please sign in to comment.