Browse Source

navigator pop

Daniel Zlotin 7 years ago
parent
commit
7923cbcbbe

+ 13
- 3
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java View File

@@ -27,7 +27,7 @@ public class Navigator extends ParentController {
27 27
 
28 28
 	@Override
29 29
 	public Collection<ViewController> getChildControllers() {
30
-		return Collections.singletonList(root);
30
+		return root == null ? Collections.<ViewController>emptyList() : Collections.singletonList(root);
31 31
 	}
32 32
 
33 33
 	/*
@@ -65,8 +65,8 @@ public class Navigator extends ParentController {
65 65
 		getView().addView(viewController.getView());
66 66
 	}
67 67
 
68
-	public void push(final String onId, final ViewController viewController) {
69
-		ViewController found = root.findControllerById(onId);
68
+	public void push(final String fromId, final ViewController viewController) {
69
+		ViewController found = findControllerById(fromId);
70 70
 		if (found == null) return;
71 71
 
72 72
 		StackController parentStackController = found.getParentStackController();
@@ -74,4 +74,14 @@ public class Navigator extends ParentController {
74 74
 
75 75
 		parentStackController.push(viewController);
76 76
 	}
77
+
78
+	public void pop(final String fromId) {
79
+		ViewController found = findControllerById(fromId);
80
+		if (found == null) return;
81
+
82
+		StackController parentStackController = found.getParentStackController();
83
+		if (parentStackController == null) return;
84
+
85
+		parentStackController.pop();
86
+	}
77 87
 }

+ 26
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigatorTest.java View File

@@ -18,6 +18,7 @@ public class NavigatorTest extends BaseTest {
18 18
 	private Navigator uut;
19 19
 	private ViewController child1;
20 20
 	private ViewController child2;
21
+	private ViewController child3;
21 22
 
22 23
 	@Override
23 24
 	public void beforeEach() {
@@ -26,6 +27,7 @@ public class NavigatorTest extends BaseTest {
26 27
 		uut = new Navigator(activity);
27 28
 		child1 = new SimpleViewController(activity, "child1");
28 29
 		child2 = new SimpleViewController(activity, "child2");
30
+		child3 = new SimpleViewController(activity, "child3");
29 31
 	}
30 32
 
31 33
 
@@ -97,8 +99,8 @@ public class NavigatorTest extends BaseTest {
97 99
 		stack1.push(child1);
98 100
 		stack2.push(child2);
99 101
 		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
100
-
101 102
 		uut.setRoot(bottomTabsController);
103
+
102 104
 		SimpleViewController newChild = new SimpleViewController(activity, "new child");
103 105
 		uut.push(child2.getId(), newChild);
104 106
 
@@ -106,6 +108,29 @@ public class NavigatorTest extends BaseTest {
106 108
 		assertThat(stack2.getChildControllers()).contains(newChild);
107 109
 	}
108 110
 
111
+	@Test
112
+	public void pop_InvalidDoesNothing() throws Exception {
113
+		uut.pop("123");
114
+		uut.setRoot(child1);
115
+		uut.pop(child1.getId());
116
+	}
117
+
118
+	@Test
119
+	public void pop_FromCorrectStackByFindingChildId() throws Exception {
120
+		BottomTabsController bottomTabsController = new BottomTabsController(activity, "tabsController");
121
+		StackController stack1 = new StackController(activity, "stack1");
122
+		StackController stack2 = new StackController(activity, "stack2");
123
+		stack1.push(child1);
124
+		stack2.push(child2);
125
+		stack2.push(child3);
126
+		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
127
+		uut.setRoot(bottomTabsController);
128
+
129
+		uut.pop(child2.getId());
130
+
131
+		assertThat(stack2.getChildControllers()).containsOnly(child2);
132
+	}
133
+
109 134
 	private void assertHasSingleChildViewOf(ViewController parent, ViewController child) {
110 135
 		assertThat(((ViewGroup) parent.getView()).getChildCount()).isEqualTo(1);
111 136
 		assertThat(((ViewGroup) parent.getView()).getChildAt(0)).isEqualTo(child.getView()).isNotNull();