Browse Source

Implement topBar.borderColor and topBar.borderHeight

Guy Carmeli 6 years ago
parent
commit
1b8e762d1d

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

@@ -7,12 +7,18 @@ import android.util.Log;
7 7
 import com.reactnativenavigation.BuildConfig;
8 8
 import com.reactnativenavigation.parse.params.Bool;
9 9
 import com.reactnativenavigation.parse.params.Button;
10
+import com.reactnativenavigation.parse.params.Color;
11
+import com.reactnativenavigation.parse.params.Fraction;
10 12
 import com.reactnativenavigation.parse.params.NullBool;
13
+import com.reactnativenavigation.parse.params.NullColor;
14
+import com.reactnativenavigation.parse.params.NullFraction;
11 15
 import com.reactnativenavigation.parse.params.NullNumber;
12 16
 import com.reactnativenavigation.parse.params.NullText;
13 17
 import com.reactnativenavigation.parse.params.Number;
14 18
 import com.reactnativenavigation.parse.params.Text;
15 19
 import com.reactnativenavigation.parse.parsers.BoolParser;
20
+import com.reactnativenavigation.parse.parsers.ColorParser;
21
+import com.reactnativenavigation.parse.parsers.FractionParser;
16 22
 import com.reactnativenavigation.parse.parsers.NumberParser;
17 23
 import com.reactnativenavigation.parse.parsers.TextParser;
18 24
 import com.reactnativenavigation.utils.TypefaceLoader;
