Skip to content

Commit a830794

Browse files
author
Wes Smith
committed
move navigation controller routing to a category
1 parent 970c7cc commit a830794

File tree

8 files changed

+104
-34
lines changed

8 files changed

+104
-34
lines changed

DeepLinkKit.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
DEAD328B19E079D3003C8D65 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = DEAD327B19E079D3003C8D65 /* InfoPlist.strings */; };
5252
DEAD328C19E079D3003C8D65 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DEAD327D19E079D3003C8D65 /* Images.xcassets */; };
5353
DEAD328D19E079D3003C8D65 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = DEAD327E19E079D3003C8D65 /* main.m */; };
54+
DEB497F71CB441FA005FC580 /* UINavigationController_DPLRoutingSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = DEB497F61CB441FA005FC580 /* UINavigationController_DPLRoutingSpec.m */; };
5455
DECB32531A881CA10071C76E /* NSObject_DPLJSONObjectSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = DECB32521A881CA10071C76E /* NSObject_DPLJSONObjectSpec.m */; };
5556
DECB32551A882D1A0071C76E /* DPLMutableDeepLinkSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = DECB32541A882D1A0071C76E /* DPLMutableDeepLinkSpec.m */; };
5657
DECB32571A8866700071C76E /* DPLMutableDeepLink_AppLinksSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = DECB32561A8866700071C76E /* DPLMutableDeepLink_AppLinksSpec.m */; };
@@ -125,6 +126,8 @@
125126
DE16E9341A4289D500077E18 /* DPLRouteMatcher.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DPLRouteMatcher.h; sourceTree = "<group>"; };
126127
DE16E9351A4289D500077E18 /* DPLRouteMatcher.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DPLRouteMatcher.m; sourceTree = "<group>"; };
127128
DE16E9371A4289DF00077E18 /* DPLRouteMatcherSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DPLRouteMatcherSpec.m; sourceTree = "<group>"; };
129+
DE1B5F581CB301300086227F /* UINavigationController+DPLRouting.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UINavigationController+DPLRouting.h"; sourceTree = "<group>"; };
130+
DE1B5F591CB301300086227F /* UINavigationController+DPLRouting.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UINavigationController+DPLRouting.m"; sourceTree = "<group>"; };
128131
DE3E61081A3B4485008D6DFC /* NSString+DPLJSON.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSString+DPLJSON.h"; sourceTree = "<group>"; };
129132
DE3E61091A3B4485008D6DFC /* NSString+DPLJSON.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSString+DPLJSON.m"; sourceTree = "<group>"; };
130133
DE3E610D1A3B4492008D6DFC /* NSString_DPLJSONSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSString_DPLJSONSpec.m; sourceTree = "<group>"; };
@@ -156,6 +159,7 @@
156159
DEAD328319E079D3003C8D65 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
157160
DEAD328419E079D3003C8D65 /* Tests-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Tests-Info.plist"; sourceTree = "<group>"; };
158161
DEAD328519E079D3003C8D65 /* Tests-Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Tests-Prefix.pch"; sourceTree = "<group>"; };
162+
DEB497F61CB441FA005FC580 /* UINavigationController_DPLRoutingSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UINavigationController_DPLRoutingSpec.m; sourceTree = "<group>"; };
159163
DEB4EDBC1A4A036D00F31D14 /* DPLDeepLink_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DPLDeepLink_Private.h; sourceTree = "<group>"; };
160164
DEB4EDBD1A4A726200F31D14 /* DeepLinkKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DeepLinkKit.h; sourceTree = "<group>"; };
161165
DEC308D71CAD6A05006828A5 /* module.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; name = module.modulemap; path = DeepLinkKit/module.modulemap; sourceTree = "<group>"; };
@@ -471,6 +475,8 @@
471475
DE3E61091A3B4485008D6DFC /* NSString+DPLJSON.m */,
472476
DECB32501A87E94B0071C76E /* NSObject+DPLJSONObject.h */,
473477
DECB32511A87E94B0071C76E /* NSObject+DPLJSONObject.m */,
478+
DE1B5F581CB301300086227F /* UINavigationController+DPLRouting.h */,
479+
DE1B5F591CB301300086227F /* UINavigationController+DPLRouting.m */,
474480
);
475481
path = Categories;
476482
sourceTree = "<group>";
@@ -482,6 +488,7 @@
482488
DE3E610D1A3B4492008D6DFC /* NSString_DPLJSONSpec.m */,
483489
DE16E91D1A42733100077E18 /* NSString_DPLTrimSpec.m */,
484490
DECB32521A881CA10071C76E /* NSObject_DPLJSONObjectSpec.m */,
491+
DEB497F61CB441FA005FC580 /* UINavigationController_DPLRoutingSpec.m */,
485492
);
486493
path = Categories;
487494
sourceTree = "<group>";
@@ -1055,6 +1062,7 @@
10551062
DE058E101A3B485500147C04 /* DPLDeepLinkSpec.m in Sources */,
10561063
DECB32551A882D1A0071C76E /* DPLMutableDeepLinkSpec.m in Sources */,
10571064
DE058E0A1A3B46FD00147C04 /* NSString_DPLQuerySpec.m in Sources */,
1065+
DEB497F71CB441FA005FC580 /* UINavigationController_DPLRoutingSpec.m in Sources */,
10581066
DECB32571A8866700071C76E /* DPLMutableDeepLink_AppLinksSpec.m in Sources */,
10591067
2F4988DE1AE71ABC0069EF2B /* DPLRouteHandlerIntegrationTest.m in Sources */,
10601068
DEA55ADF1A5F2BBC00C312F5 /* DPLDeepLink_AppLinksSpec.m in Sources */,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@import UIKit;
2+
3+
@interface UINavigationController (DPLRouting)
4+
5+
/**
6+
Places a view controller in the receivers view controller stack.
7+
@param targetViewController The view controller to be placed in/on to the stack.
8+
@note The `targetViewController' is pushed or inserted into the stack replacing any pre-existing instances of the same class. If the `targetViewController' instance is already in the stack, it will become the `topViewController'.
9+
*/
10+
- (void)DPL_placeTargetViewController:(UIViewController *)targetViewController;
11+
12+
@end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#import "UINavigationController+DPLRouting.h"
2+
3+
@implementation UINavigationController (DPLRouting)
4+
5+
- (void)DPL_placeTargetViewController:(UIViewController *)targetViewController {
6+
7+
if ([self.viewControllers containsObject:targetViewController]) {
8+
[self popToViewController:targetViewController animated:NO];
9+
}
10+
else {
11+
12+
for (UIViewController *controller in self.viewControllers) {
13+
if ([controller isMemberOfClass:[targetViewController class]]) {
14+
15+
[self popToViewController:controller animated:NO];
16+
[self popViewControllerAnimated:NO];
17+
18+
if ([controller isEqual:self.topViewController]) {
19+
[self setViewControllers:@[targetViewController] animated:NO];
20+
}
21+
22+
break;
23+
}
24+
}
25+
26+
if (![self.topViewController isEqual:targetViewController]) {
27+
[self pushViewController:targetViewController animated:NO];
28+
}
29+
}
30+
}
31+
32+
@end

