Use guard statements to reduce nesting
This commit is contained in:
parent
ddabe206ab
commit
edd7398cd4
4 changed files with 58 additions and 56 deletions
|
|
@ -51,11 +51,10 @@ class PGPKeyArmorImportTableViewController: AutoCellHeightUITableViewController,
|
|||
let selectedCell = tableView.cellForRow(at: indexPath)
|
||||
if selectedCell == scanPublicKeyCell {
|
||||
scanned.reset(keytype: ScannedPGPKey.KeyType.publicKey)
|
||||
performSegue(withIdentifier: "showPGPScannerSegue", sender: self)
|
||||
} else if selectedCell == scanPrivateKeyCell {
|
||||
scanned.reset(keytype: ScannedPGPKey.KeyType.privateKey)
|
||||
performSegue(withIdentifier: "showPGPScannerSegue", sender: self)
|
||||
}
|
||||
performSegue(withIdentifier: "showPGPScannerSegue", sender: self)
|
||||
tableView.deselectRow(at: indexPath, animated: true)
|
||||
}
|
||||
|
||||
|
|
@ -78,14 +77,15 @@ class PGPKeyArmorImportTableViewController: AutoCellHeightUITableViewController,
|
|||
}
|
||||
|
||||
override func prepare(for segue: UIStoryboardSegue, sender _: Any?) {
|
||||
if segue.identifier == "showPGPScannerSegue" {
|
||||
if let navController = segue.destination as? UINavigationController {
|
||||
if let viewController = navController.topViewController as? QRScannerController {
|
||||
viewController.delegate = self
|
||||
}
|
||||
} else if let viewController = segue.destination as? QRScannerController {
|
||||
guard segue.identifier == "showPGPScannerSegue" else {
|
||||
return
|
||||
}
|
||||
if let navController = segue.destination as? UINavigationController {
|
||||
if let viewController = navController.topViewController as? QRScannerController {
|
||||
viewController.delegate = self
|
||||
}
|
||||
} else if let viewController = segue.destination as? QRScannerController {
|
||||
viewController.delegate = self
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -419,9 +419,8 @@ extension PasswordEditorTableViewController: QRScannerControllerDelegate {
|
|||
func checkScannedOutput(line: String) -> (accept: Bool, message: String) {
|
||||
if let url = URL(string: line), Token(url: url) != nil {
|
||||
return (accept: true, message: "ValidTokenUrl".localize())
|
||||
} else {
|
||||
return (accept: false, message: "InvalidTokenUrl".localize())
|
||||
}
|
||||
return (accept: false, message: "InvalidTokenUrl".localize())
|
||||
}
|
||||
|
||||
func handleScannedOutput(line: String) {
|
||||
|
|
|
|||
|
|
@ -86,61 +86,63 @@ class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDeleg
|
|||
}
|
||||
}
|
||||
|
||||
override func didReceiveMemoryWarning() {
|
||||
super.didReceiveMemoryWarning()
|
||||
// Dispose of any resources that can be recreated.
|
||||
}
|
||||
|
||||
// MARK: - AVCaptureMetadataOutputObjectsDelegate Methods
|
||||
|
||||
func metadataOutput(_: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from _: AVCaptureConnection) {
|
||||
if let metadataObj = metadataObjects.first as? AVMetadataMachineReadableCodeObject,
|
||||
supportedCodeTypes.contains(metadataObj.type),
|
||||
let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj) {
|
||||
// draw a bounds on the found QR code
|
||||
qrCodeFrameView?.frame = barCodeObject.bounds
|
||||
guard let metadataObj = metadataObjects.first as? AVMetadataMachineReadableCodeObject else {
|
||||
return setNotDetected()
|
||||
}
|
||||
guard supportedCodeTypes.contains(metadataObj.type) else {
|
||||
return setNotDetected()
|
||||
}
|
||||
guard let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj) else {
|
||||
return setNotDetected()
|
||||
}
|
||||
|
||||
// check whether it is a valid result
|
||||
if let scanned = metadataObj.stringValue {
|
||||
if let (accept, message) = delegate?.checkScannedOutput(line: scanned) {
|
||||
scannerOutput.text = message
|
||||
if accept == true {
|
||||
captureSession?.stopRunning()
|
||||
delegate?.handleScannedOutput(line: scanned)
|
||||
DispatchQueue.main.async {
|
||||
SVProgressHUD.showSuccess(withStatus: "Done".localize())
|
||||
SVProgressHUD.dismiss(withDelay: 1)
|
||||
self.navigationController?.popViewController(animated: true)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// no delegate, show the scanned result
|
||||
scannerOutput.text = scanned
|
||||
}
|
||||
} else {
|
||||
scannerOutput.text = "NoStringValue".localize()
|
||||
}
|
||||
} else {
|
||||
qrCodeFrameView?.frame = CGRect.zero
|
||||
scannerOutput.text = "NoQrCodeDetected.".localize()
|
||||
// draw a bounds on the found QR code
|
||||
qrCodeFrameView?.frame = barCodeObject.bounds
|
||||
|
||||
// check whether it is a valid result
|
||||
guard let scanned = metadataObj.stringValue else {
|
||||
scannerOutput.text = "NoStringValue".localize()
|
||||
return
|
||||
}
|
||||
guard let (accept, message) = delegate?.checkScannedOutput(line: scanned) else {
|
||||
// no delegate, show the scanned result
|
||||
scannerOutput.text = scanned
|
||||
return
|
||||
}
|
||||
scannerOutput.text = message
|
||||
guard accept else {
|
||||
return
|
||||
}
|
||||
captureSession?.stopRunning()
|
||||
delegate?.handleScannedOutput(line: scanned)
|
||||
DispatchQueue.main.async {
|
||||
SVProgressHUD.showSuccess(withStatus: "Done".localize())
|
||||
SVProgressHUD.dismiss(withDelay: 1)
|
||||
self.navigationController?.popViewController(animated: true)
|
||||
}
|
||||
}
|
||||
|
||||
func presentCameraSettings() {
|
||||
private func setNotDetected() {
|
||||
qrCodeFrameView?.frame = CGRect.zero
|
||||
scannerOutput.text = "NoQrCodeDetected.".localize()
|
||||
}
|
||||
|
||||
private func presentCameraSettings() {
|
||||
let alertController = UIAlertController(
|
||||
title: "Error".localize(),
|
||||
message: "CameraAccessDenied.".localize() | "WarningToggleCameraPermissionsResetsApp.".localize(),
|
||||
preferredStyle: .alert
|
||||
)
|
||||
alertController.addAction(UIAlertAction(title: "Cancel".localize(), style: .default))
|
||||
alertController.addAction(
|
||||
UIAlertAction(title: "Settings".localize(), style: .cancel) { _ in
|
||||
if let url = URL(string: UIApplication.openSettingsURLString) {
|
||||
UIApplication.shared.open(url, options: [:]) { _ in }
|
||||
}
|
||||
alertController.addAction(UIAlertAction(title: "Settings".localize(), style: .cancel) { _ in
|
||||
if let url = URL(string: UIApplication.openSettingsURLString) {
|
||||
UIApplication.shared.open(url)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
present(alertController, animated: true)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -95,14 +95,15 @@ class SSHKeyArmorImportTableViewController: AutoCellHeightUITableViewController,
|
|||
}
|
||||
|
||||
override func prepare(for segue: UIStoryboardSegue, sender _: Any?) {
|
||||
if segue.identifier == "showSSHScannerSegue" {
|
||||
if let navController = segue.destination as? UINavigationController {
|
||||
if let viewController = navController.topViewController as? QRScannerController {
|
||||
viewController.delegate = self
|
||||
}
|
||||
} else if let viewController = segue.destination as? QRScannerController {
|
||||
guard segue.identifier == "showSSHScannerSegue" else {
|
||||
return
|
||||
}
|
||||
if let navController = segue.destination as? UINavigationController {
|
||||
if let viewController = navController.topViewController as? QRScannerController {
|
||||
viewController.delegate = self
|
||||
}
|
||||
} else if let viewController = segue.destination as? QRScannerController {
|
||||
viewController.delegate = self
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue