Browse Source

Android: Handle unknown node type use-case

Amit Davidi 7 years ago
parent
commit
e3e3d67fad

+ 18
- 8
android/app/src/main/java/com/reactnativenavigation/layout/LayoutFactory.java View File

@@ -1,6 +1,7 @@
1 1
 package com.reactnativenavigation.layout;
2 2
 
3 3
 import android.app.Activity;
4
+import android.support.annotation.NonNull;
4 5
 import android.view.View;
5 6
 
6 7
 import java.util.List;
@@ -21,17 +22,26 @@ public class LayoutFactory {
21 22
     public View create(LayoutNode node) {
22 23
         switch (node.type) {
23 24
             case "Container":
24
-                String name = (String) node.data.get("name");
25
-                return new Container(activity, rootViewCreator, node.id, name);
26
-
25
+                return createContainerView(node);
27 26
             case "ContainerStack":
28
-                ContainerStack containerStack = new ContainerStack(activity);
29
-                List<LayoutNode> children = node.children;
30
-                addChildrenNodes(containerStack, children);
31
-                return containerStack;
27
+                return createContainerStackView(node);
28
+            default:
29
+                throw new IllegalArgumentException("Invalid node type: "+node.type);
32 30
         }
31
+    }
32
+
33
+    @NonNull
34
+    private View createContainerStackView(LayoutNode node) {
35
+        ContainerStack containerStack = new ContainerStack(activity);
36
+        List<LayoutNode> children = node.children;
37
+        addChildrenNodes(containerStack, children);
38
+        return containerStack;
39
+    }
33 40
 
34
-        return null;
41
+    @NonNull
42
+    private View createContainerView(LayoutNode node) {
43
+        String name = (String) node.data.get("name");
44
+        return new Container(activity, rootViewCreator, node.id, name);
35 45
     }
36 46
 
37 47
     private void addChildrenNodes(ContainerStack containerStack, List<LayoutNode> children) {

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

@@ -49,14 +49,6 @@ public class NavigationModule extends ReactContextBaseJavaModule {
49 49
                 final LayoutNode layoutTreeRoot = readableMapToLayoutNode(layoutTree);
50 50
                 final View rootView = factory.create(layoutTreeRoot);
51 51
                 NavigationActivity.instance.setContentView(rootView);
52
-
53
-//                Map<String, Object> node = new HashMap<String, Object>();
54
-//                node.put("id", container.getString("id"));
55
-//                HashMap<String, Object> data = new HashMap<>();
56
-//                data.put("name", container.getMap("data").getString("name"));
57
-//                node.put("data", data);
58
-//                View rootView = factory.create(node);
59
-//                NavigationActivity.instance.setContentView(rootView);
60 52
             }
61 53
         });
62 54
     }
@@ -88,8 +80,6 @@ public class NavigationModule extends ReactContextBaseJavaModule {
88 80
                 case Map:
89 81
                     map.put(key, readableMapToJavaMap(readableMap.getMap(key)));
90 82
                     break;
91
-                default:
92
-                    throw new IllegalArgumentException("WTF?!");
93 83
             }
94 84
         }
95 85
         return map;

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

@@ -17,6 +17,7 @@ import org.robolectric.RobolectricTestRunner;
17 17
 
18 18
 import java.util.ArrayList;
19 19
 import java.util.Arrays;
20
+import java.util.Collections;
20 21
 import java.util.HashMap;
21 22
 import java.util.List;
22 23
 
@@ -89,6 +90,14 @@ public class LayoutFactoryTest {
89 90
         assertViewChildren(container2, mockView2);
90 91
     }
91 92
 
93
+    @Test(expected = IllegalArgumentException.class)
94
+    public void throwsExceptionForUnknownType() {
95
+        when(rootViewCreator.createRootView(eq(VIEW_ID), eq(VIEW_NAME))).thenReturn(mockView);
96
+        final LayoutNode node = new LayoutNode(VIEW_ID, "***unknownType***", Collections.<String, Object>emptyMap());
97
+
98
+        createLayoutFactory().create(node);
99
+    }
100
+
92 101
     private LayoutFactory createLayoutFactory() {
93 102
         return new LayoutFactory(Robolectric.buildActivity(Activity.class).get(), rootViewCreator);
94 103
     }