Skip to content

Commit a31982d

Browse files
diogotkzaher
authored andcommitted
Add reative wrapper for UINavigationController delegate methods willShow and didShow
1 parent a097088 commit a31982d

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

Rx.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,12 @@
13571357
D2FC15B31BCB95E5007361FF /* SkipWhile.swift in Sources */ = {isa = PBXBuildFile; fileRef = D22B6D251BC8504A00BCE0AB /* SkipWhile.swift */; };
13581358
D2FC15B41BCB95E7007361FF /* SkipWhile.swift in Sources */ = {isa = PBXBuildFile; fileRef = D22B6D251BC8504A00BCE0AB /* SkipWhile.swift */; };
13591359
D2FC15B51BCB95E8007361FF /* SkipWhile.swift in Sources */ = {isa = PBXBuildFile; fileRef = D22B6D251BC8504A00BCE0AB /* SkipWhile.swift */; };
1360+
D9080ACF1EA05AE0002B433B /* RxNavigationControllerDelegateProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9080ACD1EA05A16002B433B /* RxNavigationControllerDelegateProxy.swift */; };
1361+
D9080AD01EA05AF7002B433B /* RxNavigationControllerDelegateProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9080ACD1EA05A16002B433B /* RxNavigationControllerDelegateProxy.swift */; };
1362+
D9080AD11EA05DA2002B433B /* RxNavigationControllerDelegateProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9080ACD1EA05A16002B433B /* RxNavigationControllerDelegateProxy.swift */; };
1363+
D9080AD41EA05DE9002B433B /* UINavigationController+Rx.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9080AD21EA05DDF002B433B /* UINavigationController+Rx.swift */; };
1364+
D9080AD51EA05DEB002B433B /* UINavigationController+Rx.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9080AD21EA05DDF002B433B /* UINavigationController+Rx.swift */; };
1365+
D9080AD61EA05DEC002B433B /* UINavigationController+Rx.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9080AD21EA05DDF002B433B /* UINavigationController+Rx.swift */; };
13601366
EB15145D1DFFAACB00555E2C /* Optional.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB15145C1DFFAACB00555E2C /* Optional.swift */; };
13611367
EB15145E1DFFAACB00555E2C /* Optional.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB15145C1DFFAACB00555E2C /* Optional.swift */; };
13621368
EB15145F1DFFAACB00555E2C /* Optional.swift in Sources */ = {isa = PBXBuildFile; fileRef = EB15145C1DFFAACB00555E2C /* Optional.swift */; };
@@ -2030,6 +2036,8 @@
20302036
D285BAC31BC0231000B3F602 /* SkipUntil.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SkipUntil.swift; sourceTree = "<group>"; };
20312037
D2EA280C1BB9B5A200880ED3 /* RxSwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RxSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; };
20322038
D2EBEB811BB9B99D003A27DC /* RxBlocking.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RxBlocking.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2039+
D9080ACD1EA05A16002B433B /* RxNavigationControllerDelegateProxy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RxNavigationControllerDelegateProxy.swift; sourceTree = "<group>"; };
2040+
D9080AD21EA05DDF002B433B /* UINavigationController+Rx.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UINavigationController+Rx.swift"; sourceTree = "<group>"; };
20332041
EB15145C1DFFAACB00555E2C /* Optional.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Optional.swift; sourceTree = "<group>"; };
20342042
ECBBA59A1DF8C0BA00DDDC2E /* UITabBarController+Rx.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UITabBarController+Rx.swift"; sourceTree = "<group>"; };
20352043
ECBBA59D1DF8C0D400DDDC2E /* RxTabBarControllerDelegateProxy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RxTabBarControllerDelegateProxy.swift; sourceTree = "<group>"; };
@@ -2761,6 +2769,7 @@
27612769
7F600F3D1C5D0C0100535B1D /* UIRefreshControl+Rx.swift */,
27622770
C882540D1B8A752B00B02D69 /* UIScrollView+Rx.swift */,
27632771
C882540E1B8A752B00B02D69 /* UISearchBar+Rx.swift */,
2772+
D9080AD21EA05DDF002B433B /* UINavigationController+Rx.swift */,
27642773
C882540F1B8A752B00B02D69 /* UISegmentedControl+Rx.swift */,
27652774
C88254101B8A752B00B02D69 /* UISlider+Rx.swift */,
27662775
F31F35AF1BB4FED800961002 /* UIStepper+Rx.swift */,
@@ -2824,6 +2833,7 @@
28242833
84C225A21C33F00B008724EC /* RxTextStorageDelegateProxy.swift */,
28252834
846436E11C9AF64C0035B40D /* RxSearchControllerDelegateProxy.swift */,
28262835
844BC8AA1CE4FA5600F5C7CB /* RxPickerViewDelegateProxy.swift */,
2836+
D9080ACD1EA05A16002B433B /* RxNavigationControllerDelegateProxy.swift */,
28272837
4613457B1D9A4AEE001ABAF2 /* RxWebViewDelegateProxy.swift */,
28282838
);
28292839
path = Proxies;
@@ -3896,6 +3906,7 @@
38963906
C89AB2061DAAC3350065FBE6 /* KVORepresentable+Swift.swift in Sources */,
38973907
C89AB1E21DAAC3350065FBE6 /* Variable+Driver.swift in Sources */,
38983908
C89AB1DE1DAAC3350065FBE6 /* ObservableConvertibleType+Driver.swift in Sources */,
3909+
D9080ACF1EA05AE0002B433B /* RxNavigationControllerDelegateProxy.swift in Sources */,
38993910
C88254271B8A752B00B02D69 /* UIBarButtonItem+Rx.swift in Sources */,
39003911
C89AB2161DAAC3350065FBE6 /* NSObject+Rx+KVORepresentable.swift in Sources */,
39013912
C882542B1B8A752B00B02D69 /* UIDatePicker+Rx.swift in Sources */,
@@ -3906,6 +3917,7 @@
39063917
C8093EE11B8A732E0088E94D /* DelegateProxy.swift in Sources */,
39073918
C89AB2501DAAC3A60065FBE6 /* _RXObjCRuntime.m in Sources */,
39083919
C89AB21E1DAAC3350065FBE6 /* NSObject+Rx.swift in Sources */,
3920+
D9080AD41EA05DE9002B433B /* UINavigationController+Rx.swift in Sources */,
39093921
88718CFE1CE5D80000D88D60 /* UITabBar+Rx.swift in Sources */,
39103922
88D98F2E1CE7549A00D50457 /* RxTabBarDelegateProxy.swift in Sources */,
39113923
C88254331B8A752B00B02D69 /* UISwitch+Rx.swift in Sources */,
@@ -4997,8 +5009,10 @@
49975009
C89AB1D91DAAC3350065FBE6 /* Driver+Subscription.swift in Sources */,
49985010
91BE429F1CBF7F3D00F6B062 /* UIPageControl+Rx.swift in Sources */,
49995011
846436E61C9AF6670035B40D /* RxSearchControllerDelegateProxy.swift in Sources */,
5012+
D9080AD11EA05DA2002B433B /* RxNavigationControllerDelegateProxy.swift in Sources */,
50005013
C89AB2111DAAC3350065FBE6 /* Logging.swift in Sources */,
50015014
C89AB1761DAAC1680065FBE6 /* ControlTarget.swift in Sources */,
5015+
D9080AD61EA05DEC002B433B /* UINavigationController+Rx.swift in Sources */,
50025016
C89AB21D1DAAC3350065FBE6 /* NSObject+Rx+RawRepresentable.swift in Sources */,
50035017
C89AB2191DAAC3350065FBE6 /* NSObject+Rx+KVORepresentable.swift in Sources */,
50045018
C89AB2531DAAC3A60065FBE6 /* _RXObjCRuntime.m in Sources */,
@@ -5051,8 +5065,10 @@
50515065
C89AB1D01DAAC3350065FBE6 /* ControlEvent+Driver.swift in Sources */,
50525066
84C225A41C33F00B008724EC /* RxTextStorageDelegateProxy.swift in Sources */,
50535067
C89AB2081DAAC3350065FBE6 /* KVORepresentable+Swift.swift in Sources */,
5068+
D9080AD01EA05AF7002B433B /* RxNavigationControllerDelegateProxy.swift in Sources */,
50545069
D203C5131BB9C53E00D02D00 /* UITextView+Rx.swift in Sources */,
50555070
D203C4F41BB9C52400D02D00 /* RxTableViewReactiveArrayDataSource.swift in Sources */,
5071+
D9080AD51EA05DEB002B433B /* UINavigationController+Rx.swift in Sources */,
50565072
7F600F401C5D0C6D00535B1D /* UIRefreshControl+Rx.swift in Sources */,
50575073
C89AB1D81DAAC3350065FBE6 /* Driver+Subscription.swift in Sources */,
50585074
C89AB1DC1DAAC3350065FBE6 /* Driver.swift in Sources */,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// RxNavigationControllerDelegateProxy.swift
3+
// Rx
4+
//
5+
// Created by Diogo on 13/04/17.
6+
// Copyright © 2017 Krunoslav Zaher. All rights reserved.
7+
//
8+
9+
#if os(iOS) || os(tvOS)
10+
11+
import UIKit
12+
#if !RX_NO_MODULE
13+
import RxSwift
14+
#endif
15+
16+
/// For more information take a look at `DelegateProxyType`.
17+
public class RxNavigationControllerDelegateProxy
18+
: DelegateProxy
19+
, UINavigationControllerDelegate
20+
, DelegateProxyType {
21+
22+
/// For more information take a look at `DelegateProxyType`.
23+
public class func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
24+
let navigationController: UINavigationController = castOrFatalError(object)
25+
return navigationController.delegate
26+
}
27+
28+
/// For more information take a look at `DelegateProxyType`.
29+
public class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) {
30+
let navigationController: UINavigationController = castOrFatalError(object)
31+
navigationController.delegate = castOptionalOrFatalError(delegate)
32+
}
33+
34+
/// For more information take a look at `DelegateProxyType`.
35+
public override class func createProxyForObject(_ object: AnyObject) -> AnyObject {
36+
let navigationController: UINavigationController = castOrFatalError(object)
37+
return navigationController.createRxDelegateProxy()
38+
}
39+
}
40+
41+
#endif
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// UINavigationController+Rx.swift
3+
// Rx
4+
//
5+
// Created by Diogo on 13/04/17.
6+
// Copyright © 2017 Krunoslav Zaher. All rights reserved.
7+
//
8+
9+
#if os(iOS) || os(tvOS)
10+
11+
#if !RX_NO_MODULE
12+
import RxSwift
13+
#endif
14+
import UIKit
15+
16+
extension UINavigationController {
17+
/// Factory method that enables subclasses to implement their own `delegate`.
18+
///
19+
/// - returns: Instance of delegate proxy that wraps `delegate`.
20+
public func createRxDelegateProxy() -> RxNavigationControllerDelegateProxy {
21+
return RxNavigationControllerDelegateProxy(parentObject: self)
22+
}
23+
}
24+
25+
extension Reactive where Base: UINavigationController {
26+
public typealias ShowEvent = (viewController: UIViewController, animated: Bool)
27+
28+
/// Reactive wrapper for `delegate`.
29+
///
30+
/// For more information take a look at `DelegateProxyType` protocol documentation.
31+
public var delegate: DelegateProxy {
32+
return RxNavigationControllerDelegateProxy.proxyForObject(base)
33+
}
34+
35+
/// Reactive wrapper for delegate method `navigationController(:willShow:animated:)`.
36+
public var willShow: ControlEvent<ShowEvent> {
37+
let source: Observable<ShowEvent> = delegate
38+
.methodInvoked(#selector(UINavigationControllerDelegate.navigationController(_:willShow:animated:)))
39+
.map { arg in
40+
let viewController = try castOrThrow(UIViewController.self, arg[1])
41+
let animated = try castOrThrow(Bool.self, arg[2])
42+
return (viewController, animated)
43+
}
44+
return ControlEvent(events: source)
45+
}
46+
47+
/// Reactive wrapper for delegate method `navigationController(:didShow:animated:)`.
48+
public var didShow: ControlEvent<ShowEvent> {
49+
let source: Observable<ShowEvent> = delegate
50+
.methodInvoked(#selector(UINavigationControllerDelegate.navigationController(_:didShow:animated:)))
51+
.map { arg in
52+
let viewController = try castOrThrow(UIViewController.self, arg[1])
53+
let animated = try castOrThrow(Bool.self, arg[2])
54+
return (viewController, animated)
55+
}
56+
return ControlEvent(events: source)
57+
}
58+
}
59+
60+
#endif

0 commit comments

Comments
 (0)