Browse Source

ios: popTo

Ran Greenberg 7 years ago
parent
commit
569592cb3c

+ 4
- 0
ios/RNNBridgeModule.m View File

30
 	[_commandsHandler pop:containerId];
30
 	[_commandsHandler pop:containerId];
31
 }
31
 }
32
 
32
 
33
+RCT_EXPORT_METHOD(popTo:(NSString*)containerId toContainerId:(NSString*)toContainerId) {
34
+	[_commandsHandler popTo:containerId toContainerId:toContainerId];
35
+}
36
+
33
 RCT_EXPORT_METHOD(showModal:(NSDictionary*)layout) {
37
 RCT_EXPORT_METHOD(showModal:(NSDictionary*)layout) {
34
 	[_commandsHandler showModal:layout];
38
 	[_commandsHandler showModal:layout];
35
 }
39
 }

+ 2
- 0
ios/RNNCommandsHandler.h View File

14
 
14
 
15
 -(void) pop:(NSString*)containerId;
15
 -(void) pop:(NSString*)containerId;
16
 
16
 
17
+-(void) popTo:(NSString*)containerId toContainerId:(NSString*)toContainerId;
18
+
17
 -(void) showModal:(NSDictionary*)layout;
19
 -(void) showModal:(NSDictionary*)layout;
18
 
20
 
19
 -(void) dismissModal:(NSString*)containerId;
21
 -(void) dismissModal:(NSString*)containerId;

+ 5
- 0
ios/RNNCommandsHandler.m View File

44
 	[_navigationStackManager pop:containerId];
44
 	[_navigationStackManager pop:containerId];
45
 }
45
 }
46
 
46
 
47
+-(void) popTo:(NSString*)containerId toContainerId:(NSString*)toContainerId {
48
+	[self assertReady];
49
+	[_navigationStackManager popTo:containerId toContainerId:toContainerId];
50
+}
51
+
47
 -(void) showModal:(NSDictionary*)layout {
52
 -(void) showModal:(NSDictionary*)layout {
48
 	[self assertReady];
53
 	[self assertReady];
49
 	UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];
54
 	UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];

+ 1
- 0
ios/RNNNavigationStackManager.h View File

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

+ 15
- 3
ios/RNNNavigationStackManager.m View File

12
 
12
 
13
 -(void)push:(UIViewController *)newTop onTop:(NSString *)containerId {
13
 -(void)push:(UIViewController *)newTop onTop:(NSString *)containerId {
14
 	UIViewController *vc = [_store findContainerForId:containerId];
14
 	UIViewController *vc = [_store findContainerForId:containerId];
15
-	[[vc navigationController] pushViewController:newTop animated:true];
15
+	[[vc navigationController] pushViewController:newTop animated:YES];
16
 }
16
 }
17
 
17
 
18
 -(void)pop:(NSString *)containerId {
18
 -(void)pop:(NSString *)containerId {
19
 	UIViewController* vc = [_store findContainerForId:containerId];
19
 	UIViewController* vc = [_store findContainerForId:containerId];
20
 	UINavigationController* nvc = [vc navigationController];
20
 	UINavigationController* nvc = [vc navigationController];
21
 	if ([nvc topViewController] == vc) {
21
 	if ([nvc topViewController] == vc) {
22
-		[nvc popViewControllerAnimated:true];
22
+		[nvc popViewControllerAnimated:YES];
23
 	} else {
23
 	} else {
24
 		NSMutableArray * vcs = nvc.viewControllers.mutableCopy;
24
 		NSMutableArray * vcs = nvc.viewControllers.mutableCopy;
25
 		[vcs removeObject:vc];
25
 		[vcs removeObject:vc];
26
-		[nvc setViewControllers:vcs animated:true];
26
+		[nvc setViewControllers:vcs animated:YES];
27
 	}
27
 	}
28
 	[_store removeContainer:containerId];
28
 	[_store removeContainer:containerId];
29
 }
29
 }
30
 
30
 
