Parcourir la source

Implement TopBar.elevation option

This commit also changes the way TopBar background color is applied.
Instead of applying it to the TitleBar, it's applied to the entire TopBar layout
Guy Carmeli il y a 6 ans
Parent
révision
2f5185bdde

+ 3
- 2
docs/docs/styling.md Voir le fichier

@@ -187,11 +187,12 @@ Navigation.mergeOptions(this.props.componentId, {
187 187
     backgroundColor: 'red'
188 188
   },
189 189
   topBar: {
190
-    height: 70, // TopBar height in dp.
190
+    height: 70, // TopBar height in dp
191 191
     borderColor: 'red',
192 192
     borderHeight: 1.3,
193
+    elevation: 1.5, // TopBar elevation in dp
193 194
     title: {
194
-      height: 70 // TitleBar height in dp.
195
+      height: 70 // TitleBar height in dp
195 196
     }
196 197
   },
197 198
   bottomTabs: {

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/TopBarOptions.java Voir le fichier

@@ -46,6 +46,7 @@ public class TopBarOptions {
46 46
         options.height = NumberParser.parse(json, "height");
47 47
         options.borderColor = ColorParser.parse(json, "borderColor");
48 48
         options.borderHeight = FractionParser.parse(json, "borderHeight");
49
+        options.elevation = FractionParser.parse(json, "elevation");
49 50
 
50 51
         options.validate();
51 52
         return options;
@@ -60,6 +61,7 @@ public class TopBarOptions {
60 61
     public Bool hideOnScroll = new NullBool();
61 62
     public Bool drawBehind = new NullBool();
62 63
     public Number height = new NullNumber();
64
+    public Fraction elevation = new NullFraction();
63 65
     public Fraction borderHeight = new NullFraction();
64 66
     public Color borderColor = new NullColor();
65 67
     @Nullable public ArrayList<Button> leftButtons;
@@ -79,6 +81,7 @@ public class TopBarOptions {
79 81
         if (other.height.hasValue()) height = other.height;
80 82
         if (other.borderHeight.hasValue()) borderHeight = other.borderHeight;
81 83
         if (other.borderColor.hasValue()) borderColor = other.borderColor;
84
+        if (other.elevation.hasValue()) elevation = other.elevation;
82 85
         validate();
83 86
     }
84 87
 
@@ -96,6 +99,7 @@ public class TopBarOptions {
96 99
         if (!height.hasValue()) height = defaultOptions.height;
97 100
         if (!borderHeight.hasValue()) borderHeight = defaultOptions.borderHeight;
98 101
         if (!borderColor.hasValue()) borderColor = defaultOptions.borderColor;
102
+        if (!elevation.hasValue()) elevation = defaultOptions.elevation;
99 103
         validate();
100 104
     }
101 105
 

+ 5
- 0
lib/android/app/src/main/java/com/reactnativenavigation/presentation/StackOptionsPresenter.java Voir le fichier

@@ -47,6 +47,7 @@ public class StackOptionsPresenter {
47 47
 
48 48
     private void applyTopBarOptions(TopBarOptions options, AnimationsOptions animationOptions, Component component, Options componentOptions) {
49 49
         topBar.setHeight(options.height.get(LayoutParams.WRAP_CONTENT));
50
+        topBar.setElevation(options.elevation);
50 51
 
51 52
         topBar.setTitleHeight(options.title.height.get(LayoutParams.WRAP_CONTENT));
52 53
         topBar.setTitle(options.title.text.get(""));
@@ -141,6 +142,10 @@ public class StackOptionsPresenter {
141 142
     }
142 143
 
143 144
     private void mergeTopBarOptions(TopBarOptions options, AnimationsOptions animationsOptions, Component component) {
145
+        if (options.height.hasValue()) topBar.setHeight(options.height.get());
146
+        if (options.elevation.hasValue()) topBar.setElevation(options.elevation);
147
+
148
+        if (options.title.height.hasValue()) topBar.setTitleHeight(options.title.height.get());
144 149
         if (options.title.text.hasValue()) topBar.setTitle(options.title.text.get());
145 150
         if (options.title.component.hasValue()) topBar.setTitleComponent(options.title.component);
146 151
         if (options.title.color.hasValue()) topBar.setTitleTextColor(options.title.color.get());

+ 12
- 1
lib/android/app/src/main/java/com/reactnativenavigation/views/topbar/TopBar.java Voir le fichier

@@ -3,6 +3,7 @@ package com.reactnativenavigation.views.topbar;
3 3
 import android.annotation.SuppressLint;
4 4
 import android.content.Context;
5 5
 import android.graphics.Typeface;
6
+import android.os.Build;
6 7
 import android.support.annotation.ColorInt;
7 8
 import android.support.annotation.NonNull;
8 9
 import android.support.annotation.RestrictTo;
@@ -27,6 +28,7 @@ import com.reactnativenavigation.parse.AnimationOptions;
27 28
 import com.reactnativenavigation.parse.Component;
28 29
 import com.reactnativenavigation.parse.params.Button;
29 30
 import com.reactnativenavigation.parse.params.Color;
31
+import com.reactnativenavigation.parse.params.Fraction;
30 32
 import com.reactnativenavigation.parse.params.Number;
31 33
 import com.reactnativenavigation.utils.CompatUtils;
32 34
 import com.reactnativenavigation.utils.UiUtils;
@@ -216,7 +218,16 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
216 218
     }
217 219
 
218 220
     public void setBackgroundColor(Color color) {
219
-        titleBar.setBackgroundColor(color);
221
+        if (!color.hasValue()) return;
222
+        setBackgroundColor(color.get());
223
+    }
224
+
225
+    public void setElevation(Fraction elevation) {
226
+        if (elevation.hasValue() &&
227
+            Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
228
+            getElevation() != elevation.get().floatValue()) {
229
+            setElevation(UiUtils.dpToPx(getContext(), elevation.get().floatValue()));
230
+        }
220 231
     }
221 232
 
222 233
     public Toolbar getTitleBar() {

+ 20
- 6
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/OptionsApplyingTest.java Voir le fichier

@@ -1,6 +1,7 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4
+import android.content.Context;
4 5
 import android.graphics.Color;
5 6
 import android.graphics.drawable.ColorDrawable;
6 7
 import android.view.View;
@@ -20,10 +21,11 @@ import com.reactnativenavigation.parse.params.Bool;
20 21
 import com.reactnativenavigation.parse.params.Fraction;
21 22
 import com.reactnativenavigation.parse.params.Text;
22 23
 import com.reactnativenavigation.utils.CommandListenerAdapter;
23
-import com.reactnativenavigation.utils.ViewUtils;
24 24
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarBackgroundViewController;
25 25
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
26
-import com.reactnativenavigation.views.topbar.TopBarBackgroundView;
26
+import com.reactnativenavigation.views.StackLayout;
27
+import com.reactnativenavigation.views.titlebar.TitleBarReactViewCreator;
28
+import com.reactnativenavigation.views.topbar.TopBar;
27 29
 
28 30
 import org.json.JSONObject;
29 31
 import org.junit.Test;
@@ -31,7 +33,10 @@ import org.junit.Test;
31 33
 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
32 34
 import static android.widget.RelativeLayout.BELOW;
33 35
 import static org.assertj.core.api.Java6Assertions.assertThat;
36
+import static org.mockito.ArgumentMatchers.any;
34 37
 import static org.mockito.Mockito.spy;
38
+import static org.mockito.Mockito.times;
39
+import static org.mockito.Mockito.verify;
35 40
 
36 41
 public class OptionsApplyingTest extends BaseTest {
37 42
     private Activity activity;
@@ -39,6 +44,8 @@ public class OptionsApplyingTest extends BaseTest {
39 44
     private ComponentViewController uut;
40 45
     private IReactView view;
41 46
     private Options initialNavigationOptions;
47
+    private TopBarController topBarController;
48
+    private TopBar topBar;
42 49
 
43 50
     @Override
44 51
     public void beforeEach() {
@@ -54,11 +61,18 @@ public class OptionsApplyingTest extends BaseTest {
54 61
                 (activity1, componentId, componentName) -> view,
55 62
                 initialNavigationOptions
56 63
         );
64
+        topBarController = new TopBarController() {
65
+            @Override
66
+            protected TopBar createTopBar(Context context, ReactViewCreator buttonCreator, TitleBarReactViewCreator titleBarReactViewCreator, TopBarBackgroundViewController topBarBackgroundViewController, TopBarButtonController.OnClickListener topBarButtonClickListener, StackLayout stackLayout) {
67
+                topBar = spy(super.createTopBar(context, buttonCreator, titleBarReactViewCreator, topBarBackgroundViewController, topBarButtonClickListener, stackLayout));
68
+                return topBar;
69
+            }
70
+        };
57 71
         stackController = new StackControllerBuilder(activity)
58 72
                 .setTopBarButtonCreator(new TopBarButtonCreatorMock())
59 73
                 .setTitleBarReactViewCreator(new TitleBarReactViewCreatorMock())
60 74
                 .setTopBarBackgroundViewController(new TopBarBackgroundViewController(activity, new TopBarBackgroundViewCreatorMock()))
61
-                .setTopBarController(new TopBarController())
75
+                .setTopBarController(topBarController)
62 76
                 .setId("stack")
63 77
                 .setInitialOptions(new Options())
64 78
                 .createStackController();
@@ -128,7 +142,7 @@ public class OptionsApplyingTest extends BaseTest {
128 142
         opts.topBar.background.color = new com.reactnativenavigation.parse.params.Color(Color.RED);
129 143
         uut.mergeOptions(opts);
130 144
 
131
-        assertThat(((ColorDrawable) stackController.getTopBar().getTitleBar().getBackground()).getColor()).isEqualTo(Color.RED);
145
+        assertThat(((ColorDrawable) stackController.getTopBar().getBackground()).getColor()).isEqualTo(Color.RED);
132 146
     }
133 147
 
134 148
     @Test
@@ -205,6 +219,7 @@ public class OptionsApplyingTest extends BaseTest {
205 219
 
206 220
     @Test
207 221
     public void appliesTopBarComponent() throws Exception {
222
+        disablePushAnimation(uut);
208 223
         JSONObject json = new JSONObject();
209 224
         json.put("component", new JSONObject().put("name","someComponent").put("componentId", "id"));
210 225
         uut.options.topBar.background = TopBarBackgroundOptions.parse(json);
@@ -212,8 +227,7 @@ public class OptionsApplyingTest extends BaseTest {
212 227
         stackController.push(uut, new CommandListenerAdapter());
213 228
         uut.onViewAppeared();
214 229
 
215
-        assertThat(((ColorDrawable) stackController.getTopBar().getTitleBar().getBackground()).getColor()).isEqualTo(Color.TRANSPARENT);
216
-        assertThat(ViewUtils.findChildrenByClassRecursive(stackController.getTopBar(), TopBarBackgroundView.class)).isNotNull();
230
+        verify(topBar, times(1)).setBackgroundComponent(any());
217 231
     }
218 232
 
219 233
     @Test