forked from RxSwiftCommunity/Action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlertAction.swift
66 lines (58 loc) · 2.01 KB
/
AlertAction.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import UIKit
import RxSwift
import RxCocoa
public extension UIAlertAction {
public static func Action(title: String?, style: UIAlertActionStyle) -> UIAlertAction {
return UIAlertAction(title: title, style: style, handler: { action in
action.rx_action?.execute()
})
}
/// Binds enabled state of action to button, and subscribes to rx_tap to execute action.
/// These subscriptions are managed in a private, inaccessible dispose bag. To cancel
/// them, set the rx_action to nil or another action.
public var rx_action: CocoaAction? {
get {
var action: CocoaAction?
doLocked {
action = objc_getAssociatedObject(self, &AssociatedKeys.Action) as? Action
}
return action
}
set {
doLocked {
// Store new value.
objc_setAssociatedObject(self, &AssociatedKeys.Action, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
// This effectively disposes of any existing subscriptions.
self.resetActionDisposeBag()
// Set up new bindings, if applicable.
if let action = newValue {
action
.enabled
.bindTo(self.rx_enabled)
.addDisposableTo(self.actionDisposeBag)
}
}
}
}
}
extension UIAlertAction {
var rx_enabled: AnyObserver<Bool> {
return AnyObserver { [weak self] event in
MainScheduler.ensureExecutingOnScheduler()
switch event {
case .Next(let value):
self?.enabled = value
case .Error(let error):
let error = "Binding error to UI: \(error)"
#if DEBUG
rxFatalError(error)
#else
print(error)
#endif
break
case .Completed:
break
}
}
}
}