simplify alert code

This commit is contained in:
Bob Sun 2017-02-16 00:54:42 +08:00
parent 5ed3629c52
commit edfd8c97a7
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
7 changed files with 21 additions and 40 deletions

View file

@ -26,10 +26,9 @@ class EditPasswordTableViewController: PasswordEditorTableViewController {
if identifier == "saveEditPasswordSegue" { if identifier == "saveEditPasswordSegue" {
let nameCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! ContentTableViewCell let nameCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! ContentTableViewCell
if nameCell.getContent() != password?.name { if nameCell.getContent() != password?.name {
let alertTitle = "Cannot Save Edit"
let alertMessage = "Editing name is not supported." let alertMessage = "Editing name is not supported."
let alert = UIAlertController(title: "Cannot Save Edit", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert) Utils.alert(title: alertTitle, message: alertMessage, controller: self) {
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true) {
nameCell.setContent(content: self.password!.name) nameCell.setContent(content: self.password!.name)
} }
return false return false

View file

@ -48,9 +48,8 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController {
func tapHideUnknownSwitchDetailButton(_ sender: Any?) { 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 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) let alertTitle = "Hide Unknown Fields"
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil)
self.present(alert, animated: true, completion: nil)
} }
func hideUnknownSwitchAction(_ sender: Any?) { func hideUnknownSwitchAction(_ sender: Any?) {

View file

@ -38,11 +38,7 @@ class GitServerSettingTableViewController: UITableViewController {
if authenticationMethod == "SSH Key" { if authenticationMethod == "SSH Key" {
if Defaults[.gitRepositorySSHPublicKeyURL] == nil && Defaults[.gitRepositorySSHPrivateKeyURL] == nil { if Defaults[.gitRepositorySSHPublicKeyURL] == nil && Defaults[.gitRepositorySSHPrivateKeyURL] == nil {
authenticationMethod = "Password" authenticationMethod = "Password"
Utils.alert(title: "Cannot Select SSH Key", message: "Please setup SSH key first.", controller: self, completion: nil)
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)
} else { } else {
authenticationMethod = "SSH Key" authenticationMethod = "SSH Key"
} }
@ -65,9 +61,7 @@ class GitServerSettingTableViewController: UITableViewController {
if authenticationMethod == nil { if authenticationMethod == nil {
alertMessage = "Authentication method is not set. Please set your authentication method first." alertMessage = "Authentication method is not set. Please set your authentication method first."
} }
let alert = UIAlertController(title: "Cannot Save Settings", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert) Utils.alert(title: "Cannot Save Settings", message: alertMessage, controller: self, completion: nil)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
return false return false
} }
} }

View file

@ -32,26 +32,17 @@ class PGPKeySettingTableViewController: UITableViewController {
} }
guard URL(string: pgpPublicKeyURLTextField.text!) != nil else { guard URL(string: pgpPublicKeyURLTextField.text!) != nil else {
let alertMessage = "Please set Public Key URL first." Utils.alert(title: "Cannot Save", message: "Please set Public Key URL first.", controller: self, completion: nil)
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)
return false return false
} }
guard URL(string: pgpPrivateKeyURLTextField.text!) != nil else { guard URL(string: pgpPrivateKeyURLTextField.text!) != nil else {
let alertMessage = "Please set Private Key URL first." Utils.alert(title: "Cannot Save", message: "Please set Private Key URL first.", controller: self, completion: nil)
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)
return false return false
} }
if URL(string: pgpPublicKeyURLTextField.text!)!.scheme! == "http" && if URL(string: pgpPublicKeyURLTextField.text!)!.scheme! == "http" &&
URL(string: pgpPrivateKeyURLTextField.text!)!.scheme! == "http" { URL(string: pgpPrivateKeyURLTextField.text!)!.scheme! == "http" {
let alertMessage = "HTTP connection is not supported." Utils.alert(title: "Cannot Save Settings", message: "HTTP connection is not supported.", controller: self, completion: nil)
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)
return false return false
} }
} }

View file

@ -179,9 +179,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
func copyToPasteboard(from indexPath: IndexPath) { func copyToPasteboard(from indexPath: IndexPath) {
if Defaults[.pgpKeyID] == nil { 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) Utils.alert(title: "Cannot Copy Password", message: "PGP Key is not set. Please set your PGP Key first.", controller: self, completion: nil)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
return return
} }
let index = sections[indexPath.section].index + indexPath.row 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 { override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "showPasswordDetail" { if identifier == "showPasswordDetail" {
if Defaults[.pgpKeyID] == nil { 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) Utils.alert(title: "Cannot Show Password", message: "PGP Key is not set. Please set your PGP Key first.", controller: self, completion: nil)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
if let s = sender as? UITableViewCell { if let s = sender as? UITableViewCell {
let selectedIndexPath = tableView.indexPath(for: s)! let selectedIndexPath = tableView.indexPath(for: s)!
tableView.deselectRow(at: selectedIndexPath, animated: true) tableView.deselectRow(at: selectedIndexPath, animated: true)

View file

@ -33,17 +33,11 @@ class SSHKeySettingTableViewController: UITableViewController {
func doneButtonTapped(_ sender: UIButton) { func doneButtonTapped(_ sender: UIButton) {
guard URL(string: publicKeyURLTextField.text!) != nil else { guard URL(string: publicKeyURLTextField.text!) != nil else {
let alertMessage = "Please set Public Key URL first." Utils.alert(title: "Cannot Save", message: "Please set Public Key URL first.", controller: self, completion: nil)
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)
return return
} }
guard URL(string: privateKeyURLTextField.text!) != nil else { guard URL(string: privateKeyURLTextField.text!) != nil else {
let alertMessage = "Please set Private Key URL first." Utils.alert(title: "Cannot Save", message: "Please set Private Key URL first.", controller: self, completion: nil)
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)
return return
} }

View file

@ -8,6 +8,7 @@
import Foundation import Foundation
import SwiftyUserDefaults import SwiftyUserDefaults
import UIKit
class Utils { class Utils {
static func removeFileIfExists(atPath path: String) { static func removeFileIfExists(atPath path: String) {
@ -50,6 +51,13 @@ class Utils {
return randomString 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 // https://gist.github.com/NikolaiRuhe/eeb135d20c84a7097516