Browse Source

android popTo viewController

Daniel Zlotin 7 years ago
parent
commit
8377393bb9

+ 36
- 0
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ScreenLifecycleTest.java View File

@@ -0,0 +1,36 @@
1
+package com.reactnativenavigation.e2e.androide2e;
2
+
3
+import android.support.test.uiautomator.By;
4
+
5
+import org.junit.Ignore;
6
+import org.junit.Test;
7
+
8
+public class ScreenLifecycleTest extends BaseTest {
9
+
10
+	@Test
11
+	public void onStartOnStop() throws Exception {
12
+		launchTheApp();
13
+		assertMainShown();
14
+		elementByText("PUSH LIFECYCLE SCREEN").click();
15
+		assertExists(By.text("onStart"));
16
+		elementByText("PUSH TO TEST ONSTOP").click();
17
+		assertExists(By.text("onStop"));
18
+	}
19
+
20
+	@Ignore
21
+	@Test
22
+	public void unmountIsCalledWhenPopped() throws Exception {
23
+		launchTheApp();
24
+		assertMainShown();
25
+		elementByText("PUSH LIFECYCLE SCREEN").click();
26
+		assertExists(By.text("onStart"));
27
+
28
+		device().pressBack();
29
+
30
+		assertExists(By.text("componentWillUnmount"));
31
+		elementByText("OK").click();
32
+		assertExists(By.text("onStop"));
33
+		elementByText("OK").click();
34
+		assertMainShown();
35
+	}
36
+}

+ 12
- 0
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ScreenStackTest.java View File

@@ -30,4 +30,16 @@ public class ScreenStackTest extends BaseTest {
30 30
 		elementByText("POP").click();
31 31
 		assertMainShown();
32 32
 	}
33
+
34
+	@Test
35
+	public void popToId() throws Exception {
36
+		launchTheApp();
37
+		assertMainShown();
38
+		elementByText("PUSH").click();
39
+		elementByText("PUSH").click();
40
+		elementByText("PUSH").click();
41
+		assertExists(By.text("Stack Position: 3"));
42
+		elementByText("POP TO STACK POSITION 1").click();
43
+		assertExists(By.text("Stack Position: 1"));
44
+	}
33 45
 }

+ 0
- 25
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/TopLevelApiTest.java View File

@@ -27,29 +27,4 @@ public class TopLevelApiTest extends BaseTest {
27 27
 		swipeRightOpenSideMenu();
28 28
 		assertExists(By.text("This is a left side menu screen"));
29 29
 	}
30
-
31
-	@Test
32
-	public void screenLifecycle() throws Exception {
33
-		launchTheApp();
34
-		assertMainShown();
35
-		elementByText("PUSH LIFECYCLE SCREEN").click();
36
-		assertExists(By.text("onStart"));
37
-		elementByText("PUSH TO TEST ONSTOP").click();
38
-		assertExists(By.text("onStop"));
39
-	}
40
-
41
-	@Test
42
-	@Ignore
43
-	public void unmountIsCalledOnPop() throws Exception {
44
-		launchTheApp();
45
-		assertMainShown();
46
-		elementByText("PUSH LIFECYCLE SCREEN").click();
47
-		elementByText("onStart");
48
-		device().pressBack();
49
-		assertExists(By.text("componentWillUnmount"));
50
-		elementByText("OK").click();
51
-		assertExists(By.text("onStop"));
52
-		elementByText("OK").click();
53
-		assertMainShown();
54
-	}
55 30
 }

+ 10
- 0
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java View File

@@ -66,6 +66,16 @@ public class NavigationModule extends ReactContextBaseJavaModule {
66 66
 		});
67 67
 	}
68 68
 
69
+	@ReactMethod
70
+	public void popTo(final String onContainerId, final String toContainerId) {
71
+		handle(new Runnable() {
72
+			@Override
73
+			public void run() {
74
+				store.getViewController(onContainerId).getStackController().popTo(store.getViewController(toContainerId));
75
+			}
76
+		});
77
+	}
78
+
69 79
 	private NavigationActivity activity() {
70 80
 		return (NavigationActivity) getCurrentActivity();
71 81
 	}

