Browse Source

[Breaking] Consolidate screenBackgroundColor and orientation to layout node

screenBackgroundColor was also renamed to backgroundColor

```js
layout: {
  orientation: 'landscape',
  backgroundColor: 'red'
}
```
Guy Carmeli 6 years ago
parent
commit
e171d7abc4

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

1
+package com.reactnativenavigation.parse;
2
+
3
+import com.reactnativenavigation.parse.params.Color;
4
+import com.reactnativenavigation.parse.params.NullColor;
5
+import com.reactnativenavigation.parse.params.NullNumber;
6
+import com.reactnativenavigation.parse.params.Number;
7
+import com.reactnativenavigation.parse.parsers.ColorParser;
8
+import com.reactnativenavigation.parse.parsers.NumberParser;
9
+
10
+import org.json.JSONObject;
11
+
12
+public class LayoutOptions {
13
+    public static LayoutOptions parse(JSONObject json) {
14
+        LayoutOptions result = new LayoutOptions();
15
+        if (json == null) return result;
16
+
17
+        result.backgroundColor = ColorParser.parse(json, "backgroundColor");
18
+        result.topMargin = NumberParser.parse(json, "topMargin");
19
+        result.orientation = OrientationOptions.parse(json);
20
+
21
+        return result;
22
+    }
23
+
24
+    public Color backgroundColor = new NullColor();
25
+    public Number topMargin = new NullNumber();
26
+    public OrientationOptions orientation = new OrientationOptions();
27
+
28
+    public void mergeWith(LayoutOptions other) {
29
+        if (other.backgroundColor.hasValue()) backgroundColor = other.backgroundColor;
30
+        if (other.topMargin.hasValue()) topMargin = other.topMargin;
31
+        if (other.orientation.hasValue()) orientation = other.orientation;
32
+
33
+    }
34
+
35
+    public void mergeWithDefault(LayoutOptions defaultOptions) {
36
+        if (!backgroundColor.hasValue()) backgroundColor = defaultOptions.backgroundColor;
37
+        if (!topMargin.hasValue()) topMargin = defaultOptions.topMargin;
38
+        if (!orientation.hasValue()) orientation = defaultOptions.orientation;
39
+    }
40
+}

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

24
         Options result = new Options();
24
         Options result = new Options();
25
         if (json == null) return result.withDefaultOptions(defaultOptions);
25
         if (json == null) return result.withDefaultOptions(defaultOptions);
26
 
26
 
27
-        result.orientationOptions = OrientationOptions.parse(json);
28
         result.topBar = TopBarOptions.parse(typefaceManager, json.optJSONObject("topBar"));
27
         result.topBar = TopBarOptions.parse(typefaceManager, json.optJSONObject("topBar"));
29
         result.topTabsOptions = TopTabsOptions.parse(json.optJSONObject("topTabs"));
28
         result.topTabsOptions = TopTabsOptions.parse(json.optJSONObject("topTabs"));
30
         result.topTabOptions = TopTabOptions.parse(typefaceManager, json.optJSONObject("topTab"));
29
         result.topTabOptions = TopTabOptions.parse(typefaceManager, json.optJSONObject("topTab"));
37
         result.screenBackgroundColor = ColorParser.parse(json, "screenBackgroundColor");
36
         result.screenBackgroundColor = ColorParser.parse(json, "screenBackgroundColor");
38
         result.modal = ModalOptions.parse(json);
37
         result.modal = ModalOptions.parse(json);
39
         result.statusBar = StatusBarOptions.parse(json.optJSONObject("statusBar"));
38
         result.statusBar = StatusBarOptions.parse(json.optJSONObject("statusBar"));
39
+        result.layout = LayoutOptions.parse(json.optJSONObject("layout"));
40
 
40
 
41
         return result.withDefaultOptions(defaultOptions);
41
         return result.withDefaultOptions(defaultOptions);
42
     }
42
     }
43
 
43
 
44
-    @NonNull public OrientationOptions orientationOptions = new OrientationOptions();
45
     @NonNull public TopBarOptions topBar = new TopBarOptions();
44
     @NonNull public TopBarOptions topBar = new TopBarOptions();
46
     @NonNull public TopTabsOptions topTabsOptions = new TopTabsOptions();
45
     @NonNull public TopTabsOptions topTabsOptions = new TopTabsOptions();
47
     @NonNull public TopTabOptions topTabOptions = new TopTabOptions();
46
     @NonNull public TopTabOptions topTabOptions = new TopTabOptions();
54
     @NonNull public Color screenBackgroundColor = new NullColor();
53
     @NonNull public Color screenBackgroundColor = new NullColor();
55
     @NonNull public ModalOptions modal = new ModalOptions();
54
     @NonNull public ModalOptions modal = new ModalOptions();
