Przeglądaj źródła

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 6 lat temu
rodzic
commit
2f5185bdde

+ 3
- 2
docs/docs/styling.md Wyświetl plik

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

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/TopBarOptions.java Wyświetl plik

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

+ 5
- 0
lib/android/app/src/main/java/com/reactnativenavigation/presentation/StackOptionsPresenter.java Wyświetl plik

47
 
47
 
48
     private void applyTopBarOptions(TopBarOptions options, AnimationsOptions animationOptions, Component component, Options componentOptions) {
48
     private void applyTopBarOptions(TopBarOptions options, AnimationsOptions animationOptions, Component component, Options componentOptions) {
49
         topBar.setHeight(options.height.get(LayoutParams.WRAP_CONTENT));
49
         topBar.setHeight(options.height.get(LayoutParams.WRAP_CONTENT));
50
+        topBar.setElevation(options.elevation);
50
 
51
 
51
         topBar.setTitleHeight(options.title.height.get(LayoutParams.WRAP_CONTENT));
52
         topBar.setTitleHeight(options.title.height.get(LayoutParams.WRAP_CONTENT));
52
         topBar.setTitle(options.title.text.get(""));
53
         topBar.setTitle(options.title.text.get(""));
141
     }
142
     }
142
 
143
 
143
     private void mergeTopBarOptions(TopBarOptions options, AnimationsOptions animationsOptions, Component component) {
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
         if (options.title.text.hasValue()) topBar.setTitle(options.title.text.get());
149
         if (options.title.text.hasValue()) topBar.setTitle(options.title.text.get());
145
         if (options.title.component.hasValue()) topBar.setTitleComponent(options.title.component);
150
         if (options.title.component.hasValue()) topBar.setTitleComponent(options.title.component);
146
         if (options.title.color.hasValue()) topBar.setTitleTextColor(options.title.color.get());
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 Wyświetl plik

3
 import android.annotation.SuppressLint;
3
 import android.annotation.SuppressLint;
4
 import android.content.Context;
4
 import android.content.Context;
5
 import android.graphics.Typeface;
5
 import android.graphics.Typeface;
6
+import android.os.Build;
6
 import android.support.annotation.ColorInt;
7
 import android.support.annotation.ColorInt;
7
 import android.support.annotation.NonNull;
8
 import android.support.annotation.NonNull;
8
 import android.support.annotation.RestrictTo;
9
 import android.support.annotation.RestrictTo;
27
 import com.reactnativenavigation.parse.Component;
28
 import com.reactnativenavigation.parse.Component;
28
 import com.reactnativenavigation.parse.params.Button;
29
 import com.reactnativenavigation.parse.params.Button;
29
 import com.reactnativenavigation.parse.params.Color;
30
 import com.reactnativenavigation.parse.params.Color;
31
+import com.reactnativenavigation.parse.params.Fraction;
30
 import com.reactnativenavigation.parse.params.Number;
32
 import com.reactnativenavigation.parse.params.Number;
31
 import com.reactnativenavigation.utils.CompatUtils;
33
 import com.reactnativenavigation.utils.CompatUtils;
32
 import com.reactnativenavigation.utils.UiUtils;
34
 import com.reactnativenavigation.utils.UiUtils;
216
     }
218
     }
217
 
219
 
