From 6f295be74d4d6160b2922a7fa07b3c79d34c5848 Mon Sep 17 00:00:00 2001 From: Yishi Lin Date: Thu, 3 Oct 2019 14:27:00 +0800 Subject: [PATCH] Polish codes of QR code scanner --- ...SHKeyArmorSettingTableViewController.swift | 67 +++++---------- ...GPKeyArmorSettingTableViewController.swift | 86 +++++++------------ pass/Controllers/QRScannerController.swift | 7 +- pass/de.lproj/Localizable.stringsdict | 2 +- pass/en.lproj/Localizable.stringsdict | 2 +- 5 files changed, 57 insertions(+), 107 deletions(-) diff --git a/pass/Controllers/GitSSHKeyArmorSettingTableViewController.swift b/pass/Controllers/GitSSHKeyArmorSettingTableViewController.swift index c70ab8a..46a7126 100644 --- a/pass/Controllers/GitSSHKeyArmorSettingTableViewController.swift +++ b/pass/Controllers/GitSSHKeyArmorSettingTableViewController.swift @@ -17,54 +17,34 @@ class GitSSHKeyArmorSettingTableViewController: AutoCellHeightUITableViewControl let passwordStore = PasswordStore.shared class ScannedSSHKey { - static let maxNumberOfGif = 100 - var numberOfSegments = 0 - var previousSegment = "" - var key = "" + var segments = [String]() var message = "" - var hasStarted = false - var isDone = false func reset() { - numberOfSegments = 0 - previousSegment = "" - key = "" + self.segments.removeAll() message = "LookingForStartingFrame.".localize() - hasStarted = false - isDone = false } - func addSegment(segment: String) { - // skip duplicated segments - guard segment != previousSegment else { - return + func addSegment(segment: String) -> (accept: Bool, message: String) { + // Skip duplicated segments. + guard segment != self.segments.last else { + return (accept: false, message: self.message) } - previousSegment = segment - - // check whether we have found the first block - if hasStarted == false { - hasStarted = segment.contains("-----BEGIN") + + // Check whether we have found the first block. + guard !self.segments.isEmpty || segment.contains("-----BEGIN") else { + return (accept: false, message: self.message) } - guard hasStarted == true else { - return + + // Update the list of scanned segment and return. + self.segments.append(segment) + if segment.range(of: "-----END.*KEY-----", options: .regularExpression, range: nil, locale: nil) != nil { + self.message = "Done".localize() + return (accept: true, message: self.message) + } else { + self.message = "ScannedQrCodes(%d)".localize(self.segments.count) + return (accept: false, message: self.message) } - - // check the number of segments - numberOfSegments = numberOfSegments + 1 - guard numberOfSegments <= ScannedSSHKey.maxNumberOfGif else { - key = "TooManyQrCodes".localize() - return - } - - // update full text and check whether we are done - key.append(segment) - if let index1 = key.range(of: "-----END")?.lowerBound, - let _ = key.suffix(from: index1).range(of: "KEY-----")?.lowerBound { - isDone = true - } - - // update message - message = "ScannedQrCodes(%d)".localize(numberOfSegments) } } var scanned = ScannedSSHKey() @@ -109,17 +89,12 @@ class GitSSHKeyArmorSettingTableViewController: AutoCellHeightUITableViewControl // MARK: - QRScannerControllerDelegate Methods func checkScannedOutput(line: String) -> (accept: Bool, message: String) { - scanned.addSegment(segment: line) - if scanned.isDone { - return (accept: true, message: "Done".localize()) - } else { - return (accept: false, message: scanned.message) - } + return scanned.addSegment(segment: line) } // MARK: - QRScannerControllerDelegate Methods func handleScannedOutput(line: String) { - armorPrivateKeyTextView.text = scanned.key + armorPrivateKeyTextView.text = scanned.segments.joined(separator: "") } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { diff --git a/pass/Controllers/PGPKeyArmorSettingTableViewController.swift b/pass/Controllers/PGPKeyArmorSettingTableViewController.swift index 340adc3..cbab609 100644 --- a/pass/Controllers/PGPKeyArmorSettingTableViewController.swift +++ b/pass/Controllers/PGPKeyArmorSettingTableViewController.swift @@ -19,71 +19,47 @@ class PGPKeyArmorSettingTableViewController: AutoCellHeightUITableViewController let keychain = AppKeychain.shared class ScannedPGPKey { - static let maxNumberOfGif = 100 enum KeyType { case publicKey, privateKey } var keyType = KeyType.publicKey - var numberOfSegments = 0 - var previousSegment = "" - var key = "" + var segments = [String]() var message = "" - var hasStarted = false - var isDone = false func reset(keytype: KeyType) { self.keyType = keytype - numberOfSegments = 0 - previousSegment = "" - key = "" + self.segments.removeAll() message = "LookingForStartingFrame.".localize() - hasStarted = false - isDone = false } - func addSegment(segment: String) { - // skip duplicated segments - guard segment != previousSegment else { - return + func addSegment(segment: String) -> (accept: Bool, message: String) { + let keyTypeStr = self.keyType == .publicKey ? "Public" : "Private" + let theOtherKeyTypeStr = self.keyType == .publicKey ? "Private" : "Public" + + // Skip duplicated segments. + guard segment != self.segments.last else { + return (accept: false, message: self.message) } - previousSegment = segment - // check whether we have found the first block - if hasStarted == false { - let findPublic = segment.contains("-----BEGIN PGP PUBLIC KEY BLOCK-----") - let findPrivate = segment.contains("-----BEGIN PGP PRIVATE KEY BLOCK-----") - switch keyType { - case .publicKey: - if findPrivate { - message = "ScanPrivateKey.".localize() - } - hasStarted = findPublic - case .privateKey: - if findPublic { - message = "ScanPrivateKey.".localize() - } - hasStarted = findPrivate + // Check whether we have found the first block. + guard !self.segments.isEmpty || segment.contains("-----BEGIN PGP \(keyTypeStr.uppercased()) KEY BLOCK-----") else { + // Check whether we are scanning the wrong key type. + if segment.contains("-----BEGIN PGP \(theOtherKeyTypeStr.uppercased()) KEY BLOCK-----") { + self.message = "Scan\(keyTypeStr)Key.".localize() } + return (accept: false, message: self.message) } - guard hasStarted == true else { - return + + // Update the list of scanned segment and return. + self.segments.append(segment) + if segment.contains("-----END PGP .* KEY BLOCK-----") { + self.message = "Done".localize() + return (accept: true, message: self.message) + } else { + self.message = "ScannedQrCodes(%d)".localize(self.segments.count) + print(self.message) + return (accept: false, message: self.message) } - - // check the number of segments - numberOfSegments = numberOfSegments + 1 - guard numberOfSegments <= ScannedPGPKey.maxNumberOfGif else { - key = "TooManyQrCodes".localize() - return - } - - // update full text and check whether we are done - key.append(segment) - if key.contains("-----END PGP PUBLIC KEY BLOCK-----") || key.contains("-----END PGP PRIVATE KEY BLOCK-----") { - isDone = true - } - - // update message - message = "ScannedQrCodes(%d)".localize(numberOfSegments) } } var scanned = ScannedPGPKey() @@ -158,21 +134,17 @@ class PGPKeyArmorSettingTableViewController: AutoCellHeightUITableViewController // MARK: - QRScannerControllerDelegate Methods func checkScannedOutput(line: String) -> (accept: Bool, message: String) { - scanned.addSegment(segment: line) - if scanned.isDone { - return (accept: true, message: "Done".localize()) - } else { - return (accept: false, message: scanned.message) - } + return scanned.addSegment(segment: line) } // MARK: - QRScannerControllerDelegate Methods func handleScannedOutput(line: String) { + let key = scanned.segments.joined(separator: "") switch scanned.keyType { case .publicKey: - armorPublicKeyTextView.text = scanned.key + armorPublicKeyTextView.text = key case .privateKey: - armorPrivateKeyTextView.text = scanned.key + armorPrivateKeyTextView.text = key } } diff --git a/pass/Controllers/QRScannerController.swift b/pass/Controllers/QRScannerController.swift index 4131b47..2f33b71 100644 --- a/pass/Controllers/QRScannerController.swift +++ b/pass/Controllers/QRScannerController.swift @@ -37,10 +37,13 @@ class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDeleg } // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video as the media type parameter. - let captureDevice = AVCaptureDevice.default(for: AVMediaType.video) + guard let captureDevice = AVCaptureDevice.default(for: .video) else { + scannerOutput.text = "CameraAccessDenied.".localize() + return + } do { // Get an instance of the AVCaptureDeviceInput class using the previous device object. - let input = try AVCaptureDeviceInput(device: captureDevice!) + let input = try AVCaptureDeviceInput(device: captureDevice) // Initialize the captureSession object. captureSession = AVCaptureSession() diff --git a/pass/de.lproj/Localizable.stringsdict b/pass/de.lproj/Localizable.stringsdict index 89b0929..09af6eb 100644 --- a/pass/de.lproj/Localizable.stringsdict +++ b/pass/de.lproj/Localizable.stringsdict @@ -5,7 +5,7 @@ ScannedQrCodes(%d) NSStringLocalizedFormatKey - %#@code@ + %#@codes@ codes NSStringFormatSpecTypeKey diff --git a/pass/en.lproj/Localizable.stringsdict b/pass/en.lproj/Localizable.stringsdict index 14e29ef..07e6b11 100644 --- a/pass/en.lproj/Localizable.stringsdict +++ b/pass/en.lproj/Localizable.stringsdict @@ -5,7 +5,7 @@ ScannedQrCodes(%d) NSStringLocalizedFormatKey - %#@code@ + %#@codes@ codes NSStringFormatSpecTypeKey