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) let selectedCell = tableView.cellForRow(at: indexPath)
if selectedCell == scanPublicKeyCell { if selectedCell == scanPublicKeyCell {
scanned.reset(keytype: ScannedPGPKey.KeyType.publicKey) scanned.reset(keytype: ScannedPGPKey.KeyType.publicKey)
performSegue(withIdentifier: "showPGPScannerSegue", sender: self)
} else if selectedCell == scanPrivateKeyCell { } else if selectedCell == scanPrivateKeyCell {
scanned.reset(keytype: ScannedPGPKey.KeyType.privateKey) scanned.reset(keytype: ScannedPGPKey.KeyType.privateKey)
performSegue(withIdentifier: "showPGPScannerSegue", sender: self)
} }
performSegue(withIdentifier: "showPGPScannerSegue", sender: self)
tableView.deselectRow(at: indexPath, animated: true) tableView.deselectRow(at: indexPath, animated: true)
} }
@ -78,14 +77,15 @@ class PGPKeyArmorImportTableViewController: AutoCellHeightUITableViewController,
} }
override func prepare(for segue: UIStoryboardSegue, sender _: Any?) { override func prepare(for segue: UIStoryboardSegue, sender _: Any?) {
if segue.identifier == "showPGPScannerSegue" { guard segue.identifier == "showPGPScannerSegue" else {
if let navController = segue.destination as? UINavigationController { return
if let viewController = navController.topViewController as? QRScannerController { }
viewController.delegate = self if let navController = segue.destination as? UINavigationController {
} if let viewController = navController.topViewController as? QRScannerController {
} else if let viewController = segue.destination as? QRScannerController {
viewController.delegate = self viewController.delegate = self
} }
} else if let viewController = segue.destination as? QRScannerController {
viewController.delegate = self
} }
} }
} }

View file

@ -419,9 +419,8 @@ extension PasswordEditorTableViewController: QRScannerControllerDelegate {
func checkScannedOutput(line: String) -> (accept: Bool, message: String) { func checkScannedOutput(line: String) -> (accept: Bool, message: String) {
if let url = URL(string: line), Token(url: url) != nil { if let url = URL(string: line), Token(url: url) != nil {
return (accept: true, message: "ValidTokenUrl".localize()) return (accept: true, message: "ValidTokenUrl".localize())
} else {
return (accept: false, message: "InvalidTokenUrl".localize())
} }
return (accept: false, message: "InvalidTokenUrl".localize())
} }
func handleScannedOutput(line: String) { func handleScannedOutput(line: String) {

View file

@ -86,61 +86,63 @@ class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDeleg
} }
} }
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - AVCaptureMetadataOutputObjectsDelegate Methods // MARK: - AVCaptureMetadataOutputObjectsDelegate Methods
func metadataOutput(_: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from _: AVCaptureConnection) { func metadataOutput(_: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from _: AVCaptureConnection) {
if let metadataObj = metadataObjects.first as? AVMetadataMachineReadableCodeObject, guard let metadataObj = metadataObjects.first as? AVMetadataMachineReadableCodeObject else {
supportedCodeTypes.contains(metadataObj.type), return setNotDetected()
let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj) { }
// draw a bounds on the found QR code guard supportedCodeTypes.contains(metadataObj.type) else {
qrCodeFrameView?.frame = barCodeObject.bounds return setNotDetected()
}
guard let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj) else {
return setNotDetected()
}
// check whether it is a valid result // draw a bounds on the found QR code
if let scanned = metadataObj.stringValue { qrCodeFrameView?.frame = barCodeObject.bounds
if let (accept, message) = delegate?.checkScannedOutput(line: scanned) {
scannerOutput.text = message // check whether it is a valid result
if accept == true { guard let scanned = metadataObj.stringValue else {
captureSession?.stopRunning() scannerOutput.text = "NoStringValue".localize()
delegate?.handleScannedOutput(line: scanned) return
DispatchQueue.main.async { }
SVProgressHUD.showSuccess(withStatus: "Done".localize()) guard let (accept, message) = delegate?.checkScannedOutput(line: scanned) else {
SVProgressHUD.dismiss(withDelay: 1) // no delegate, show the scanned result
self.navigationController?.popViewController(animated: true) scannerOutput.text = scanned
} return
} }
} else { scannerOutput.text = message
// no delegate, show the scanned result guard accept else {
scannerOutput.text = scanned return
} }
} else { captureSession?.stopRunning()
scannerOutput.text = "NoStringValue".localize() delegate?.handleScannedOutput(line: scanned)
} DispatchQueue.main.async {
} else { SVProgressHUD.showSuccess(withStatus: "Done".localize())
qrCodeFrameView?.frame = CGRect.zero SVProgressHUD.dismiss(withDelay: 1)
scannerOutput.text = "NoQrCodeDetected.".localize() self.navigationController?.popViewController(animated: true)
} }
} }
func presentCameraSettings() { private func setNotDetected() {
qrCodeFrameView?.frame = CGRect.zero
scannerOutput.text = "NoQrCodeDetected.".localize()
}
private func presentCameraSettings() {
let alertController = UIAlertController( let alertController = UIAlertController(
title: "Error".localize(), title: "Error".localize(),
message: "CameraAccessDenied.".localize() | "WarningToggleCameraPermissionsResetsApp.".localize(), message: "CameraAccessDenied.".localize() | "WarningToggleCameraPermissionsResetsApp.".localize(),
preferredStyle: .alert preferredStyle: .alert
) )
alertController.addAction(UIAlertAction(title: "Cancel".localize(), style: .default)) alertController.addAction(UIAlertAction(title: "Cancel".localize(), style: .default))
alertController.addAction( alertController.addAction(UIAlertAction(title: "Settings".localize(), style: .cancel) { _ in
UIAlertAction(title: "Settings".localize(), style: .cancel) { _ in if let url = URL(string: UIApplication.openSettingsURLString) {
if let url = URL(string: UIApplication.openSettingsURLString) { UIApplication.shared.open(url)
UIApplication.shared.open(url, options: [:]) { _ in }
}
} }
}
) )
present(alertController, animated: true) present(alertController, animated: true)
} }
} }

View file

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