UIAlertTemplate.swift 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. @objc public class UIAlertTemplate: NSObject {
  22. @objc public static func showAlert(owner: UIViewController, title: String?, message: String?, actionOk: ((UIAlertAction) -> Void)? = nil) {
  23. let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
  24. alert.addAction(UIAlertAction(title: NSLocalizedString("ok", comment: ""), style: .default, handler: actionOk))
  25. owner.present(alert, animated: true, completion: nil);
  26. }
  27. @objc public static func showAlert(owner: UIViewController, title: String?, message: String?, actionOk: ((UIAlertAction) -> Void)? = nil, actionCancel: ((UIAlertAction) -> Void)? = nil) {
  28. let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
  29. alert.addAction(UIAlertAction(title: NSLocalizedString("ok", comment: ""), style: .default, handler: actionOk))
  30. alert.addAction(UIAlertAction(title: NSLocalizedString("cancel", comment: ""), style: .default, handler: actionCancel))
  31. owner.present(alert, animated: true, completion: nil);
  32. }
  33. @objc public static func showAlert(owner: UIViewController, title: String?, message: String?, titleOk: String, actionOk: ((UIAlertAction) -> Void)? = nil, titleCancel: String, actionCancel: ((UIAlertAction) -> Void)? = nil) {
  34. let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
  35. alert.addAction(UIAlertAction(title: titleOk, style: .default, handler: actionOk))
  36. alert.addAction(UIAlertAction(title: titleCancel, style: .default, handler: actionCancel))
  37. owner.present(alert, animated: true, completion: nil);
  38. }
  39. @objc public static func showDestructiveAlert(owner: UIViewController, title: String?, message: String?, titleDestructive: String, actionDestructive: ((UIAlertAction) -> Void)? = nil, titleCancel: String, actionCancel: ((UIAlertAction) -> Void)? = nil) {
  40. let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
  41. alert.addAction(UIAlertAction(title: titleCancel, style: .cancel, handler: actionCancel))
  42. alert.addAction(UIAlertAction(title: titleDestructive, style: .destructive, handler: actionDestructive))
  43. owner.present(alert, animated: true, completion: nil);
  44. }
  45. public static func showConfirm(owner: UIViewController, popOverSource: UIView, title: String, message: String?, titleOk: String, actionOk: ((UIAlertAction) -> Void)? = nil, titleCancel: String, actionCancel: ((UIAlertAction) -> Void)? = nil) {
  46. let confirm = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.actionSheet)
  47. confirm.addAction(UIAlertAction(title: titleCancel, style: .cancel, handler: actionCancel))
  48. confirm.addAction(UIAlertAction(title: titleOk, style: .destructive, handler: actionOk))
  49. let popOver = confirm.popoverPresentationController
  50. popOver?.sourceView = popOverSource
  51. popOver?.sourceRect = popOverSource.bounds
  52. popOver?.permittedArrowDirections = .any
  53. owner.present(confirm, animated: true, completion: nil)
  54. }
  55. public static func showPicker(owner: UIViewController, title: String, message: String, options: [String: ((UIAlertAction) -> Void)?], titleCancel:String, actionCancel:((UIAlertAction) -> Void)? = nil) {
  56. let picker = UIAlertController(title: title, message: message, preferredStyle: .alert)
  57. options.forEach { (arg0) in
  58. let (key, action) = arg0
  59. picker.addAction(UIAlertAction(title: key, style: .default, handler: action))
  60. }
  61. picker.addAction(UIAlertAction(title: titleCancel, style: .cancel, handler: actionCancel))
  62. owner.present(picker, animated: true, completion: nil);
  63. }
  64. }