Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. extension UIAlertController {
  2.  
  3. // MARK: - Lifecycle
  4.  
  5. convenience init(error: Error) {
  6. self.init(
  7. title: Localizable.errorTitle,
  8. message: error.localizedDescription,
  9. preferredStyle: .alert
  10. )
  11. let action = UIAlertAction(
  12. title: Localizable.buttonOk,
  13. style: .default,
  14. handler: nil
  15. )
  16. addAction(action)
  17. }
  18.  
  19. struct Action<T> {
  20. var title: String?
  21. var style: UIAlertAction.Style
  22. var value: T?
  23.  
  24. static func action(
  25. title: String?,
  26. style: UIAlertAction.Style = .default,
  27. value: T?) -> Action {
  28. return Action(
  29. title: title,
  30. style: style,
  31. value: value
  32. )
  33. }
  34. }
  35.  
  36. static func present<T>(
  37. in viewController: UIViewController,
  38. title: String? = nil,
  39. message: String? = nil,
  40. style: UIAlertController.Style,
  41. actions: [Action<T>]) -> Observable<T> {
  42. return Observable.create { observer in
  43. let alertController = UIAlertController(
  44. title: title,
  45. message: message,
  46. preferredStyle: style
  47. )
  48. actions.enumerated().forEach { _, action in
  49. let alertAction = UIAlertAction(
  50. title: action.title,
  51. style: action.style) { _ in
  52. if action.style != .destructive {
  53. observer.onNext(action.value!)
  54. }
  55. observer.onCompleted()
  56. }
  57. alertController.addAction(alertAction)
  58. }
  59. viewController.present(alertController, animated: true, completion: nil)
  60. return Disposables.create()
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement