2022-01-09 21:38:39 -08:00
|
|
|
//
|
|
|
|
|
// AlertPresenting.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Copyright © 2022 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
|
|
public typealias AlertAction = (UIAlertAction) -> Void
|
|
|
|
|
|
|
|
|
|
public protocol AlertPresenting {
|
|
|
|
|
func presentAlert(title: String, message: String)
|
|
|
|
|
func presentFailureAlert(title: String?, message: String, action: AlertAction?)
|
|
|
|
|
func presentAlertWithAction(title: String, message: String, action: AlertAction?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public extension AlertPresenting where Self: UIViewController {
|
|
|
|
|
func presentAlert(title: String, message: String) {
|
|
|
|
|
presentAlert(
|
|
|
|
|
title: title,
|
|
|
|
|
message: message,
|
|
|
|
|
actions: [UIAlertAction(title: "OK", style: .cancel, handler: nil)]
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 22:01:37 +02:00
|
|
|
// swiftlint:disable:next function_default_parameter_at_end
|
2022-01-09 21:38:39 -08:00
|
|
|
func presentFailureAlert(title: String? = nil, message: String, action: AlertAction? = nil) {
|
|
|
|
|
let title = title ?? "Error"
|
|
|
|
|
presentAlert(
|
|
|
|
|
title: title,
|
|
|
|
|
message: message,
|
|
|
|
|
actions: [UIAlertAction(title: "OK", style: .cancel, handler: action)]
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func presentAlertWithAction(title: String, message: String, action: AlertAction?) {
|
|
|
|
|
presentAlert(
|
|
|
|
|
title: title,
|
|
|
|
|
message: message,
|
|
|
|
|
actions: [
|
|
|
|
|
UIAlertAction(title: "Yes", style: .default, handler: action),
|
|
|
|
|
UIAlertAction(title: "No", style: .cancel, handler: nil),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func presentAlert(title: String, message: String, actions: [UIAlertAction] = []) {
|
|
|
|
|
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
|
|
|
|
actions.forEach { action in
|
|
|
|
|
alertController.addAction(action)
|
|
|
|
|
}
|
|
|
|
|
present(alertController, animated: true, completion: nil)
|
|
|
|
|
}
|
|
|
|
|
}
|