6
6
// Copyright © 2016 Artem Antihevich. All rights reserved.
7
7
//
8
8
9
- import Foundation
10
- import UIKit
11
- import RxSwift
9
+ #if os(iOS) || os(tvOS)
10
+ import UIKit
11
+ #if !RX_NO_MODULE
12
+ import RxSwift
13
+ #endif
12
14
13
15
public extension Reactive where Base: UIViewController {
14
16
/// Observe viewDidLoad calls on current instance
15
17
public func onViewDidLoad( ) -> Observable < Void > {
16
- return ARViewControllerLifeCircleManager . instance. didLoad. filter { [ weak base] in $0 === base } . map { _ in ( ) }
18
+ return ARViewControllerLifeCycleManager . instance. didLoad. filter { [ weak base] in $0 === base } . map { _ in ( ) }
17
19
}
18
20
19
21
/// Observe viewWillAppear calls on current instance
20
22
public func onViewWillAppear( ) -> Observable < Bool > {
21
- return ARViewControllerLifeCircleManager . instance. willAppear. filter { [ weak base] in $0. controller === base } . map { $0. animated }
23
+ return ARViewControllerLifeCycleManager . instance. willAppear. filter { [ weak base] in $0. controller === base } . map { $0. animated }
22
24
}
23
25
24
26
/// Observe viewDidAppear calls on current instance
25
27
public func onViewDidAppear( ) -> Observable < Bool > {
26
- return ARViewControllerLifeCircleManager . instance. didAppear. filter { [ weak base] in $0. controller === base } . map { $0. animated }
28
+ return ARViewControllerLifeCycleManager . instance. didAppear. filter { [ weak base] in $0. controller === base } . map { $0. animated }
27
29
}
28
30
29
31
/// Observe viewWillDisappear calls on current instance
30
32
public func onViewWillDisappear( ) -> Observable < Bool > {
31
- return ARViewControllerLifeCircleManager . instance. willDisappear. filter { [ weak base] in $0. controller === base } . map { $0. animated }
33
+ return ARViewControllerLifeCycleManager . instance. willDisappear. filter { [ weak base] in $0. controller === base } . map { $0. animated }
32
34
}
33
35
34
36
/// Observe viewDidDisappear calls on current instance
35
37
public func onViewDidDisappear( ) -> Observable < Bool > {
36
- return ARViewControllerLifeCircleManager . instance. didDisappear. filter { [ weak base] in $0. controller === base } . map { $0. animated }
38
+ return ARViewControllerLifeCycleManager . instance. didDisappear. filter { [ weak base] in $0. controller === base } . map { $0. animated }
37
39
}
38
40
}
39
41
40
42
public extension Reactive where Base: UIViewController {
41
43
/// observe viewDidLoad calls on all instances of current type
42
44
public static func onViewDidLoad( ) -> Observable < Base > {
43
45
return Observable . create ( { observer -> Disposable in
44
- return ARViewControllerLifeCircleManager . instance. didLoad. subscribe ( onNext: { vc in
46
+ return ARViewControllerLifeCycleManager . instance. didLoad. subscribe ( onNext: { vc in
45
47
if let required = vc as? Base {
46
48
observer. onNext ( required)
47
49
}
@@ -52,7 +54,7 @@ public extension Reactive where Base: UIViewController {
52
54
/// observe viewWillAppear calls on all instances of current type
53
55
public static func onViewWillAppear( ) -> Observable < ( controller: Base , animated: Bool ) > {
54
56
return Observable . create ( { observer -> Disposable in
55
- return ARViewControllerLifeCircleManager . instance. willAppear. subscribe ( onNext: { ( vc, animated) in
57
+ return ARViewControllerLifeCycleManager . instance. willAppear. subscribe ( onNext: { ( vc, animated) in
56
58
if let required = vc as? Base {
57
59
observer. onNext ( ( required, animated) )
58
60
}
@@ -63,7 +65,7 @@ public extension Reactive where Base: UIViewController {
63
65
/// observe viewDidAppear calls on all instances of current type
64
66
public static func onViewDidAppear( ) -> Observable < ( controller: Base , animated: Bool ) > {
65
67
return Observable . create ( { observer -> Disposable in
66
- return ARViewControllerLifeCircleManager . instance. didAppear. subscribe ( onNext: { ( vc, animated) in
68
+ return ARViewControllerLifeCycleManager . instance. didAppear. subscribe ( onNext: { ( vc, animated) in
67
69
if let required = vc as? Base {
68
70
observer. onNext ( ( required, animated) )
69
71
}
@@ -74,7 +76,7 @@ public extension Reactive where Base: UIViewController {
74
76
/// observe viewWillDisappear calls on all instances of current type
75
77
public static func onViewWillDisappear( ) -> Observable < ( controller: Base , animated: Bool ) > {
76
78
return Observable . create ( { observer -> Disposable in
77
- return ARViewControllerLifeCircleManager . instance. willDisappear. subscribe ( onNext: { ( vc, animated) in
79
+ return ARViewControllerLifeCycleManager . instance. willDisappear. subscribe ( onNext: { ( vc, animated) in
78
80
if let required = vc as? Base {
79
81
observer. onNext ( ( required, animated) )
80
82
}
@@ -85,7 +87,7 @@ public extension Reactive where Base: UIViewController {
85
87
/// observe viewDidDisappear calls on all instances of current type
86
88
public static func onViewDidDisappear( ) -> Observable < ( controller: Base , animated: Bool ) > {
87
89
return Observable . create ( { observer -> Disposable in
88
- return ARViewControllerLifeCircleManager . instance. didDisappear. subscribe ( onNext: { ( vc, animated) in
90
+ return ARViewControllerLifeCycleManager . instance. didDisappear. subscribe ( onNext: { ( vc, animated) in
89
91
if let required = vc as? Base {
90
92
observer. onNext ( ( required, animated) )
91
93
}
@@ -94,17 +96,17 @@ public extension Reactive where Base: UIViewController {
94
96
}
95
97
}
96
98
97
- private class ARViewControllerLifeCircleManager {
99
+ private class ARViewControllerLifeCycleManager {
98
100
private static var __once : ( ) = {
99
101
swapMethods ( #selector( UIViewController . viewDidLoad) , swizzled: #selector( UIViewController . ar_viewDidLoad) )
100
102
swapMethods ( #selector( UIViewController . viewWillAppear ( _: ) ) , swizzled: #selector( UIViewController . ar_viewWillAppear ( _: ) ) )
101
103
swapMethods ( #selector( UIViewController . viewDidAppear ( _: ) ) , swizzled: #selector( UIViewController . ar_viewDidAppear ( _: ) ) )
102
104
swapMethods ( #selector( UIViewController . viewWillDisappear ( _: ) ) , swizzled: #selector( UIViewController . ar_viewWillDisappear ( _: ) ) )
103
105
swapMethods ( #selector( UIViewController . viewDidDisappear ( _: ) ) , swizzled: #selector( UIViewController . ar_viewDidDisappear ( _: ) ) )
104
106
} ( )
105
- static let instance : ARViewControllerLifeCircleManager = {
107
+ static let instance : ARViewControllerLifeCycleManager = {
106
108
_ = __once
107
- return ARViewControllerLifeCircleManager ( )
109
+ return ARViewControllerLifeCycleManager ( )
108
110
} ( )
109
111
fileprivate let didLoad = PublishSubject < UIViewController > ( )
110
112
fileprivate let willAppear = PublishSubject < ( controller: UIViewController , animated: Bool ) > ( )
@@ -113,8 +115,8 @@ private class ARViewControllerLifeCircleManager {
113
115
fileprivate let didDisappear = PublishSubject < ( controller: UIViewController , animated: Bool ) > ( )
114
116
115
117
fileprivate class func swapMethods( _ original: Selector , swizzled: Selector ) {
116
- let originalMethod = class_getInstanceMethod ( UIViewController . self, original)
117
- let swizzledMethod = class_getInstanceMethod ( UIViewController . self, swizzled)
118
+ guard let originalMethod = class_getInstanceMethod ( UIViewController . self, original) ,
119
+ let swizzledMethod = class_getInstanceMethod ( UIViewController . self, swizzled) else { return }
118
120
let didAddMethod = class_addMethod ( UIViewController . self, original, method_getImplementation ( swizzledMethod) , method_getTypeEncoding ( swizzledMethod) )
119
121
if didAddMethod { class_replaceMethod ( UIViewController . self, swizzled, method_getImplementation ( originalMethod) , method_getTypeEncoding ( originalMethod) ) }
120
122
else { method_exchangeImplementations ( originalMethod, swizzledMethod) }
@@ -124,22 +126,24 @@ private class ARViewControllerLifeCircleManager {
124
126
extension UIViewController {
125
127
@objc fileprivate func ar_viewDidLoad( ) -> ( ) {
126
128
self . ar_viewDidLoad ( )
127
- ARViewControllerLifeCircleManager . instance. didLoad. onNext ( self )
129
+ ARViewControllerLifeCycleManager . instance. didLoad. onNext ( self )
128
130
}
129
131
@objc fileprivate func ar_viewWillAppear( _ animated: Bool ) -> ( ) {
130
132
self . ar_viewWillAppear ( animated)
131
- ARViewControllerLifeCircleManager . instance. willAppear. onNext ( ( self , animated) )
133
+ ARViewControllerLifeCycleManager . instance. willAppear. onNext ( ( self , animated) )
132
134
}
133
135
@objc fileprivate func ar_viewDidAppear( _ animated: Bool ) -> ( ) {
134
136
self . ar_viewDidAppear ( animated)
135
- ARViewControllerLifeCircleManager . instance. didAppear. onNext ( ( self , animated: animated) )
137
+ ARViewControllerLifeCycleManager . instance. didAppear. onNext ( ( self , animated: animated) )
136
138
}
137
139
@objc fileprivate func ar_viewWillDisappear( _ animated: Bool ) -> ( ) {
138
140
self . ar_viewWillDisappear ( animated)
139
- ARViewControllerLifeCircleManager . instance. willDisappear. onNext ( ( self , animated: animated) )
141
+ ARViewControllerLifeCycleManager . instance. willDisappear. onNext ( ( self , animated: animated) )
140
142
}
141
143
@objc fileprivate func ar_viewDidDisappear( _ animated: Bool ) -> ( ) {
142
144
self . ar_viewDidDisappear ( animated)
143
- ARViewControllerLifeCircleManager . instance. didDisappear. onNext ( ( self , animated: animated) )
145
+ ARViewControllerLifeCycleManager . instance. didDisappear. onNext ( ( self , animated: animated) )
144
146
}
145
147
}
148
+
149
+ #endif
0 commit comments