+ 18
- 9
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java View File

@@ -8,7 +8,7 @@ import android.widget.FrameLayout;
8 8
 import java.util.ArrayDeque;
9 9
 
10 10
 public class StackController extends ViewController {
11
-	private final ArrayDeque<ViewController> childControllers = new ArrayDeque<>();
11
+	private final ArrayDeque<ViewController> stack = new ArrayDeque<>();
12 12
 
13 13
 	public StackController(final Activity activity) {
14 14
 		super(activity);
@@ -18,7 +18,7 @@ public class StackController extends ViewController {
18 18
 		ViewController previousTop = peek();
19 19
 
20 20
 		child.setStackController(this);
21
-		childControllers.push(child);
21
+		stack.push(child);
22 22
 		getView().addView(child.getView());
23 23
 
24 24
 		if (previousTop != null) {
@@ -27,14 +27,14 @@ public class StackController extends ViewController {
27 27
 	}
28 28
 
29 29
 	public boolean canPop() {
30
-		return childControllers.size() > 1;
30
+		return stack.size() > 1;
31 31
 	}
32 32
 
33 33
 	public void pop() {
34 34
 		if (!canPop()) {
35 35
 			return;
36 36
 		}
37
-		ViewController poppedController = childControllers.pop();
37
+		ViewController poppedController = stack.pop();
38 38
 		getView().removeView(poppedController.getView());
39 39
 
40 40
 		ViewController previousTop = peek();
@@ -45,20 +45,20 @@ public class StackController extends ViewController {
45 45
 		if (peek() == childController) {
46 46
 			pop();
47 47
 		} else {
48
-			childControllers.remove(childController);
48
+			stack.remove(childController);
49 49
 		}
50 50
 	}
51 51
 
52 52
 	public ViewController peek() {
53
-		return childControllers.peek();
53
+		return stack.peek();
54 54
 	}
55 55
 
56 56
 	public int size() {
57
-		return childControllers.size();
57
+		return stack.size();
58 58
 	}
59 59
 
60 60
 	public boolean isEmpty() {
61
-		return childControllers.isEmpty();
61
+		return stack.isEmpty();
62 62
 	}
63 63
 
64 64
 	@Override
@@ -88,6 +88,15 @@ public class StackController extends ViewController {
88 88
 	}
89 89
 
90 90
 	ArrayDeque<ViewController> getStack() {
91
-		return childControllers;
91
+		return stack;
92
+	}
93
+
94
+	public void popTo(final ViewController viewController) {
95
+		if (!stack.contains(viewController)) {
96
+			return;
97
+		}
98
+		while (peek() != viewController) {
99
+			pop();
100
+		}
92 101
 	}
93 102
 }

+ 24
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackControllerTest.java View File

@@ -179,6 +179,30 @@ public class StackControllerTest extends BaseTest {
179 179
 		assertThat(uut.getStackController()).isEqualTo(uut);
180 180
 	}
181 181
 
182
+	@Test
183
+	public void popTo_PopsTopUntilControllerIsNewTop() throws Exception {
184
+		uut.push(child1);
185
+		uut.push(child2);
186
+		uut.push(child3);
187
+
188
+		assertThat(uut.size()).isEqualTo(3);
189
+		assertThat(uut.peek()).isEqualTo(child3);
190
+
191
+		uut.popTo(child1);
192
+
193
+		assertThat(uut.size()).isEqualTo(1);
194
+		assertThat(uut.peek()).isEqualTo(child1);
195
+	}
196
+
197
+	@Test
198
+	public void popTo_NotAChildOfThisStack_DoesNothing() throws Exception {
199
+		uut.push(child1);
200
+		uut.push(child3);
201
+		assertThat(uut.size()).isEqualTo(2);
202
+		uut.popTo(child2);
203
+		assertThat(uut.size()).isEqualTo(2);
204
+	}
205
+
182 206
 	private void assertHasSingleChildViewOfController(ViewController childController) {
183 207
 		assertThat(uut.getView().getChildCount()).isEqualTo(1);
184 208
 		assertThat(uut.getView().getChildAt(0)).isEqualTo(childController.getView());