31
+-(void) popTo:(NSString*)containerId toContainerId:(NSString*)toContainerId {
32
+	UIViewController *vc = [_store findContainerForId:containerId];
33
+	UINavigationController *nvc = [vc navigationController];
34
+	
35
+	UIViewController *toVC = [_store findContainerForId:toContainerId];
36
+	
37
+	if (vc && toVC) {
38
+		[nvc popToViewController:toVC animated:YES];
39
+		// TODO - remove the poped vcs from store
40
+	}
41
+}
42
+
31
 @end
43
 @end

+ 9
- 0
playground/e2e/app.test.js View File

63
     await elementByLabel('Pop').tap();
63
     await elementByLabel('Pop').tap();
64
     await expect(elementByLabel('React Native Navigation!')).toBeVisible();
64
     await expect(elementByLabel('React Native Navigation!')).toBeVisible();
65
   });
65
   });
66
+
67
+  it('pop to specific id', async () => {
68
+    await elementByLabel('Push').tap();
69
+    await elementByLabel('Push').tap();
70
+    await elementByLabel('Push').tap();
71
+    await expect(elementByLabel('Stack Position: 3')).toBeVisible();
72
+    await elementByLabel('Pop To Stack Position 1').tap();
73
+    await expect(elementByLabel('Stack Position: 1')).toBeVisible();
74
+  })
66
 });
75
 });
67
 
76
 
68
 describe('modal', () => {
77
 describe('modal', () => {

+ 8
- 1
playground/src/containers/PushedScreen.js View File

15
     this.onClickPush = this.onClickPush.bind(this);
15
     this.onClickPush = this.onClickPush.bind(this);
16
     this.onClickPop = this.onClickPop.bind(this);
16
     this.onClickPop = this.onClickPop.bind(this);
17
     this.onClickPopPrevious = this.onClickPopPrevious.bind(this);
17
     this.onClickPopPrevious = this.onClickPopPrevious.bind(this);
18
+    this.onClickPopToFirstPosition = this.onClickPopToFirstPosition.bind(this);
18
   }
19
   }
19
   render() {
20
   render() {
21
+    const stackPosition = this.getStackPosition();
20
     return (
22
     return (
21
       <View style={styles.root}>
23
       <View style={styles.root}>
22
         <Text style={styles.h1}>{`Pushed Screen`}</Text>
24
         <Text style={styles.h1}>{`Pushed Screen`}</Text>
23
-        <Text style={styles.h2}>{`Stack Position: ${this.getStackPosition()}`}</Text>
25
+        <Text style={styles.h2}>{`Stack Position: ${stackPosition}`}</Text>
24
         <Button title="Push" onPress={this.onClickPush} />
26
         <Button title="Push" onPress={this.onClickPush} />
25
         <Button title="Pop" onPress={this.onClickPop} />
27
         <Button title="Pop" onPress={this.onClickPop} />
26
         <Button title="Pop Previous" onPress={this.onClickPopPrevious} />
28
         <Button title="Pop Previous" onPress={this.onClickPopPrevious} />
29
+        {stackPosition === 3 && <Button title="Pop To Stack Position 1" onPress={this.onClickPopToFirstPosition} />}
27
         <Text style={styles.footer}>{`this.props.id = ${this.props.id}`}</Text>
30
         <Text style={styles.footer}>{`this.props.id = ${this.props.id}`}</Text>
28
       </View>
31
       </View>
29
     );
32
     );
47
     Navigation.on(_.last(this.props.previousScreenIds)).pop();
50
     Navigation.on(_.last(this.props.previousScreenIds)).pop();
48
   }
51
   }
49
 
52
 
53
+  onClickPopToFirstPosition() {
54
+    Navigation.on(this.props.id).popTo(this.props.previousScreenIds[0]);
55
+  }
56
+
50
   getStackPosition() {
57
   getStackPosition() {
51
     return this.props.stackPosition || 1;
58
     return this.props.stackPosition || 1;
52
   }
59
   }