DeepLinkKit/DeepLinkKit_Private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
#import "NSString+DPLQuery.h"
88
#import "NSString+DPLJSON.h"
99
#import "NSObject+DPLJSONObject.h"
10+
#import "UINavigationController+DPLRouting.h"

DeepLinkKit/RouteHandler/DPLRouteHandler.m

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#import "DPLRouteHandler.h"
2+
#import "UINavigationController+DPLRouting.h"
23

34
@implementation DPLRouteHandler
45

@@ -32,39 +33,7 @@ - (void)presentTargetViewController:(UIViewController <DPLTargetViewController>
3233
}
3334
else if ([presentingViewController isKindOfClass:[UINavigationController class]]) {
3435

35-
[self placeTargetViewController:targetViewController
36-
inNavigationController:(UINavigationController *)presentingViewController];
37-
}
38-
}
39-
40-
41-
#pragma mark - Private
42-
43-
- (void)placeTargetViewController:(UIViewController *)targetViewController
44-
inNavigationController:(UINavigationController *)navigationController {
45-
46-
if ([navigationController.viewControllers containsObject:targetViewController]) {
47-
[navigationController popToViewController:targetViewController animated:NO];
48-
}
49-
else {
50-
51-
for (UIViewController *controller in navigationController.viewControllers) {
52-
if ([controller isMemberOfClass:[targetViewController class]]) {
53-
54-
[navigationController popToViewController:controller animated:NO];
55-
[navigationController popViewControllerAnimated:NO];
56-
57-
if ([controller isEqual:navigationController.topViewController]) {
58-
[navigationController setViewControllers:@[targetViewController] animated:NO];
59-
}
60-
61-
break;
62-
}
63-
}
64-
65-
if (![navigationController.topViewController isEqual:targetViewController]) {
66-
[navigationController pushViewController:targetViewController animated:NO];
67-
}
36+
[((UINavigationController *)presentingViewController) DPL_placeTargetViewController:targetViewController];
6837
}
6938
}
7039

