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

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

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

@@ -48,18 +48,10 @@ public class OrientationOptions {
48 48
         }
49 49
     }
50 50
 
51
-    public void mergeWith(OrientationOptions other) {
52
-        if (other.hasValue()) orientations = other.orientations;
53
-    }
54
-
55 51
     public boolean hasValue() {
56 52
         return !orientations.isEmpty();
57 53
     }
58 54
 
59
-    public void mergeWithDefault(OrientationOptions defaultOptions) {
60
-        if (!hasValue()) orientations = defaultOptions.orientations;
61
-    }
62
-
63 55
     @Override
64 56
     public String toString() {
65 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,7 +23,7 @@ public class OptionsPresenter {
23 23
     }
24 24
 
25 25
     public void present(View view, Options options) {
26
-        applyOrientation(options.orientationOptions);
26
+        applyOrientation(options.layout.orientation);
27 27
         applyViewOptions(view, options);
28 28
         applyStatusBarOptions(view, options.statusBar);
29 29
     }

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

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

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

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

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

@@ -55,9 +55,9 @@ public class OptionsMergingTest extends BaseTest {
55 55
         verify(uut, times(0)).applyOrientation(any());
56 56
 
57 57
         JSONObject orientation = new JSONObject().put("orientation", "landscape");
58
-        options.orientationOptions = OrientationOptions.parse(orientation);
58
+        options.layout.orientation = OrientationOptions.parse(orientation);
59 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 63
     @Test

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

@@ -16,7 +16,9 @@ class ModalScreen extends Component {
16 16
         drawBehind: true,
17 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,7 +13,9 @@ class OrientationDetectScreen extends Component {
13 13
     this.detectHorizontal = this.detectHorizontal.bind(this);
14 14
     this.state = { horizontal: false };
15 15
     Navigation.mergeOptions(this.props.componentId, {
16
-      orientation: props.orientation
16
+      layout: {
17
+        orientation: props.orientation
18
+      }
17 19
     });
18 20
   }
19 21