session-ios/SignalUtilitiesKit/Utilities/OWSAlerts.swift

79 lines
3.0 KiB
Swift
Raw Normal View History

2017-05-08 18:34:50 +02:00
//
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
2017-05-08 18:34:50 +02:00
//
import Foundation
import SessionUtilitiesKit
2017-05-08 18:34:50 +02:00
@objc public class OWSAlerts: NSObject {
2018-08-08 18:49:21 +02:00
@objc
public class func showAlert(_ alert: UIAlertController) {
guard let frontmostViewController = CurrentAppContext().frontmostViewController() else {
2018-08-27 16:27:48 +02:00
owsFailDebug("frontmostViewController was unexpectedly nil")
2018-08-08 18:49:21 +02:00
return
}
2019-03-21 15:26:38 +01:00
frontmostViewController.presentAlert(alert)
2018-08-08 18:49:21 +02:00
}
@objc
public class func showAlert(title: String) {
self.showAlert(title: title, message: nil, buttonTitle: nil)
}
@objc
public class func showAlert(title: String?, message: String) {
self.showAlert(title: title, message: message, buttonTitle: nil)
2017-05-08 19:29:10 +02:00
}
@objc
public class func showAlert(title: String?, message: String? = nil, buttonTitle: String? = nil, buttonAction: ((UIAlertAction) -> Void)? = nil) {
2018-04-16 23:39:13 +02:00
guard let fromViewController = CurrentAppContext().frontmostViewController() else {
return
}
showAlert(title: title, message: message, buttonTitle: buttonTitle, buttonAction: buttonAction,
fromViewController: fromViewController)
}
@objc
public class func showAlert(title: String?, message: String? = nil, buttonTitle: String? = nil, buttonAction: ((UIAlertAction) -> Void)? = nil, fromViewController: UIViewController?) {
2017-05-09 16:45:20 +02:00
2017-05-08 19:29:10 +02:00
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let actionTitle = buttonTitle ?? NSLocalizedString("BUTTON_OK", comment: "")
let okAction = UIAlertAction(title: actionTitle, style: .default, handler: buttonAction)
okAction.accessibilityIdentifier = "OWSAlerts.\("ok")"
alert.addAction(okAction)
2019-03-21 15:26:38 +01:00
fromViewController?.presentAlert(alert)
2017-05-08 19:29:10 +02:00
}
@objc
public class func showConfirmationAlert(title: String, message: String? = nil, proceedTitle: String? = nil, proceedAction: @escaping (UIAlertAction) -> Void) {
assert(title.count > 0)
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(self.cancelAction)
let actionTitle = proceedTitle ?? NSLocalizedString("BUTTON_OK", comment: "")
let okAction = UIAlertAction(title: actionTitle, style: .default, handler: proceedAction)
okAction.accessibilityIdentifier = "OWSAlerts.\("ok")"
alert.addAction(okAction)
2019-03-21 15:26:38 +01:00
CurrentAppContext().frontmostViewController()?.presentAlert(alert)
}
2018-03-06 14:29:25 +01:00
@objc
2018-03-14 14:02:44 +01:00
public class func showErrorAlert(message: String) {
2018-03-06 21:40:29 +01:00
self.showAlert(title: CommonStrings.errorAlertTitle, message: message, buttonTitle: nil)
2018-03-06 14:29:25 +01:00
}
@objc
public class var cancelAction: UIAlertAction {
let action = UIAlertAction(title: CommonStrings.cancelButton, style: .cancel) { _ in
Logger.debug("Cancel item")
// Do nothing.
}
action.accessibilityIdentifier = "OWSAlerts.\("cancel")"
return action
}
2017-05-08 18:34:50 +02:00
}