Browse Source

Parse externalComponent options

Guy Carmeli 6 years ago
parent
commit
5d9571459b

+ 38
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/ExternalComponent.java View File

@@ -0,0 +1,38 @@
1
+package com.reactnativenavigation.parse;
2
+
3
+import com.reactnativenavigation.parse.params.NullText;
4
+import com.reactnativenavigation.parse.params.Text;
5
+import com.reactnativenavigation.parse.parsers.TextParser;
6
+
7
+import org.json.JSONObject;
8
+
9
+public class ExternalComponent {
10
+
11
+    public Text classCreator = new NullText();
12
+
13
+    public static ExternalComponent parse(JSONObject json) {
14
+        ExternalComponent options = new ExternalComponent();
15
+        if (json == null) {
16
+            return options;
17
+        }
18
+
19
+        options.classCreator = TextParser.parse(json, "classCreator");
20
+        if (!options.classCreator.hasValue()) {
21
+            throw new RuntimeException("ExternalClass must declare classCreator - a fully qualified method name");
22
+        }
23
+
24
+        return options;
25
+    }
26
+
27
+    public void mergeWith(ExternalComponent other) {
28
+        if (other.classCreator.hasValue()) {
29
+            classCreator = other.classCreator;
30
+        }
31
+    }
32
+
33
+    public void mergeWithDefault(ExternalComponent defaultOptions) {
34
+        if (!classCreator.hasValue()) {
35
+            classCreator = defaultOptions.classCreator;
36
+        }
37
+    }
38
+}

+ 5
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/Options.java View File

@@ -29,6 +29,7 @@ public class Options implements DEFAULT_VALUES {
29 29
         result.fabOptions = FabOptions.parse(json.optJSONObject("fab"));
30 30
         result.animationsOptions = AnimationsOptions.parse(json.optJSONObject("animations"));
31 31
         result.sideMenuRootOptions = SideMenuRootOptions.parse(json.optJSONObject("sideMenu"));
32
+        result.externalComponent = ExternalComponent.parse(json.optJSONObject("externalComponent"));
32 33
 
33 34
         return result.withDefaultOptions(defaultOptions);
34 35
     }
