Browse Source

android popToRoot

Daniel Zlotin 7 years ago
parent
commit
b76a1c7290

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

@@ -42,4 +42,15 @@ public class ScreenStackTest extends BaseTest {
42 42
 		elementByText("POP TO STACK POSITION 1").click();
43 43
 		assertExists(By.text("Stack Position: 1"));
44 44
 	}
45
+
46
+	@Test
47
+	public void popToRoot() throws Exception {
48
+		launchTheApp();
49
+		assertMainShown();
50
+		elementByText("PUSH").click();
51
+		elementByText("PUSH").click();
52
+		elementByText("PUSH").click();
53
+		elementByText("POP TO ROOT").click();
54
+		assertMainShown();
55
+	}
45 56
 }

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

@@ -76,6 +76,16 @@ public class NavigationModule extends ReactContextBaseJavaModule {
76 76
 		});
77 77
 	}
78 78
 
79
+	@ReactMethod
80
+	public void popToRoot(final String onContainerId) {
81
+		handle(new Runnable() {
82
+			@Override
83
+			public void run() {
84
+				store.getViewController(onContainerId).getStackController().popToRoot();
85
+			}
86
+		});
87
+	}
88
+
79 89
 	private NavigationActivity activity() {
80 90
 		return (NavigationActivity) getCurrentActivity();
81 91
 	}

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

@@ -99,4 +99,10 @@ public class StackController extends ViewController {
99 99
 			pop();
100 100
 		}
101 101
 	}
102
+
103
+	public void popToRoot() {
104
+		while (canPop()) {
105
+			pop();
106
+		}
107
+	}
102 108
 }

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

@@ -203,6 +203,28 @@ public class StackControllerTest extends BaseTest {
203 203
 		assertThat(uut.size()).isEqualTo(2);
204 204
 	}
205 205
 
206
+	@Test
207
+	public void popToRoot_PopsEverythingAboveFirstController() throws Exception {
208
+		uut.push(child1);
209
+		uut.push(child2);
210
+		uut.push(child3);
211
+
212
+		assertThat(uut.size()).isEqualTo(3);
213
+		assertThat(uut.peek()).isEqualTo(child3);
214
+
215
+		uut.popToRoot();
216
+
217
+		assertThat(uut.size()).isEqualTo(1);
218
+		assertThat(uut.peek()).isEqualTo(child1);
219
+	}
220
+
221
+	@Test
222
+	public void popToRoot_EmptyStackDoesNothing() throws Exception {
223
+		assertThat(uut.isEmpty()).isTrue();
224
+		uut.popToRoot();
225
+		assertThat(uut.isEmpty()).isTrue();
226
+	}
227
+
206 228
 	private void assertHasSingleChildViewOfController(ViewController childController) {
207 229
 		assertThat(uut.getView().getChildCount()).isEqualTo(1);
208 230
 		assertThat(uut.getView().getChildAt(0)).isEqualTo(childController.getView());