Browse Source

ios - popToRoot fixed #891

Ran Greenberg 7 years ago
parent
commit
3b03bccf03

+ 4
- 0
ios/RNNBridgeModule.m View File

@@ -34,6 +34,10 @@ RCT_EXPORT_METHOD(popTo:(NSString*)containerId toContainerId:(NSString*)toContai
34 34
 	[_commandsHandler popTo:containerId toContainerId:toContainerId];
35 35
 }
36 36
 
37
+RCT_EXPORT_METHOD(popToRoot:(NSString*)containerId) {
38
+	[_commandsHandler popToRoot:containerId];
39
+}
40
+
37 41
 RCT_EXPORT_METHOD(showModal:(NSDictionary*)layout) {
38 42
 	[_commandsHandler showModal:layout];
39 43
 }

+ 2
- 0
ios/RNNCommandsHandler.h View File

@@ -16,6 +16,8 @@
16 16
 
17 17
 -(void) popTo:(NSString*)containerId toContainerId:(NSString*)toContainerId;
18 18
 
19
+-(void) popToRoot:(NSString*)containerId;
20
+
19 21
 -(void) showModal:(NSDictionary*)layout;
20 22
 
21 23
 -(void) dismissModal:(NSString*)containerId;

+ 5
- 0
ios/RNNCommandsHandler.m View File

@@ -49,6 +49,11 @@
49 49
 	[_navigationStackManager popTo:containerId toContainerId:toContainerId];
50 50
 }
51 51
 
52
+-(void) popToRoot:(NSString*)containerId {
53
+	[self assertReady];
54
+	[_navigationStackManager popToRoot:containerId];
55
+}
56
+
52 57
 -(void) showModal:(NSDictionary*)layout {
53 58
 	[self assertReady];
54 59
 	UIViewController *newVc = [_controllerFactory createLayoutAndSaveToStore:layout];

+ 1
- 0
ios/RNNNavigationStackManager.h View File

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

+ 6
- 0
ios/RNNNavigationStackManager.m View File

@@ -40,4 +40,10 @@
40 40
 	}
41 41
 }
42 42
 
43
+-(void) popToRoot:(NSString*)containerId {
44
+	UIViewController* vc = [_store findContainerForId:containerId];
45
+	UINavigationController* nvc = [vc navigationController];
46
+	[nvc popToRootViewControllerAnimated:YES];
47
+}
48
+
43 49
 @end

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

@@ -72,6 +72,14 @@ describe('screen stack', () => {
72 72
     await elementByLabel('Pop To Stack Position 1').tap();
73 73
     await expect(elementByLabel('Stack Position: 1')).toBeVisible();
74 74
   });
75
+
76
+  it('pop to root', async () => {
77
+    await elementByLabel('Push').tap();
78
+    await elementByLabel('Push').tap();
79
+    await elementByLabel('Push').tap();
80
+    await elementByLabel('Pop To Root').tap();
81
+    await expect(elementByLabel('React Native Navigation!')).toBeVisible();
82
+  });
75 83
 });
76 84
 
77 85
 describe('modal', () => {

+ 6
- 0
playground/src/containers/PushedScreen.js View File

@@ -16,6 +16,7 @@ class PushedScreen extends Component {
16 16
     this.onClickPop = this.onClickPop.bind(this);
17 17
     this.onClickPopPrevious = this.onClickPopPrevious.bind(this);
18 18
     this.onClickPopToFirstPosition = this.onClickPopToFirstPosition.bind(this);
19
+    this.onClickPopToRoot = this.onClickPopToRoot.bind(this);
19 20
   }
20 21
   render() {
21 22
     const stackPosition = this.getStackPosition();
@@ -26,6 +27,7 @@ class PushedScreen extends Component {
26 27
         <Button title="Push" onPress={this.onClickPush} />
27 28
         <Button title="Pop" onPress={this.onClickPop} />
28 29
         <Button title="Pop Previous" onPress={this.onClickPopPrevious} />
30
+        <Button title="Pop To Root" onPress={this.onClickPopToRoot} />
29 31
         {stackPosition > 2 && <Button title="Pop To Stack Position 1" onPress={this.onClickPopToFirstPosition} />}
30 32
         <Text style={styles.footer}>{`this.props.id = ${this.props.id}`}</Text>
31 33
       </View>
@@ -54,6 +56,10 @@ class PushedScreen extends Component {
54 56
     Navigation.on(this.props.id).popTo(this.props.previousScreenIds[0]);
55 57
   }
56 58
 
59
+  onClickPopToRoot() {
60
+    Navigation.on(this.props.id).popToRoot();
61
+  }
62
+
57 63
   getStackPosition() {
58 64
     return this.props.stackPosition || 1;
59 65
   }