Browse Source

push and pop multiple screens from container stack

Guy Carmeli 8 years ago
parent
commit
ab785acc16

+ 6
- 4
android/app/src/main/java/com/reactnativenavigation/layout/ContainerStackLayout.java View File

@@ -4,9 +4,11 @@ import android.content.Context;
4 4
 import android.view.View;
5 5
 import android.widget.FrameLayout;
6 6
 
7
+import java.util.Stack;
8
+
7 9
 public class ContainerStackLayout extends FrameLayout implements StackLayout {
8 10
 
9
-    private View removedView;
11
+    private Stack<View> backStack = new Stack<>();
10 12
 
11 13
     public ContainerStackLayout(Context context) {
12 14
         super(context);
@@ -15,13 +17,13 @@ public class ContainerStackLayout extends FrameLayout implements StackLayout {
15 17
     @Override
16 18
     public void push(View view) {
17 19
         addView(view);
18
-        removedView = getChildAt(0);
19
-        removeView(removedView);
20
+        backStack.push(getChildAt(0));
21
+        removeView(getChildAt(0));
20 22
     }
21 23
 
22 24
     @Override
23 25
     public void pop() {
24
-        addView(removedView);
26
+        addView(backStack.pop());
25 27
         removeView(getChildAt(0));
26 28
     }
27 29
 

+ 40
- 0
android/app/src/test/java/com/reactnativenavigation/LayoutFactoryTest.java View File

@@ -187,6 +187,46 @@ public class LayoutFactoryTest {
187 187
         assertThat(result.getChildAt(0)).isEqualTo(otherMockView);
188 188
     }
189 189
 
190
+    @Test
191
+    public void pushTwoScreensToStackLayout() throws Exception {
192
+        when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
193
+        final LayoutNode container = createContainerNode();
194
+        final LayoutNode stackNode = createContainerStackNode(container);
195
+        final ContainerStackLayout containerStackLayout = (ContainerStackLayout) createLayoutFactory().create(stackNode);
196
+
197
+        View first = new View(activity);
198
+        pushContainer(containerStackLayout, OTHER_NODE_ID, OTHER_REACT_ROOT_VIEW_KEY, first);
199
+
200
+        View second = new View(activity);
201
+        pushContainer(containerStackLayout, "secondPushedScreenId", "secondPushedScreenKey", second);
202
+
203
+        ViewGroup result = (ViewGroup) TestUtils.assertViewChildrenCount(containerStackLayout, 1).get(0);
204
+        assertThat(result.getChildAt(0)).isEqualTo(second);
205
+    }
206
+
207
+    @Test
208
+    public void popTwoScreensFromStackLayout() throws Exception {
209
+        when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
210
+        final LayoutNode container = createContainerNode();
211
+        final LayoutNode stackNode = createContainerStackNode(container);
212
+        final ContainerStackLayout containerStackLayout = (ContainerStackLayout) createLayoutFactory().create(stackNode);
213
+
214
+        pushContainer(containerStackLayout, OTHER_NODE_ID, OTHER_REACT_ROOT_VIEW_KEY, new View(activity));
215
+        pushContainer(containerStackLayout, "secondPushedScreenId", "secondPushedScreenKey", new View(activity));
216
+
217
+        containerStackLayout.pop();
218
+        containerStackLayout.pop();
219
+
220
+        ViewGroup result = (ViewGroup) TestUtils.assertViewChildrenCount(containerStackLayout, 1).get(0);
221
+        assertThat(result.getChildAt(0)).isEqualTo(mockView);
222
+    }
223
+
224
+    private void pushContainer(ContainerStackLayout containerStackLayout, String screenId, String reactRootViewKey, View rootView) {
225
+        when(reactRootViewCreator.create(eq(screenId), eq(reactRootViewKey))).thenReturn(rootView);
226
+        View pushedContainer = createLayoutFactory().create(createContainerNode(screenId, reactRootViewKey));
227
+        containerStackLayout.push(pushedContainer);
228
+    }
229
+
190 230
     @Test
191 231
     public void popScreenFromScreenStackLayout() {
192 232
         when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);