@@ -43,6 +44,7 @@ public class Options implements DEFAULT_VALUES {
43 44
     @NonNull public FabOptions fabOptions = new FabOptions();
44 45
     @NonNull public AnimationsOptions animationsOptions = new AnimationsOptions();
45 46
     @NonNull public SideMenuRootOptions sideMenuRootOptions = new SideMenuRootOptions();
47
+    @NonNull public ExternalComponent externalComponent = new ExternalComponent();
46 48
 
47 49
     void setTopTabIndex(int i) {
48 50
         topTabOptions.tabIndex = i;
@@ -61,6 +63,7 @@ public class Options implements DEFAULT_VALUES {
61 63
         result.fabOptions.mergeWith(fabOptions);
62 64
         result.animationsOptions.mergeWith(animationsOptions);
63 65
         result.sideMenuRootOptions.mergeWith(sideMenuRootOptions);
66
+        result.externalComponent.mergeWith(externalComponent);
64 67
         return result;
65 68
     }
66 69
 
@@ -76,6 +79,7 @@ public class Options implements DEFAULT_VALUES {
76 79
         result.fabOptions.mergeWith(other.fabOptions);
77 80
         result.animationsOptions.mergeWith(other.animationsOptions);
78 81
         result.sideMenuRootOptions.mergeWith(other.sideMenuRootOptions);
82
+        result.externalComponent.mergeWith(other.externalComponent);
79 83
         return result;
80 84
     }
81 85
 
@@ -89,6 +93,7 @@ public class Options implements DEFAULT_VALUES {
89 93
         fabOptions.mergeWithDefault(other.fabOptions);
90 94
         animationsOptions.mergeWithDefault(other.animationsOptions);
91 95
         sideMenuRootOptions.mergeWithDefault(other.sideMenuRootOptions);
96
+        externalComponent.mergeWithDefault(other.externalComponent);
92 97
         return this;
93 98
     }
94 99
 

+ 19
- 5
lib/android/app/src/test/java/com/reactnativenavigation/parse/OptionsTest.java View File

@@ -39,6 +39,7 @@ public class OptionsTest extends BaseTest {
39 39
     private static final String BOTTOM_TABS_BADGE = "3";
40 40
     private static final String BOTTOM_TABS_CURRENT_TAB_ID = "ComponentId";
41 41
     private static final Number BOTTOM_TABS_CURRENT_TAB_INDEX = new Number(1);
42
+    private static final String EXTERNAL_CLASS_CREATOR = "com.rnn.creators.Creator.createFragment";
42 43
     private TypefaceLoader mockLoader;
43 44
 
44 45
     @Override
@@ -57,6 +58,7 @@ public class OptionsTest extends BaseTest {
57 58
         JSONObject json = new JSONObject()
58 59
                 .put("topBar", createTopBar(TOP_BAR_VISIBLE.get()))
59 60
                 .put("fab", createFab())
61
+                .put("externalComponent", createExternalComponent())
60 62
                 .put("bottomTabs", createBottomTabs());
61 63
         Options result = Options.parse(mockLoader, json);
62 64
         assertResult(result);
@@ -83,6 +85,7 @@ public class OptionsTest extends BaseTest {
83 85
         assertThat(result.fabOptions.hideOnScroll.get()).isEqualTo(FAB_HIDE_ON_SCROLL);
84 86
         assertThat(result.fabOptions.alignVertically.get()).isEqualTo(FAB_ALIGN_VERTICALLY);
85 87
         assertThat(result.fabOptions.alignHorizontally.get()).isEqualTo(FAB_ALIGN_HORIZONTALLY);
88
+        assertThat(result.externalComponent.classCreator.get()).isEqualTo(EXTERNAL_CLASS_CREATOR);
86 89
     }
87 90
 
88 91
     @NonNull
@@ -120,6 +123,11 @@ public class OptionsTest extends BaseTest {
120 123
                 .put("visible", FAB_VISIBLE);
121 124
     }
122 125
 
126
+    private JSONObject createExternalComponent() throws JSONException {
127
+        return new JSONObject()
128
+                .put("classCreator", EXTERNAL_CLASS_CREATOR);
129
+    }
130
+
123 131
     @NonNull
124 132
     private JSONObject createOtherFab() throws JSONException {
125 133
         return new JSONObject()
@@ -154,6 +162,10 @@ public class OptionsTest extends BaseTest {
154 162
                 .put("tabBadge", BOTTOM_TABS_BADGE);
155 163
     }
156 164
 
165
+    private JSONObject createOtherExternalClass() {
166
+        return new JSONObject();
167
+    }
168
+
157 169
     @Test
158 170
     public void mergeDoesNotMutate() throws Exception {
159 171
         JSONObject json1 = new JSONObject();
@@ -174,10 +186,11 @@ public class OptionsTest extends BaseTest {
174 186
 
175 187
     @Test
176 188
     public void mergeDefaultOptions() throws Exception {
177
-        JSONObject json = new JSONObject();
178
-        json.put("topBar", createTopBar(TOP_BAR_VISIBLE.get()));
179
-        json.put("fab", createFab());
180
-        json.put("bottomTabs", createBottomTabs());
189
+        JSONObject json = new JSONObject()
190
+                .put("topBar", createTopBar(TOP_BAR_VISIBLE.get()))
191
+                .put("fab", createFab())
192
+                .put("bottomTabs", createBottomTabs())
193
+                .put("externalComponent", createExternalComponent());
181 194
         Options defaultOptions = Options.parse(mockLoader, json);
182 195
         Options options = new Options();
183 196
 
@@ -189,7 +202,8 @@ public class OptionsTest extends BaseTest {
189 202
         JSONObject defaultJson = new JSONObject()
190 203
                 .put("topBar", createOtherTopBar())
191 204
                 .put("fab", createOtherFab())
192
-                .put("bottomTabs", createOtherBottomTabs());
205
+                .put("bottomTabs", createOtherBottomTabs())
206
+                .put("externalComponent", createExternalComponent());
193 207
         Options defaultOptions = Options.parse(mockLoader, defaultJson);
194 208
 
195 209
         JSONObject json = new JSONObject()