@@ -38,6 +44,8 @@ public class TopBarOptions {
38 44
         options.leftButtons = Button.parseJsonArray(json.optJSONArray("leftButtons"), typefaceLoader);
39 45
         options.testId = TextParser.parse(json, "testID");
40 46
         options.height = NumberParser.parse(json, "height");
47
+        options.borderColor = ColorParser.parse(json, "borderColor");
48
+        options.borderHeight = FractionParser.parse(json, "borderHeight");
41 49
 
42 50
         options.validate();
43 51
         return options;
@@ -52,6 +60,8 @@ public class TopBarOptions {
52 60
     public Bool hideOnScroll = new NullBool();
53 61
     public Bool drawBehind = new NullBool();
54 62
     public Number height = new NullNumber();
63
+    public Fraction borderHeight = new NullFraction();
64
+    public Color borderColor = new NullColor();
55 65
     @Nullable public ArrayList<Button> leftButtons;
56 66
     @Nullable public ArrayList<Button> rightButtons;
57 67
 
@@ -67,6 +77,8 @@ public class TopBarOptions {
67 77
         if (other.leftButtons != null) leftButtons = other.leftButtons;
68 78
         if (other.rightButtons != null) rightButtons = other.rightButtons;
69 79
         if (other.height.hasValue()) height = other.height;
80
+        if (other.borderHeight.hasValue()) borderHeight = other.borderHeight;
81
+        if (other.borderColor.hasValue()) borderColor = other.borderColor;
70 82
         validate();
71 83
     }
72 84
 
@@ -82,6 +94,8 @@ public class TopBarOptions {
82 94
         if (rightButtons == null) rightButtons = defaultOptions.rightButtons;
83 95
         if (!testId.hasValue()) testId = defaultOptions.testId;
84 96
         if (!height.hasValue()) height = defaultOptions.height;
97
+        if (!borderHeight.hasValue()) borderHeight = defaultOptions.borderHeight;
98
+        if (!borderColor.hasValue()) borderColor = defaultOptions.borderColor;
85 99
         validate();
86 100
     }
87 101
 

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/parse/params/Fraction.java View File

@@ -1,7 +1,7 @@
1 1
 package com.reactnativenavigation.parse.params;
2 2
 
3
-public class Fraction extends Param<Float> {
4
-    public Fraction(float value) {
3
+public class Fraction extends Param<Double> {
4
+    public Fraction(double value) {
5 5
         super(value);
6 6
     }
7 7
 }

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/parse/parsers/FractionParser.java View File

@@ -7,6 +7,6 @@ import org.json.JSONObject;
7 7
 
8 8
 public class FractionParser {
9 9
     public static Fraction parse(JSONObject json, String fraction) {
10
-        return json.has(fraction) ? new Fraction(json.optInt(fraction)) : new NullFraction();
10
+        return json.has(fraction) ? new Fraction(json.optDouble(fraction)) : new NullFraction();
11 11
     }
12 12
 }

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

@@ -21,8 +21,9 @@ import java.util.ArrayList;
21 21
 public class StackOptionsPresenter {
22 22
     private static final int DEFAULT_TITLE_COLOR = Color.BLACK;
23 23
     private static final int DEFAULT_SUBTITLE_COLOR = Color.GRAY;
24
-    private final float defaultTitleFontSize;
25
-    private final float defaultSubtitleFontSize;
24
+    private static final int DEFAULT_BORDER_COLOR = Color.BLACK;
25
+    private final double defaultTitleFontSize;
26
+    private final double defaultSubtitleFontSize;
26 27
 
27 28
     private TopBar topBar;
28 29
 
@@ -61,6 +62,9 @@ public class StackOptionsPresenter {
61 62
         topBar.setSubtitleFontFamily(options.subtitle.fontFamily);
62 63
         topBar.setSubtitleAlignment(options.subtitle.alignment);
63 64
 
65
+        topBar.setBorderHeight(options.borderHeight.get(0d));
66
+        topBar.setBorderColor(options.borderColor.get(DEFAULT_BORDER_COLOR));
67
+
64 68
         topBar.setBackgroundColor(options.background.color);
65 69
         topBar.setBackgroundComponent(options.background.component);
66 70
         if (options.testId.hasValue()) topBar.setTestId(options.testId.get());

+ 4
- 4
lib/android/app/src/main/java/com/reactnativenavigation/views/titlebar/TitleBar.java View File

@@ -74,9 +74,9 @@ public class TitleBar extends Toolbar {
74 74
         if (color.hasValue()) setBackgroundColor(color.get());
75 75
     }
76 76
 
77
-    public void setTitleFontSize(float size) {
77
+    public void setTitleFontSize(double size) {
78 78
         TextView titleTextView = findTitleTextView();
79
-        if (titleTextView != null) titleTextView.setTextSize(size);
79
+        if (titleTextView != null) titleTextView.setTextSize((float) size);
80 80
     }
81 81
 
82 82
     public void setTitleTypeface(Typeface typeface) {
@@ -95,9 +95,9 @@ public class TitleBar extends Toolbar {
95 95
         if (subtitleTextView != null) subtitleTextView.setTypeface(typeface);
96 96
     }
97 97
 
98
-    public void setSubtitleFontSize(float size) {
98
+    public void setSubtitleFontSize(double size) {
99 99
         TextView subtitleTextView = findSubtitleTextView();
100
-        if (subtitleTextView != null) subtitleTextView.setTextSize(size);
100
+        if (subtitleTextView != null) subtitleTextView.setTextSize((float) size);
101 101
     }
102 102
 
103 103
     public void setSubtitleAlignment(Alignment alignment) {

+ 30
- 5
lib/android/app/src/main/java/com/reactnativenavigation/views/topbar/TopBar.java View File

@@ -9,11 +9,14 @@ import android.support.annotation.VisibleForTesting;
9 9
 import android.support.design.widget.AppBarLayout;
10 10
 import android.support.v4.view.ViewPager;
11 11
 import android.support.v7.widget.Toolbar;
12
+import android.view.Gravity;
12 13
 import android.view.View;
13 14
 import android.view.ViewGroup;
15
+import android.widget.FrameLayout;
14 16
 import android.widget.RelativeLayout;
15 17
 import android.widget.TextView;
16 18
 
19
+import com.reactnativenavigation.BuildConfig;
17 20
 import com.reactnativenavigation.anim.TopBarAnimator;
18 21
 import com.reactnativenavigation.anim.TopBarCollapseBehavior;
19 22
 import com.reactnativenavigation.interfaces.ScrollEventListener;
@@ -45,9 +48,10 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
45 48
     private final TopBarCollapseBehavior collapsingBehavior;
46 49
     private TopBarAnimator animator;
47 50
     private TopTabs topTabs;
48
-    private RelativeLayout root;
51
+    private FrameLayout root;
49 52
     private StackLayout parentView;
50 53
     private TopBarBackgroundViewController topBarBackgroundViewController;
54
+    private View border;
51 55
 
52 56
     public TopBar(final Context context, ReactViewCreator buttonCreator, TitleBarReactViewCreator titleBarReactViewCreator, TopBarBackgroundViewController topBarBackgroundViewController, TopBarButtonController.OnClickListener onClickListener, StackLayout parentView) {
53 57
         super(context);
@@ -60,13 +64,26 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
60 64
     }
61 65
 
62 66
     private void createLayout(ReactViewCreator buttonCreator, TitleBarReactViewCreator titleBarReactViewCreator, TopBarButtonController.OnClickListener onClickListener) {
67
+        setId(CompatUtils.generateViewId());
63 68
         topTabs = new TopTabs(getContext());
64 69
         titleBar = createTitleBar(getContext(), buttonCreator, titleBarReactViewCreator, onClickListener);
65 70
         titleBar.setId(CompatUtils.generateViewId());
66
-        root = new RelativeLayout(getContext());
71
+        root = new FrameLayout(getContext());
72
+        root.setId(CompatUtils.generateViewId());
67 73
         root.addView(titleBar, MATCH_PARENT, WRAP_CONTENT);
74
+        border = createBorder();
75
+        root.addView(border);
68 76
         addView(root, MATCH_PARENT, WRAP_CONTENT);
69
-        setContentDescription("TopBar");
77
+        if (BuildConfig.DEBUG) setContentDescription("TopBar");
78
+    }
79
+
80
+    private View createBorder() {
81
+        View border = new View(getContext());
82
+        border.setBackgroundColor(android.graphics.Color.TRANSPARENT);
83
+        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(MATCH_PARENT, 0);
84
+        lp.gravity = Gravity.BOTTOM;
85
+        border.setLayoutParams(lp);
86
+        return border;
70 87
     }
71 88
 
72 89
     protected TitleBar createTitleBar(Context context, ReactViewCreator buttonCreator, TitleBarReactViewCreator reactViewCreator, TopBarButtonController.OnClickListener onClickListener) {
@@ -104,7 +121,7 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
104 121
         titleBar.setSubtitleTypeface(fontFamily);
105 122
     }
106 123
 
107
-    public void setSubtitleFontSize(float size) {
124
+    public void setSubtitleFontSize(double size) {
108 125
         titleBar.setSubtitleFontSize(size);
109 126
     }
110 127
 
@@ -120,7 +137,7 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
120 137
         titleBar.setTitleTextColor(color);
121 138
     }
122 139
 
123
-    public void setTitleFontSize(float size) {
140
+    public void setTitleFontSize(double size) {
124 141
         titleBar.setTitleFontSize(size);
125 142
     }
126 143
 
@@ -266,4 +283,12 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
266 283
         setRotationY(0);
267 284
         setRotation(0);
268 285
     }
286
+
287
+    public void setBorderHeight(double height) {
288
+        border.getLayoutParams().height = (int) UiUtils.dpToPx(getContext(), (float) height);
289
+    }
290
+
291
+    public void setBorderColor(int color) {
292
+        border.setBackgroundColor(color);
293
+    }
269 294
 }

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

@@ -25,7 +25,7 @@ import java.util.ArrayList;
25 25
 
26 26
 import static org.mockito.ArgumentMatchers.any;
27 27
 import static org.mockito.ArgumentMatchers.anyBoolean;
28
-import static org.mockito.ArgumentMatchers.anyFloat;
28
+import static org.mockito.ArgumentMatchers.anyDouble;
29 29
 import static org.mockito.ArgumentMatchers.anyInt;
30 30
 import static org.mockito.Mockito.mock;
31 31
 import static org.mockito.Mockito.spy;
@@ -147,7 +147,7 @@ public class OptionsMergingTest extends BaseTest {
147 147
         verify(topBar, times(t)).setTitleComponent(any());
148 148
         verify(topBar, times(t)).setBackgroundColor(any());
149 149
         verify(topBar, times(t)).setTitleTextColor(anyInt());
150
-        verify(topBar, times(t)).setTitleFontSize(anyFloat());
150
+        verify(topBar, times(t)).setTitleFontSize(anyDouble());
151 151
         verify(topBar, times(t)).setTitleTypeface(any());
152 152
         verify(topBar, times(t)).setSubtitleColor(anyInt());
153 153
         verify(topBar, times(t)).setTestId(any());

+ 2
- 0
playground/src/screens/OptionsScreen.js View File

@@ -52,6 +52,8 @@ class OptionsScreen extends Component {
52 52
         _height: TOPBAR_HEIGHT,
53 53
         visible: true,
54 54
         testID: testIDs.TOP_BAR_ELEMENT,
55
+        borderColor: 'red',
56
+        borderHeight: 1,
55 57
         rightButtons: [
56 58
           // {
57 59
           //   id: CUSTOM_BUTTON,