56
     @NonNull public StatusBarOptions statusBar = new StatusBarOptions();
55
     @NonNull public StatusBarOptions statusBar = new StatusBarOptions();
56
+    @NonNull public LayoutOptions layout = new LayoutOptions();
57
 
57
 
58
     void setTopTabIndex(int i) {
58
     void setTopTabIndex(int i) {
59
         topTabOptions.tabIndex = i;
59
         topTabOptions.tabIndex = i;
62
     @CheckResult
62
     @CheckResult
63
     public Options copy() {
63
     public Options copy() {
64
         Options result = new Options();
64
         Options result = new Options();
65
-        result.orientationOptions.mergeWith(orientationOptions);
66
         result.topBar.mergeWith(topBar);
65
         result.topBar.mergeWith(topBar);
67
         result.topTabsOptions.mergeWith(topTabsOptions);
66
         result.topTabsOptions.mergeWith(topTabsOptions);
68
         result.topTabOptions.mergeWith(topTabOptions);
67
         result.topTabOptions.mergeWith(topTabOptions);
75
         result.screenBackgroundColor = screenBackgroundColor;
74
         result.screenBackgroundColor = screenBackgroundColor;
76
         result.modal.mergeWith(modal);
75
         result.modal.mergeWith(modal);
77
         result.statusBar.mergeWith(statusBar);
76
         result.statusBar.mergeWith(statusBar);
77
+        result.layout.mergeWith(layout);
78
         return result;
78
         return result;
79
     }
79
     }
80
 
80
 
81
     @CheckResult
81
     @CheckResult
82
 	public Options mergeWith(final Options other) {
82
 	public Options mergeWith(final Options other) {
83
         Options result = copy();
83
         Options result = copy();
84
-        result.orientationOptions.mergeWith(other.orientationOptions);
85
         result.topBar.mergeWith(other.topBar);
84
         result.topBar.mergeWith(other.topBar);
86
         result.topTabsOptions.mergeWith(other.topTabsOptions);
85
         result.topTabsOptions.mergeWith(other.topTabsOptions);
87
         result.topTabOptions.mergeWith(other.topTabOptions);
86
         result.topTabOptions.mergeWith(other.topTabOptions);
93
         if (other.screenBackgroundColor.hasValue()) result.screenBackgroundColor = other.screenBackgroundColor;
92
         if (other.screenBackgroundColor.hasValue()) result.screenBackgroundColor = other.screenBackgroundColor;
94
         result.modal.mergeWith(other.modal);
93
         result.modal.mergeWith(other.modal);
95
         result.statusBar.mergeWith(other.statusBar);
94
         result.statusBar.mergeWith(other.statusBar);
95
+        result.layout.mergeWith(other.layout);
96
         return result;
96
         return result;
97
     }
97
     }
98
 
98
 
99
     Options withDefaultOptions(final Options defaultOptions) {
99
     Options withDefaultOptions(final Options defaultOptions) {
100
-        orientationOptions.mergeWithDefault(defaultOptions.orientationOptions);
101
         topBar.mergeWithDefault(defaultOptions.topBar);
100
         topBar.mergeWithDefault(defaultOptions.topBar);
102
         topTabOptions.mergeWithDefault(defaultOptions.topTabOptions);
101
         topTabOptions.mergeWithDefault(defaultOptions.topTabOptions);
103
         topTabsOptions.mergeWithDefault(defaultOptions.topTabsOptions);
102
         topTabsOptions.mergeWithDefault(defaultOptions.topTabsOptions);
109
         if (!screenBackgroundColor.hasValue()) screenBackgroundColor = defaultOptions.screenBackgroundColor;
108
         if (!screenBackgroundColor.hasValue()) screenBackgroundColor = defaultOptions.screenBackgroundColor;
110
         modal.mergeWithDefault(defaultOptions.modal);
109
         modal.mergeWithDefault(defaultOptions.modal);
111
         statusBar.mergeWithDefault(defaultOptions.statusBar);
110
         statusBar.mergeWithDefault(defaultOptions.statusBar);
111
+        layout.mergeWithDefault(defaultOptions.layout);
112
         return this;
112
         return this;
113
     }
113
     }
114
 
114
 

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

48
         }
48
         }
49
     }
49
     }
50
 
50
 
51
-    public void mergeWith(OrientationOptions other) {
52
-        if (other.hasValue()) orientations = other.orientations;
53
-    }
54
-
55
     public boolean hasValue() {
51
     public boolean hasValue() {
56
         return !orientations.isEmpty();
52
         return !orientations.isEmpty();
57
     }
53
     }
58
 
54
 
59
-    public void mergeWithDefault(OrientationOptions defaultOptions) {
60
-        if (!hasValue()) orientations = defaultOptions.orientations;
61
-    }
62
-
63
     @Override
55
     @Override
64
     public String toString() {
56
     public String toString() {
65
         return hasValue() ? Arrays.toString(orientations.toArray(new Orientation[0])) : Orientation.Default.toString();
57
         return hasValue() ? Arrays.toString(orientations.toArray(new Orientation[0])) : Orientation.Default.toString();

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/presentation/OptionsPresenter.java View File

23
     }
23
     }
24
 
24
 
25
     public void present(View view, Options options) {
25
     public void present(View view, Options options) {
26
-        applyOrientation(options.orientationOptions);
26
+        applyOrientation(options.layout.orientation);
27
         applyViewOptions(view, options);
27
         applyViewOptions(view, options);
28
         applyStatusBarOptions(view, options.statusBar);
28
         applyStatusBarOptions(view, options.statusBar);
29
     }
29
     }

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/presentation/StackOptionsPresenter.java View File

33
     }
33
     }
34
 
34
 
35
     public void applyChildOptions(Options options, Component child) {
35
     public void applyChildOptions(Options options, Component child) {
36
-        applyOrientation(options.orientationOptions);
36
+        applyOrientation(options.layout.orientation);
37
         applyButtons(options.topBar.leftButtons, options.topBar.rightButtons);
37
         applyButtons(options.topBar.leftButtons, options.topBar.rightButtons);
38
         applyTopBarOptions(options.topBar, options.animations, child, options);
38
         applyTopBarOptions(options.topBar, options.animations, child, options);
39
         applyTopTabsOptions(options.topTabsOptions);
39
         applyTopTabsOptions(options.topTabsOptions);
119
     }
119
     }
120
 
120
 
121
     public void mergeChildOptions(Options options, Component child) {
121
     public void mergeChildOptions(Options options, Component child) {
122
-        mergeOrientation(options.orientationOptions);
122
+        mergeOrientation(options.layout.orientation);
123
         mergeButtons(options.topBar.leftButtons, options.topBar.rightButtons);
123
         mergeButtons(options.topBar.leftButtons, options.topBar.rightButtons);
124
         mergeTopBarOptions(options.topBar, options.animations, child);
124
         mergeTopBarOptions(options.topBar, options.animations, child);
125
         mergeTopTabsOptions(options.topTabsOptions);
125
         mergeTopTabsOptions(options.topTabsOptions);

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/views/StackLayout.java View File

34
     }
34
     }
35
 
35
 
36
     public void applyChildOptions(Options options) {
36
     public void applyChildOptions(Options options) {
37
-        optionsPresenter.applyOrientation(options.orientationOptions);
37
+        optionsPresenter.applyOrientation(options.layout.orientation);
38
     }
38
     }
39
 
39
 
40
     public void applyChildOptions(Options options, Component child) {
40
     public void applyChildOptions(Options options, Component child) {

+ 2
- 2
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/OptionsMergingTest.java View File

55
         verify(uut, times(0)).applyOrientation(any());
55
         verify(uut, times(0)).applyOrientation(any());
56
 
56
 
57
         JSONObject orientation = new JSONObject().put("orientation", "landscape");
57
         JSONObject orientation = new JSONObject().put("orientation", "landscape");
58
-        options.orientationOptions = OrientationOptions.parse(orientation);
58
+        options.layout.orientation = OrientationOptions.parse(orientation);
59
         uut.mergeChildOptions(options, child);
59
         uut.mergeChildOptions(options, child);
60
-        verify(uut, times(1)).applyOrientation(options.orientationOptions);
60
+        verify(uut, times(1)).applyOrientation(options.layout.orientation);
61
     }
61
     }
62
 
62
 
63
     @Test
63
     @Test

+ 3
- 1
playground/src/screens/ModalScreen.js View File

16
         drawBehind: true,
16
         drawBehind: true,
17
         backgroundColor: 'transparent'
17
         backgroundColor: 'transparent'
18
       },
18
       },
19
-      orientation: ['portrait']
19
+      layout: {
20
+        orientation: ['portrait']
21
+      }
20
     };
22
     };
21
   }
23
   }
22
 
24
 

+ 3
- 1
playground/src/screens/OrientationDetectScreen.js View File

13
     this.detectHorizontal = this.detectHorizontal.bind(this);
13
     this.detectHorizontal = this.detectHorizontal.bind(this);
14
     this.state = { horizontal: false };
14
     this.state = { horizontal: false };
15
     Navigation.mergeOptions(this.props.componentId, {
15
     Navigation.mergeOptions(this.props.componentId, {
16
-      orientation: props.orientation
16
+      layout: {
17
+        orientation: props.orientation
18
+      }
17
     });
19
     });
18
   }
20
   }
19
 
21