From f5d69f31a9f18a7cdb3b300f20b023dbc4df5dd1 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 1 Mar 2021 17:18:34 +1300 Subject: [PATCH] =?UTF-8?q?1=E5=A2=9E=E5=8A=A0=20=E9=94=99=E8=AF=AF=20?= =?UTF-8?q?=E6=9E=9A=E4=B8=BE=E7=B1=BB=E5=9E=8B=202=E4=BF=AE=E6=94=B9=20?= =?UTF-8?q?=E6=89=AB=E7=A0=81=E6=97=A0=E7=BB=93=E6=9E=9C=E4=B8=8D=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=20=E4=B8=BA=20=E6=89=AB=E7=A0=81=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E7=A9=BA=E9=94=99=E8=AF=AF=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/LBXScanViewController.swift | 26 ++++++++++++++++++------- Source/LBXScanViewStyle.swift | 28 +++++++++++++++++++++++++++ swiftScan/QQScanViewController.swift | 29 +++++++++++++++++----------- 3 files changed, 65 insertions(+), 18 deletions(-) diff --git a/Source/LBXScanViewController.swift b/Source/LBXScanViewController.swift index e0375de..1483436 100755 --- a/Source/LBXScanViewController.swift +++ b/Source/LBXScanViewController.swift @@ -97,7 +97,11 @@ open class LBXScanViewController: UIViewController { // 停止扫描动画 strongSelf.qRScanView?.stopScanAnimation() } - strongSelf.handleCodeResult(arrayResult: arrayResult) + if !arrayResult.isEmpty { + strongSelf.handleCodeResult(reslut: .success(arrayResult)) + } else { + strongSelf.handleCodeResult(reslut: .failure(.noResult)) + } }) } @@ -126,7 +130,7 @@ open class LBXScanViewController: UIViewController { /** 处理扫码结果,如果是继承本控制器的,可以重写该方法,作出相应地处理,或者设置delegate作出相应处理 */ - open func handleCodeResult(arrayResult: [LBXScanResult]) { + open func handleCodeResult(reslut: Result<[LBXScanResult], LBXError>) { guard let delegate = scanResultDelegate else { fatalError("you must set scanResultDelegate or override this method without super keyword") } @@ -136,11 +140,17 @@ open class LBXScanViewController: UIViewController { } - if let result = arrayResult.first { - delegate.scanFinished(scanResult: result, error: nil) - } else { + switch reslut { + case .success(let array): + if let result = array.first { + delegate.scanFinished(scanResult: result, error: nil) + } else { + let result = LBXScanResult(str: nil, img: nil, barCodeType: nil, corner: nil) + delegate.scanFinished(scanResult: result, error: "no scan result") + } + case .failure(let error): let result = LBXScanResult(str: nil, img: nil, barCodeType: nil, corner: nil) - delegate.scanFinished(scanResult: result, error: "no scan result") + delegate.scanFinished(scanResult: result, error: error.rawValue) } } @@ -176,7 +186,9 @@ extension LBXScanViewController: UIImagePickerControllerDelegate, UINavigationCo } let arrayResult = LBXScanWrapper.recognizeQRImage(image: image) if !arrayResult.isEmpty { - handleCodeResult(arrayResult: arrayResult) + handleCodeResult(reslut: .success(arrayResult)) + } else { + handleCodeResult(reslut: .failure(.noResult)) } } diff --git a/Source/LBXScanViewStyle.swift b/Source/LBXScanViewStyle.swift index 0218ac6..d335432 100644 --- a/Source/LBXScanViewStyle.swift +++ b/Source/LBXScanViewStyle.swift @@ -76,3 +76,31 @@ public struct LBXScanViewStyle { public init() { } } + + + +public enum LBXError: Error, RawRepresentable { + public typealias RawValue = String + + public init?(rawValue: String) { + switch rawValue { + case "nr": + self = .noResult + default: + self = .none + } + } + + public var rawValue: RawValue { + switch self { + case .noResult: + return "No scan result" + default: + return "Unknown" + } + } + + case noResult + case none + +} diff --git a/swiftScan/QQScanViewController.swift b/swiftScan/QQScanViewController.swift index 777c363..f78e55d 100644 --- a/swiftScan/QQScanViewController.swift +++ b/swiftScan/QQScanViewController.swift @@ -53,19 +53,26 @@ class QQScanViewController: LBXScanViewController { drawBottomItems() } - override func handleCodeResult(arrayResult: [LBXScanResult]) { - - for result: LBXScanResult in arrayResult { - if let str = result.strScanned { - print(str) + + override func handleCodeResult(reslut: Result<[LBXScanResult], LBXError>) { + switch reslut { + case .success(let array): + for result: LBXScanResult in array { + if let str = result.strScanned { + print(str) + } + } + let vc = ScanResultController() + vc.codeResult = array.first! + navigationController?.pushViewController(vc, animated: true) + default: + let alert = UIAlertController(title: "no scan result", message: nil, preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: { (_) in + })) + if !(self.navigationController?.visibleViewController?.isKind(of: UIAlertController.self))! { + self.present(alert, animated: true, completion: nil) } } - - let result: LBXScanResult = arrayResult[0] - - let vc = ScanResultController() - vc.codeResult = result - navigationController?.pushViewController(vc, animated: true) } func drawBottomItems() {