Browse Source

#833 - pop screen by id when not on top

Ran Greenberg 7 years ago
parent
commit
0117b1d61e
3 changed files with 32 additions and 5 deletions
  1. 8
    1
      ios/RNNBridgeModule.m
  2. 12
    0
      playground/e2e/app.test.js
  3. 12
    4
      playground/src/containers/PushedScreen.js

+ 8
- 1
ios/RNNBridgeModule.m View File

40
 	[self assertReady];
40
 	[self assertReady];
41
 	UIViewController *vc = [[RNN instance].store findContainerForId:containerId];
41
 	UIViewController *vc = [[RNN instance].store findContainerForId:containerId];
42
 	
42
 	
43
-	[[vc navigationController] popViewControllerAnimated:YES];
43
+	if([[vc navigationController] topViewController] == vc ) {
44
+		[[vc navigationController] popViewControllerAnimated:YES];
45
+	}
46
+	else {
47
+		NSMutableArray * vcs = [vc navigationController].viewControllers.mutableCopy;
48
+		[vcs removeObject:vc];
49
+		[[vc navigationController] setViewControllers:vcs animated:NO];
50
+	}
44
 	[[RNN instance].store removeContainer:containerId];
51
 	[[RNN instance].store removeContainer:containerId];
45
 }
52
 }
46
 
53
 

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

38
     elementByLabel('Pop').tap();
38
     elementByLabel('Pop').tap();
39
     expect(elementByLabel('React Native Navigation!')).toBeVisible();
39
     expect(elementByLabel('React Native Navigation!')).toBeVisible();
40
   });
40
   });
41
+  
42
+  it.only('pop screen deep in the stack', () => {
43
+    elementByLabel('Push').tap();
44
+    expect(elementByLabel('Stack Position: 1')).toBeVisible();
45
+    elementByLabel('Push').tap();
46
+    expect(elementByLabel('Stack Position: 2')).toBeVisible();
47
+    elementByLabel('Pop Previous').tap();
48
+    expect(elementByLabel('Stack Position: 2')).toBeVisible();
49
+    elementByLabel('Pop').tap();
50
+    expect(elementByLabel('React Native Navigation!')).toBeVisible();
51
+  
52
+  });
41
 
53
 
42
   it('show modal', () => {
54
   it('show modal', () => {
43
     elementByLabel('Show Modal').tap();
55
     elementByLabel('Show Modal').tap();

+ 12
- 4
playground/src/containers/PushedScreen.js View File

1
+import _ from 'lodash';
1
 import React, { Component } from 'react';
2
 import React, { Component } from 'react';
2
 import {
3
 import {
3
   StyleSheet,
4
   StyleSheet,
13
     super(props);
14
     super(props);
14
     this.onClickPush = this.onClickPush.bind(this);
15
     this.onClickPush = this.onClickPush.bind(this);
15
     this.onClickPop = this.onClickPop.bind(this);
16
     this.onClickPop = this.onClickPop.bind(this);
17
+    this.onClickPopPrevious = this.onClickPopPrevious.bind(this);
16
   }
18
   }
17
   render() {
19
   render() {
18
     return (
20
     return (
21
         <Text style={styles.h2}>{`Stack Position: ${this.getStackPosition()}`}</Text>
23
         <Text style={styles.h2}>{`Stack Position: ${this.getStackPosition()}`}</Text>
22
         <Button title="Push" onPress={this.onClickPush} />
24
         <Button title="Push" onPress={this.onClickPush} />
23
         <Button title="Pop" onPress={this.onClickPop} />
25
         <Button title="Pop" onPress={this.onClickPop} />
26
+        <Button title="Pop Previous" onPress={this.onClickPopPrevious} />
24
         <Text style={styles.footer}>{`this.props.id = ${this.props.id}`}</Text>
27
         <Text style={styles.footer}>{`this.props.id = ${this.props.id}`}</Text>
25
       </View>
28
       </View>
26
     );
29
     );
27
   }
30
   }
28
-
31
+  
29
   onClickPush() {
32
   onClickPush() {
30
     Navigation.on(this.props.id).push({
33
     Navigation.on(this.props.id).push({
31
       name: 'navigation.playground.PushedScreen',
34
       name: 'navigation.playground.PushedScreen',
32
       passProps: {
35
       passProps: {
33
-        stackPosition: this.getStackPosition() + 1
36
+        stackPosition: this.getStackPosition() + 1,
37
+        previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.id)
34
       }
38
       }
35
     });
39
     });
36
   }
40
   }
37
-
41
+  
38
   onClickPop() {
42
   onClickPop() {
39
     Navigation.on(this.props.id).pop();
43
     Navigation.on(this.props.id).pop();
40
   }
44
   }
41
-
45
+  
46
+  onClickPopPrevious() {
47
+    Navigation.on(_.last(this.props.previousScreenIds)).pop();
48
+  }
49
+  
42
   getStackPosition() {
50
   getStackPosition() {
43
     return this.props.stackPosition || 1;
51
     return this.props.stackPosition || 1;
44
   }
52
   }