218
     public void setBackgroundColor(Color color) {
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
     public Toolbar getTitleBar() {
233
     public Toolbar getTitleBar() {

+ 20
- 6
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/OptionsApplyingTest.java Wyświetl plik

1
 package com.reactnativenavigation.viewcontrollers;
1
 package com.reactnativenavigation.viewcontrollers;
2
 
2
 
3
 import android.app.Activity;
3
 import android.app.Activity;
4
+import android.content.Context;
4
 import android.graphics.Color;
5
 import android.graphics.Color;
5
 import android.graphics.drawable.ColorDrawable;
6
 import android.graphics.drawable.ColorDrawable;
6
 import android.view.View;
7
 import android.view.View;
20
 import com.reactnativenavigation.parse.params.Fraction;
21
 import com.reactnativenavigation.parse.params.Fraction;
21
 import com.reactnativenavigation.parse.params.Text;
22
 import com.reactnativenavigation.parse.params.Text;
22
 import com.reactnativenavigation.utils.CommandListenerAdapter;
23
 import com.reactnativenavigation.utils.CommandListenerAdapter;
23
-import com.reactnativenavigation.utils.ViewUtils;
24
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarBackgroundViewController;
24
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarBackgroundViewController;
25
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
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
 import org.json.JSONObject;
30
 import org.json.JSONObject;
29
 import org.junit.Test;
31
 import org.junit.Test;
31
 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
33
 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
32
 import static android.widget.RelativeLayout.BELOW;
34
 import static android.widget.RelativeLayout.BELOW;
33
 import static org.assertj.core.api.Java6Assertions.assertThat;
35
 import static org.assertj.core.api.Java6Assertions.assertThat;
36
+import static org.mockito.ArgumentMatchers.any;
34
 import static org.mockito.Mockito.spy;
37
 import static org.mockito.Mockito.spy;
38
+import static org.mockito.Mockito.times;
39
+import static org.mockito.Mockito.verify;
35
 
40
 
36
 public class OptionsApplyingTest extends BaseTest {
41
 public class OptionsApplyingTest extends BaseTest {
37
     private Activity activity;
42
     private Activity activity;
39
     private ComponentViewController uut;
44
     private ComponentViewController uut;
40
     private IReactView view;
45
     private IReactView view;
41
     private Options initialNavigationOptions;
46
     private Options initialNavigationOptions;
47
+    private TopBarController topBarController;
48
+    private TopBar topBar;
42
 
49
 
43
     @Override
50
     @Override
44
     public void beforeEach() {
51
     public void beforeEach() {
54
                 (activity1, componentId, componentName) -> view,
61
                 (activity1, componentId, componentName) -> view,
55
                 initialNavigationOptions
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
         stackController = new StackControllerBuilder(activity)
71
         stackController = new StackControllerBuilder(activity)
58
                 .setTopBarButtonCreator(new TopBarButtonCreatorMock())
72
                 .setTopBarButtonCreator(new TopBarButtonCreatorMock())
59
                 .setTitleBarReactViewCreator(new TitleBarReactViewCreatorMock())
73
                 .setTitleBarReactViewCreator(new TitleBarReactViewCreatorMock())
60
                 .setTopBarBackgroundViewController(new TopBarBackgroundViewController(activity, new TopBarBackgroundViewCreatorMock()))
74
                 .setTopBarBackgroundViewController(new TopBarBackgroundViewController(activity, new TopBarBackgroundViewCreatorMock()))
61
-                .setTopBarController(new TopBarController())
75
+                .setTopBarController(topBarController)
62
                 .setId("stack")
76
                 .setId("stack")
63
                 .setInitialOptions(new Options())
77
                 .setInitialOptions(new Options())
64
                 .createStackController();
78
                 .createStackController();
128
         opts.topBar.background.color = new com.reactnativenavigation.parse.params.Color(Color.RED);
142
         opts.topBar.background.color = new com.reactnativenavigation.parse.params.Color(Color.RED);
129
         uut.mergeOptions(opts);
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
     @Test
148
     @Test
205
 
219
 
206
     @Test
220
     @Test
207
     public void appliesTopBarComponent() throws Exception {
221
     public void appliesTopBarComponent() throws Exception {
222
+        disablePushAnimation(uut);
208
         JSONObject json = new JSONObject();
223
         JSONObject json = new JSONObject();
209
         json.put("component", new JSONObject().put("name","someComponent").put("componentId", "id"));
224
         json.put("component", new JSONObject().put("name","someComponent").put("componentId", "id"));
210
         uut.options.topBar.background = TopBarBackgroundOptions.parse(json);
225
         uut.options.topBar.background = TopBarBackgroundOptions.parse(json);
212
         stackController.push(uut, new CommandListenerAdapter());
227
         stackController.push(uut, new CommandListenerAdapter());
213
         uut.onViewAppeared();
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
     @Test
233
     @Test