Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. //
  2. // UIAlertController+Rx.swift
  3. //
  4.  
  5. import Foundation
  6. import RxSwift
  7.  
  8. protocol RxAlertActionType {
  9. associatedtype Result
  10.  
  11. var title: String? { get }
  12. var style: UIAlertActionStyle { get }
  13. var result: Result { get }
  14. }
  15.  
  16. struct RxAlertAction<R>: RxAlertActionType {
  17. typealias Result = R
  18.  
  19. let title: String?
  20. let style: UIAlertActionStyle
  21. let result: R
  22. }
  23.  
  24. struct RxDefaultAlertAction: RxAlertActionType {
  25. typealias Result = RxAlertControllerResult
  26.  
  27. let title: String?
  28. let style: UIAlertActionStyle
  29. let result: Result
  30. }
  31.  
  32. enum RxAlertControllerResult {
  33. case Ok
  34. }
  35.  
  36. extension UIAlertController {
  37. 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> {
  38. return Observable.create { observer -> Disposable in
  39. let alertController = UIAlertController(title: title, message: message, preferredStyle: preferredStyle)
  40.  
  41. actions.map { rxAction in
  42. UIAlertAction(title: rxAction.title, style: rxAction.style, handler: { _ in
  43. observer.onNext(rxAction.result)
  44. observer.onCompleted()
  45. })
  46. }
  47. .forEach(alertController.addAction)
  48.  
  49. viewController.presentViewController(alertController, animated: animated, completion: nil)
  50.  
  51. return AnonymousDisposable { _ in
  52. alertController.dismissViewControllerAnimated(true, completion: nil)
  53. }
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement