Browse Source

add pop to with store handling

Ran Greenberg 7 years ago
parent
commit
b74ce455b6

+ 2
- 2
lib/ios/RNNBridgeModule.m View File

@@ -30,8 +30,8 @@ RCT_EXPORT_METHOD(pop:(NSString*)containerId) {
30 30
 	[_commandsHandler pop:containerId];
31 31
 }
32 32
 
33
-RCT_EXPORT_METHOD(popTo:(NSString*)containerId toContainerId:(NSString*)toContainerId) {
34
-	[_commandsHandler popTo:containerId toContainerId:toContainerId];
33
+RCT_EXPORT_METHOD(popTo:(NSString*)fromContainerId toContainerId:(NSString*)toContainerId) {
34
+	[_commandsHandler popTo:toContainerId fromContainerId:fromContainerId];
35 35
 }
36 36
 
37 37
 RCT_EXPORT_METHOD(popToRoot:(NSString*)containerId) {

+ 1
- 1
lib/ios/RNNCommandsHandler.h View File

@@ -14,7 +14,7 @@
14 14
 
15 15
 -(void) pop:(NSString*)containerId;
16 16
 
17
--(void) popTo:(NSString*)containerId toContainerId:(NSString*)toContainerId;
17
+-(void) popTo:(NSString*)containerId fromContainerId:(NSString*)fromContainerId;
18 18
 
19 19
 -(void) popToRoot:(NSString*)containerId;
20 20
 

+ 2
- 2
lib/ios/RNNCommandsHandler.m View File

@@ -44,9 +44,9 @@
44 44
 	[_navigationStackManager pop:containerId];
45 45
 }
46 46
 
47
--(void) popTo:(NSString*)containerId toContainerId:(NSString*)toContainerId {
47
+-(void) popTo:(NSString*)containerId fromContainerId:(NSString*)fromContainerId; {
48 48
 	[self assertReady];
49
-	[_navigationStackManager popTo:containerId toContainerId:toContainerId];
49
+	[_navigationStackManager popTo:containerId fromContainerId:fromContainerId];
50 50
 }
51 51
 
52 52
 -(void) popToRoot:(NSString*)containerId {

+ 2
- 2
lib/ios/RNNNavigationStackManager.h View File

@@ -8,7 +8,7 @@
8 8
 
9 9
 -(void)push:(UIViewController*)newTop onTop:(NSString*)containerId;
10 10
 -(void)pop:(NSString*)containerId;
11
--(void)popTo:(NSString*)containerId toContainerId:(NSString*)toContainerId;
12
--(void) popToRoot:(NSString*)containerId;
11
+-(void)popTo:(NSString*)toContainerId fromContainerId:(NSString*)fromContainerId;
12
+-(void)popToRoot:(NSString*)containerId;
13 13
 
14 14
 @end

+ 6
- 4
lib/ios/RNNNavigationStackManager.m View File

@@ -28,15 +28,17 @@
28 28
 	[_store removeContainer:containerId];
29 29
 }
30 30
 
31
--(void) popTo:(NSString*)containerId toContainerId:(NSString*)toContainerId {
32
-	UIViewController *vc = [_store findContainerForId:containerId];
31
+-(void)popTo:(NSString*)toContainerId fromContainerId:(NSString*)fromContainerId {
32
+	UIViewController *vc = [_store findContainerForId:fromContainerId];
33 33
 	UINavigationController *nvc = [vc navigationController];
34 34
 	
35 35
 	UIViewController *toVC = [_store findContainerForId:toContainerId];
36 36
 	
37 37
 	if (vc && toVC) {
38
-		[nvc popToViewController:toVC animated:YES];
39
-		// TODO - remove the poped vcs from store
38
+		NSArray *popedVCs = [nvc popToViewController:toVC animated:YES];
39
+		for (UIViewController *popedVC in popedVCs) {
40
+			[_store removeContainerByViewControllerInstance:popedVC];
41
+		}
40 42
 	}
41 43
 }
42 44
 

+ 1
- 0
lib/ios/RNNStore.h View File

@@ -7,6 +7,7 @@
7 7
 -(UIViewController*) findContainerForId:(NSString*)containerId;
8 8
 -(void) setContainer:(UIViewController*)viewController containerId:(NSString*)containerId;
9 9
 -(void) removeContainer:(NSString*)containerId;
10
+-(void) removeContainerByViewControllerInstance:(UIViewController*)containerInstance;
10 11
 
11 12
 -(void) setReadyToReceiveCommands:(BOOL)isReady;
12 13
 -(BOOL) isReadyToReceiveCommands;

+ 17
- 0
lib/ios/RNNStore.m View File

@@ -36,6 +36,13 @@
36 36
 	[_containerStore removeObjectForKey:containerId];
37 37
 }
38 38
 
39
+- (void)removeContainerByViewControllerInstance:(UIViewController*)containerInstance {
40
+	NSString *foundKey = [self containerKeyForInstance:containerInstance];
41
+	if (foundKey) {
42
+		[self removeContainer:foundKey];
43
+	}
44
+}
45
+
39 46
 -(void)setReadyToReceiveCommands:(BOOL)isReady {
40 47
 	_isReadyToReceiveCommands = isReady;
41 48
 }
@@ -54,4 +61,14 @@
54 61
 	[_containerStore removeAllObjects];
55 62
 }
56 63
 
64
+-(NSString*)containerKeyForInstance:(UIViewController*)instance {
65
+	for (NSString *key in _containerStore) {
66
+		UIViewController *value = [_containerStore objectForKey:key];
67
+		if (value == instance) {
68
+			return key;
69
+		}
70
+	}
71
+	return nil;
72
+}
73
+
57 74
 @end

+ 4
- 0
playground/ios/playground.xcodeproj/project.pbxproj View File

@@ -13,6 +13,7 @@
13 13
 		13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
14 14
 		26070FD01E4B8B9D003EC8B9 /* RNNControllerFactoryTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 26070FCF1E4B8B9D003EC8B9 /* RNNControllerFactoryTest.m */; };
15 15
 		261F0E671E6EE9EC00989DE2 /* RNNModalManagerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 261F0E661E6EE9EC00989DE2 /* RNNModalManagerTest.m */; };
16
+		266DDDF71E8BCE9900C6F13C /* RNNNavigationStackManagerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 266DDDF61E8BCE9900C6F13C /* RNNNavigationStackManagerTest.m */; };
16 17
 		268692851E50572700E2C612 /* RNNStoreTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 268692841E50572700E2C612 /* RNNStoreTest.m */; };
17 18
 		26DB71671E82C65600A69712 /* RNNCommandsHandlerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 26DB71661E82C65600A69712 /* RNNCommandsHandlerTest.m */; };
18 19
 		7B8F30491E84151300110AEC /* libcxxreact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7B8F2FDE1E840F3600110AEC /* libcxxreact.a */; };
@@ -236,6 +237,7 @@
236 237
 		13B07FB71A68108700A75B9A /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
237 238
 		26070FCF1E4B8B9D003EC8B9 /* RNNControllerFactoryTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNNControllerFactoryTest.m; sourceTree = "<group>"; };
238 239
 		261F0E661E6EE9EC00989DE2 /* RNNModalManagerTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNNModalManagerTest.m; sourceTree = "<group>"; };
240
+		266DDDF61E8BCE9900C6F13C /* RNNNavigationStackManagerTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNNNavigationStackManagerTest.m; sourceTree = "<group>"; };
239 241
 		268692841E50572700E2C612 /* RNNStoreTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNNStoreTest.m; sourceTree = "<group>"; };
240 242
 		26DB71661E82C65600A69712 /* RNNCommandsHandlerTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNNCommandsHandlerTest.m; sourceTree = "<group>"; };
241 243
 		7B8F2FC41E840F1A00110AEC /* ReactNativeNavigation.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = ReactNativeNavigation.xcodeproj; path = ../../lib/ios/ReactNativeNavigation.xcodeproj; sourceTree = "<group>"; };
@@ -292,6 +294,7 @@
292 294
 				268692841E50572700E2C612 /* RNNStoreTest.m */,
293 295
 				261F0E661E6EE9EC00989DE2 /* RNNModalManagerTest.m */,
294 296
 				26DB71661E82C65600A69712 /* RNNCommandsHandlerTest.m */,
297
+				266DDDF61E8BCE9900C6F13C /* RNNNavigationStackManagerTest.m */,
295 298
 				00E356F01AD99517003FC87E /* Supporting Files */,
296 299
 			);
297 300
 			path = playgroundTests;
@@ -823,6 +826,7 @@
823 826
 			buildActionMask = 2147483647;
824 827
 			files = (
825 828
 				268692851E50572700E2C612 /* RNNStoreTest.m in Sources */,
829
+				266DDDF71E8BCE9900C6F13C /* RNNNavigationStackManagerTest.m in Sources */,
826 830
 				26DB71671E82C65600A69712 /* RNNCommandsHandlerTest.m in Sources */,
827 831
 				261F0E671E6EE9EC00989DE2 /* RNNModalManagerTest.m in Sources */,
828 832
 				26070FD01E4B8B9D003EC8B9 /* RNNControllerFactoryTest.m in Sources */,

+ 103
- 0
playground/ios/playgroundTests/RNNNavigationStackManagerTest.m View File

@@ -0,0 +1,103 @@
1
+#import <XCTest/XCTest.h>
2
+//#import <objc/runtime.h>
3
+#import "RNNStore.h"
4
+#import "RNNNavigationStackManager.h"
5
+
6
+@interface MockUINavigationController : UINavigationController
7
+@property (nonatomic, strong) NSArray* willReturnVCs;
8
+@end
9
+
10
+@implementation MockUINavigationController
11
+
12
+-(NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
13
+	return self.willReturnVCs;
14
+}
15
+
16
+@end
17
+
18
+
19
+@interface RNNNavigationStackManagerTest : XCTestCase
20
+
21
+@end
22
+
23
+@implementation RNNNavigationStackManagerTest
24
+
25
+- (void)setUp {
26
+    [super setUp];
27
+	
28
+}
29
+
30
+
31
+- (void)testPop {
32
+	RNNStore *store = [RNNStore new];
33
+	RNNNavigationStackManager *uut = [[RNNNavigationStackManager alloc] initWithStore:store];
34
+	
35
+	UINavigationController *nvc = [[UINavigationController alloc] init];
36
+	
37
+	UIViewController *vc1 = [UIViewController new];
38
+	UIViewController *vc2 = [UIViewController new];
39
+
40
+	
41
+	NSArray *vcArray = @[vc1, vc2];
42
+	[nvc setViewControllers:vcArray];
43
+	[store setContainer:vc1 containerId:@"vc1"];
44
+	[store setContainer:vc2 containerId:@"vc2"];
45
+	[uut pop:@"vc2"];
46
+	
47
+	XCTAssertNil([store findContainerForId:@"vc2"]);
48
+	XCTAssertNotNil([store findContainerForId:@"vc1"]);
49
+	
50
+}
51
+
52
+- (void)testPopTo_singleVc {
53
+	RNNStore *store = [RNNStore new];
54
+	RNNNavigationStackManager *uut = [[RNNNavigationStackManager alloc] initWithStore:store];
55
+	
56
+	UIViewController *vc1 = [UIViewController new];
57
+	UIViewController *vc2 = [UIViewController new];
58
+	UIViewController *vc3 = [UIViewController new];
59
+	MockUINavigationController *nvc = [[MockUINavigationController alloc] initWithRootViewController:vc1];
60
+	[nvc pushViewController:vc2 animated:NO];
61
+	[nvc pushViewController:vc3 animated:NO];
62
+	nvc.willReturnVCs = @[vc2, vc3];
63
+	
64
+	[store setContainer:vc1 containerId:@"vc1"];
65
+	[store setContainer:vc2 containerId:@"vc2"];
66
+	[store setContainer:vc3 containerId:@"vc3"];
67
+	[uut popTo:@"vc1" fromContainerId:@"vc3"];
68
+	
69
+	XCTAssertNil([store findContainerForId:@"vc2"]);
70
+	XCTAssertNotNil([store findContainerForId:@"vc1"]);
71
+	
72
+}
73
+
74
+
75
+//- (void)testPopTo {
76
+//	RNNStore *store = [RNNStore new];
77
+//	RNNNavigationStackManager *uut = [[RNNNavigationStackManager alloc] initWithStore:store];
78
+//	
79
+//	UINavigationController *nvc = [[UINavigationController alloc] init];
80
+//	
81
+//	UIViewController *vc1 = [UIViewController new];
82
+//	UIViewController *vc2 = [UIViewController new];
83
+//	UIViewController *vc3 = [UIViewController new];
84
+//	UIViewController *vc4 = [UIViewController new];
85
+//	
86
+//	NSArray *vcArray = @[vc1, vc2, vc3, vc4];
87
+//	[nvc setViewControllers:vcArray];
88
+//	[store setContainer:vc1 containerId:@"vc1"];
89
+//	[store setContainer:vc2 containerId:@"vc2"];
90
+//	[store setContainer:vc3 containerId:@"vc3"];
91
+//	[store setContainer:vc4 containerId:@"vc4"];
92
+//	[store setContainer:nvc containerId:@"nvc"];
93
+//	
94
+//	[uut popTo:@"vc1" fromContainerId:@"vc4"];
95
+//	
96
+//	XCTAssertNil([store findContainerForId:@"vc4"]);
97
+//	XCTAssertNil([store findContainerForId:@"vc3"]);
98
+//	XCTAssertNil([store findContainerForId:@"vc2"]);
99
+//	
100
+//}
101
+
102
+
103
+@end

+ 17
- 0
playground/ios/playgroundTests/RNNStoreTest.m View File

@@ -76,6 +76,21 @@
76 76
 	XCTAssertNil([self.store findContainerForId:vcId]);
77 77
 }
78 78
 
79
+
80
+-(void)testRemoveContainerByInstance {
81
+	NSString *containerId1 = @"cntId1";
82
+	UIViewController *vc1 = [UIViewController new];
83
+	
84
+	[self.store setContainer:vc1 containerId:containerId1];
85
+	[self.store removeContainerByViewControllerInstance:vc1];
86
+	
87
+	XCTAssertNil([self.store findContainerForId:@"cntId1"]);
88
+}
89
+
90
+
91
+#pragma mark - private
92
+
93
+
79 94
 -(void)setContainerAndRelease:(NSString*)vcId {
80 95
 	@autoreleasepool {
81 96
 		UIViewController *vc2 = [UIViewController new];
@@ -85,4 +100,6 @@
85 100
 	}
86 101
 }
87 102
 
103
+
104
+
88 105
 @end