DeepLinkKit/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ framework module DeepLinkKit {
1616
exclude header "NSString+DPLQuery.h"
1717
exclude header "NSString+DPLJSON.h"
1818
exclude header "NSObject+DPLJSONObject.h"
19+
exclude header "UINavigationController+DPLRouting.h"
1920
}
2021

2122
explicit module DeepLinkKit.Private {

Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ EXTERNAL SOURCES:
1919
:path: .
2020

2121
SPEC CHECKSUMS:
22-
DeepLinkKit: 5349fa5cc37cadeb32e0fa239c28536bc0557cf2
22+
DeepLinkKit: c5c1b216b8aa00fa7735ffeee6a354e39e7574ac
2323
Expecta: e1c022fcd33910b6be89c291d2775b3fe27a89fe
2424
KIF: 2275c6d59c77e5e56f660f006b99d73780130540
2525
OCMock: 18c9b7e67d4c2770e95bb77a9cc1ae0c91fe3835
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#import "Specta.h"
2+
#import "UINavigationController+DPLRouting.h"
3+
4+
@interface VC1 : UIViewController @end
5+
@implementation VC1 @end
6+
7+
@interface VC2 : UIViewController @end
8+
@implementation VC2 @end
9+
10+
@interface VC3 : UIViewController @end
11+
@implementation VC3 @end
12+
13+
14+
SpecBegin(UINavigationController_DPLRouting)
15+
16+
describe(@"DPL_placeTargetViewController:", ^{
17+
18+
VC1 *vc1 = [VC1 new];
19+
VC2 *vc2 = [VC2 new];
20+
VC3 *vc3 = [VC3 new];
21+
22+
__block UINavigationController *nc;
23+
beforeEach(^{
24+
nc = [[UINavigationController alloc] initWithRootViewController:[UIViewController new]];
25+
});
26+
27+
it(@"pushes a view controller on the stack if not already present", ^{
28+
[nc DPL_placeTargetViewController:vc1];
29+
expect(nc.topViewController).to.equal(vc1);
30+
});
31+
32+
it(@"pops to the view controller instance on the stack if already present", ^{
33+
[nc setViewControllers:@[vc1, vc2, vc3] animated:NO];
34+
[nc DPL_placeTargetViewController:vc2];
35+
expect(nc.topViewController).to.equal(vc2);
36+
});
37+
38+
it(@"replaces existing instances of same view controller class already present on the stack", ^{
39+
[nc setViewControllers:@[vc1, vc2, vc3] animated:NO];
40+
VC2 *newvc2instance = [VC2 new];
41+
[nc DPL_placeTargetViewController:newvc2instance];
42+
expect(nc.topViewController).to.equal(newvc2instance);
43+
expect(nc.viewControllers).toNot.contain(vc2);
44+
});
45+
});
46+
47+
SpecEnd

0 commit comments

Comments
 (0)