Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // UIAlertController+Rx.swift
- //
- import Foundation
- import RxSwift
- protocol RxAlertActionType {
- associatedtype Result
- var title: String? { get }
- var style: UIAlertActionStyle { get }
- var result: Result { get }
- }
- struct RxAlertAction<R>: RxAlertActionType {
- typealias Result = R
- let title: String?
- let style: UIAlertActionStyle
- let result: R
- }
- struct RxDefaultAlertAction: RxAlertActionType {
- typealias Result = RxAlertControllerResult
- let title: String?
- let style: UIAlertActionStyle
- let result: Result
- }
- enum RxAlertControllerResult {
- case Ok
- }
- extension UIAlertController {
- static func rx_presentAlert<Action: RxAlertActionType, Result where Action.Result == Result>(viewController: UIViewController, title: String, message: String, preferredStyle: UIAlertControllerStyle = .Alert, animated: Bool = true, actions: [Action]) -> Observable<Result> {
- return Observable.create { observer -> Disposable in
- let alertController = UIAlertController(title: title, message: message, preferredStyle: preferredStyle)
- actions.map { rxAction in
- UIAlertAction(title: rxAction.title, style: rxAction.style, handler: { _ in
- observer.onNext(rxAction.result)
- observer.onCompleted()
- })
- }
- .forEach(alertController.addAction)
- viewController.presentViewController(alertController, animated: animated, completion: nil)
- return AnonymousDisposable { _ in
- alertController.dismissViewControllerAnimated(true, completion: nil)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement