Use guard statements to reduce nesting

This commit is contained in:
Danny Moesch 2020-08-19 19:51:09 +02:00 committed by Mingshen Sun
parent ddabe206ab
commit edd7398cd4
4 changed files with 58 additions and 56 deletions

View file

@ -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,7 +77,9 @@ class PGPKeyArmorImportTableViewController: AutoCellHeightUITableViewController,
}
override func prepare(for segue: UIStoryboardSegue, sender _: Any?) {
if segue.identifier == "showPGPScannerSegue" {
guard segue.identifier == "showPGPScannerSegue" else {
return
}
if let navController = segue.destination as? UINavigationController {
if let viewController = navController.topViewController as? QRScannerController {
viewController.delegate = self
@ -88,7 +89,6 @@ class PGPKeyArmorImportTableViewController: AutoCellHeightUITableViewController,
}
}
}
}
extension PGPKeyArmorImportTableViewController: PGPKeyImporter {
static let keySource = KeySource.armor

View file

@ -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) {

View file

@ -86,25 +86,36 @@ 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) {
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()
}
// draw a bounds on the found QR code
qrCodeFrameView?.frame = barCodeObject.bounds
// check whether it is a valid result
if let scanned = metadataObj.stringValue {
if let (accept, message) = delegate?.checkScannedOutput(line: scanned) {
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
if accept == true {
guard accept else {
return
}
captureSession?.stopRunning()
delegate?.handleScannedOutput(line: scanned)
DispatchQueue.main.async {
@ -113,34 +124,25 @@ class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDeleg
self.navigationController?.popViewController(animated: true)
}
}
} else {
// no delegate, show the scanned result
scannerOutput.text = scanned
}
} else {
scannerOutput.text = "NoStringValue".localize()
}
} else {
private func setNotDetected() {
qrCodeFrameView?.frame = CGRect.zero
scannerOutput.text = "NoQrCodeDetected.".localize()
}
}
func presentCameraSettings() {
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
alertController.addAction(UIAlertAction(title: "Settings".localize(), style: .cancel) { _ in
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url, options: [:]) { _ in }
UIApplication.shared.open(url)
}
}
)
present(alertController, animated: true)
}
}

View file

@ -95,7 +95,9 @@ class SSHKeyArmorImportTableViewController: AutoCellHeightUITableViewController,
}
override func prepare(for segue: UIStoryboardSegue, sender _: Any?) {
if segue.identifier == "showSSHScannerSegue" {
guard segue.identifier == "showSSHScannerSegue" else {
return
}
if let navController = segue.destination as? UINavigationController {
if let viewController = navController.topViewController as? QRScannerController {
viewController.delegate = self
@ -104,7 +106,6 @@ class SSHKeyArmorImportTableViewController: AutoCellHeightUITableViewController,
viewController.delegate = self
}
}
}
@IBAction
private func cancelSSHScanner(segue _: UIStoryboardSegue) {}