Parcourir la source

push and pop multiple screens from container stack

Guy Carmeli il y a 8 ans
Parent
révision
ab785acc16

+ 6
- 4
android/app/src/main/java/com/reactnativenavigation/layout/ContainerStackLayout.java Voir le fichier

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

+ 40
- 0
android/app/src/test/java/com/reactnativenavigation/LayoutFactoryTest.java Voir le fichier

187
         assertThat(result.getChildAt(0)).isEqualTo(otherMockView);
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
     @Test
230
     @Test
191
     public void popScreenFromScreenStackLayout() {
231
     public void popScreenFromScreenStackLayout() {
192
         when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
232
         when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);