Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension UIAlertController {
- // MARK: - Lifecycle
- convenience init(error: Error) {
- self.init(
- title: Localizable.errorTitle,
- message: error.localizedDescription,
- preferredStyle: .alert
- )
- let action = UIAlertAction(
- title: Localizable.buttonOk,
- style: .default,
- handler: nil
- )
- addAction(action)
- }
- struct Action<T> {
- var title: String?
- var style: UIAlertAction.Style
- var value: T?
- static func action(
- title: String?,
- style: UIAlertAction.Style = .default,
- value: T?) -> Action {
- return Action(
- title: title,
- style: style,
- value: value
- )
- }
- }
- static func present<T>(
- in viewController: UIViewController,
- title: String? = nil,
- message: String? = nil,
- style: UIAlertController.Style,
- actions: [Action<T>]) -> Observable<T> {
- return Observable.create { observer in
- let alertController = UIAlertController(
- title: title,
- message: message,
- preferredStyle: style
- )
- actions.enumerated().forEach { _, action in
- let alertAction = UIAlertAction(
- title: action.title,
- style: action.style) { _ in
- if action.style != .destructive {
- observer.onNext(action.value!)
- }
- observer.onCompleted()
- }
- alertController.addAction(alertAction)
- }
- viewController.present(alertController, animated: true, completion: nil)
- return Disposables.create()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement