Browse Source

Android: Handle unknown node type use-case

Amit Davidi 8 years ago
parent
commit
e3e3d67fad

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

1
 package com.reactnativenavigation.layout;
1
 package com.reactnativenavigation.layout;
2
 
2
 
3
 import android.app.Activity;
3
 import android.app.Activity;
4
+import android.support.annotation.NonNull;
4
 import android.view.View;
5
 import android.view.View;
5
 
6
 
6
 import java.util.List;
7
 import java.util.List;
21
     public View create(LayoutNode node) {
22
     public View create(LayoutNode node) {
22
         switch (node.type) {
23
         switch (node.type) {
23
             case "Container":
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
             case "ContainerStack":
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
     private void addChildrenNodes(ContainerStack containerStack, List<LayoutNode> children) {
47
     private void addChildrenNodes(ContainerStack containerStack, List<LayoutNode> children) {

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

49
                 final LayoutNode layoutTreeRoot = readableMapToLayoutNode(layoutTree);
49
                 final LayoutNode layoutTreeRoot = readableMapToLayoutNode(layoutTree);
50
                 final View rootView = factory.create(layoutTreeRoot);
50
                 final View rootView = factory.create(layoutTreeRoot);
51
                 NavigationActivity.instance.setContentView(rootView);
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
                 case Map:
80
                 case Map:
89
                     map.put(key, readableMapToJavaMap(readableMap.getMap(key)));
81
                     map.put(key, readableMapToJavaMap(readableMap.getMap(key)));
90
                     break;
82
                     break;
91
-                default:
92
-                    throw new IllegalArgumentException("WTF?!");
93
             }
83
             }
94
         }
84
         }
95
         return map;
85
         return map;

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

17
 
17
 
18
 import java.util.ArrayList;
18
 import java.util.ArrayList;
19
 import java.util.Arrays;
19
 import java.util.Arrays;
20
+import java.util.Collections;
20
 import java.util.HashMap;
21
 import java.util.HashMap;
21
 import java.util.List;
22
 import java.util.List;
22
 
23
 
89
         assertViewChildren(container2, mockView2);
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
     private LayoutFactory createLayoutFactory() {
101
     private LayoutFactory createLayoutFactory() {
93
         return new LayoutFactory(Robolectric.buildActivity(Activity.class).get(), rootViewCreator);
102
         return new LayoutFactory(Robolectric.buildActivity(Activity.class).get(), rootViewCreator);
94
     }
103
     }