diff --git a/pass/Controllers/EditPasswordTableViewController.swift b/pass/Controllers/EditPasswordTableViewController.swift index f8bbaa7..80fd504 100644 --- a/pass/Controllers/EditPasswordTableViewController.swift +++ b/pass/Controllers/EditPasswordTableViewController.swift @@ -26,10 +26,9 @@ class EditPasswordTableViewController: PasswordEditorTableViewController { if identifier == "saveEditPasswordSegue" { let nameCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! ContentTableViewCell if nameCell.getContent() != password?.name { + let alertTitle = "Cannot Save Edit" let alertMessage = "Editing name is not supported." - let alert = UIAlertController(title: "Cannot Save Edit", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert) - alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) - self.present(alert, animated: true) { + Utils.alert(title: alertTitle, message: alertMessage, controller: self) { nameCell.setContent(content: self.password!.name) } return false diff --git a/pass/Controllers/GeneralSettingsTableViewController.swift b/pass/Controllers/GeneralSettingsTableViewController.swift index aa8a4e1..2e08045 100644 --- a/pass/Controllers/GeneralSettingsTableViewController.swift +++ b/pass/Controllers/GeneralSettingsTableViewController.swift @@ -48,9 +48,8 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController { func tapHideUnknownSwitchDetailButton(_ sender: Any?) { let alertMessage = "Only \"key: value\" format in additional fields is supported. Unsupported fields will be given \"unkown\" keys. Turn on this switch to hide unsupported fields." - let alert = UIAlertController(title: "Hide Unknown Fields", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert) - alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) - self.present(alert, animated: true, completion: nil) + let alertTitle = "Hide Unknown Fields" + Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil) } func hideUnknownSwitchAction(_ sender: Any?) { diff --git a/pass/Controllers/GitServerSettingTableViewController.swift b/pass/Controllers/GitServerSettingTableViewController.swift index 23fec79..3d50945 100644 --- a/pass/Controllers/GitServerSettingTableViewController.swift +++ b/pass/Controllers/GitServerSettingTableViewController.swift @@ -38,11 +38,7 @@ class GitServerSettingTableViewController: UITableViewController { if authenticationMethod == "SSH Key" { if Defaults[.gitRepositorySSHPublicKeyURL] == nil && Defaults[.gitRepositorySSHPrivateKeyURL] == nil { authenticationMethod = "Password" - - let alertController = UIAlertController(title: "Attention", message: "Please setup SSH key first.", preferredStyle: .alert) - let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) - alertController.addAction(defaultAction) - present(alertController, animated: true, completion: nil) + Utils.alert(title: "Cannot Select SSH Key", message: "Please setup SSH key first.", controller: self, completion: nil) } else { authenticationMethod = "SSH Key" } @@ -65,9 +61,7 @@ class GitServerSettingTableViewController: UITableViewController { if authenticationMethod == nil { alertMessage = "Authentication method is not set. Please set your authentication method first." } - let alert = UIAlertController(title: "Cannot Save Settings", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert) - alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) - self.present(alert, animated: true, completion: nil) + Utils.alert(title: "Cannot Save Settings", message: alertMessage, controller: self, completion: nil) return false } } diff --git a/pass/Controllers/PGPKeySettingTableViewController.swift b/pass/Controllers/PGPKeySettingTableViewController.swift index cbfb628..af8e76c 100644 --- a/pass/Controllers/PGPKeySettingTableViewController.swift +++ b/pass/Controllers/PGPKeySettingTableViewController.swift @@ -32,26 +32,17 @@ class PGPKeySettingTableViewController: UITableViewController { } guard URL(string: pgpPublicKeyURLTextField.text!) != nil else { - let alertMessage = "Please set Public Key URL first." - let alert = UIAlertController(title: "Cannot Save", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert) - alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) - self.present(alert, animated: true, completion: nil) + Utils.alert(title: "Cannot Save", message: "Please set Public Key URL first.", controller: self, completion: nil) return false } guard URL(string: pgpPrivateKeyURLTextField.text!) != nil else { - let alertMessage = "Please set Private Key URL first." - let alert = UIAlertController(title: "Cannot Save", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert) - alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) - self.present(alert, animated: true, completion: nil) + Utils.alert(title: "Cannot Save", message: "Please set Private Key URL first.", controller: self, completion: nil) return false } if URL(string: pgpPublicKeyURLTextField.text!)!.scheme! == "http" && URL(string: pgpPrivateKeyURLTextField.text!)!.scheme! == "http" { - let alertMessage = "HTTP connection is not supported." - let alert = UIAlertController(title: "Cannot Save Settings", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert) - alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) - self.present(alert, animated: true, completion: nil) + Utils.alert(title: "Cannot Save Settings", message: "HTTP connection is not supported.", controller: self, completion: nil) return false } } diff --git a/pass/Controllers/PasswordsViewController.swift b/pass/Controllers/PasswordsViewController.swift index 38ad12b..68b8a00 100644 --- a/pass/Controllers/PasswordsViewController.swift +++ b/pass/Controllers/PasswordsViewController.swift @@ -179,9 +179,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV func copyToPasteboard(from indexPath: IndexPath) { if Defaults[.pgpKeyID] == nil { - let alert = UIAlertController(title: "Cannot Copy Password", message: "PGP Key is not set. Please set your PGP Key first.", preferredStyle: UIAlertControllerStyle.alert) - alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) - self.present(alert, animated: true, completion: nil) + Utils.alert(title: "Cannot Copy Password", message: "PGP Key is not set. Please set your PGP Key first.", controller: self, completion: nil) return } let index = sections[indexPath.section].index + indexPath.row @@ -263,9 +261,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { if identifier == "showPasswordDetail" { if Defaults[.pgpKeyID] == nil { - let alert = UIAlertController(title: "Cannot Show Password", message: "PGP Key is not set. Please set your PGP Key first.", preferredStyle: UIAlertControllerStyle.alert) - alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) - self.present(alert, animated: true, completion: nil) + Utils.alert(title: "Cannot Show Password", message: "PGP Key is not set. Please set your PGP Key first.", controller: self, completion: nil) if let s = sender as? UITableViewCell { let selectedIndexPath = tableView.indexPath(for: s)! tableView.deselectRow(at: selectedIndexPath, animated: true) diff --git a/pass/Controllers/SSHKeySettingTableViewController.swift b/pass/Controllers/SSHKeySettingTableViewController.swift index 6725af7..65a672e 100644 --- a/pass/Controllers/SSHKeySettingTableViewController.swift +++ b/pass/Controllers/SSHKeySettingTableViewController.swift @@ -33,17 +33,11 @@ class SSHKeySettingTableViewController: UITableViewController { func doneButtonTapped(_ sender: UIButton) { guard URL(string: publicKeyURLTextField.text!) != nil else { - let alertMessage = "Please set Public Key URL first." - let alert = UIAlertController(title: "Cannot Save", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert) - alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) - self.present(alert, animated: true, completion: nil) + Utils.alert(title: "Cannot Save", message: "Please set Public Key URL first.", controller: self, completion: nil) return } guard URL(string: privateKeyURLTextField.text!) != nil else { - let alertMessage = "Please set Private Key URL first." - let alert = UIAlertController(title: "Cannot Save", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert) - alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) - self.present(alert, animated: true, completion: nil) + Utils.alert(title: "Cannot Save", message: "Please set Private Key URL first.", controller: self, completion: nil) return } diff --git a/pass/Helpers/Utils.swift b/pass/Helpers/Utils.swift index f7f2bcd..4141583 100644 --- a/pass/Helpers/Utils.swift +++ b/pass/Helpers/Utils.swift @@ -8,6 +8,7 @@ import Foundation import SwiftyUserDefaults +import UIKit class Utils { static func removeFileIfExists(atPath path: String) { @@ -50,6 +51,13 @@ class Utils { return randomString } + + static func alert(title: String, message: String, controller: UIViewController, completion: (() -> Void)?) { + let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) + alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) + controller.present(alert, animated: true, completion: completion) + + } } // https://gist.github.com/NikolaiRuhe/eeb135d20c84a7097516