Browse Source

Implement topBar.height and title.height

This commit add support for changing the TopBar height as well as changing
the height of custom component used as background
Guy Carmeli 6 years ago
parent
commit
4df7426fd1

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

@@ -7,7 +7,9 @@ import com.reactnativenavigation.parse.params.Color;
7 7
 import com.reactnativenavigation.parse.params.Fraction;
8 8
 import com.reactnativenavigation.parse.params.NullColor;
9 9
 import com.reactnativenavigation.parse.params.NullFraction;
10
+import com.reactnativenavigation.parse.params.NullNumber;
10 11
 import com.reactnativenavigation.parse.params.NullText;
12
+import com.reactnativenavigation.parse.params.Number;
11 13
 import com.reactnativenavigation.parse.params.Text;
12 14
 import com.reactnativenavigation.parse.parsers.ColorParser;
13 15
 import com.reactnativenavigation.parse.parsers.FractionParser;
@@ -38,6 +40,7 @@ public class TitleOptions {
38 40
     public Alignment alignment = Alignment.Default;
39 41
     @Nullable public Typeface fontFamily;
40 42
     public Component component = new Component();
43
+    public Number height = new NullNumber();
41 44
 
42 45
     void mergeWith(final TitleOptions other) {
43 46
         if (other.text.hasValue()) text = other.text;
@@ -46,6 +49,7 @@ public class TitleOptions {
46 49
         if (other.fontFamily != null) fontFamily = other.fontFamily;
47 50
         if (other.alignment != Alignment.Default) alignment = other.alignment;
48 51
         if (other.component.hasValue()) component = other.component;
52
+        if (other.height.hasValue()) height = other.height;
49 53
     }
50 54
 
51 55
     void mergeWithDefault(TitleOptions defaultOptions) {
@@ -55,5 +59,6 @@ public class TitleOptions {
55 59
         if (fontFamily == null) fontFamily = defaultOptions.fontFamily;
56 60
         if (alignment == Alignment.Default) alignment = defaultOptions.alignment;
57 61
         component.mergeWithDefault(defaultOptions.component);
62
+        if (!height.hasValue()) height = defaultOptions.height;
58 63
     }
59 64
 }

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

@@ -8,9 +8,12 @@ import com.reactnativenavigation.BuildConfig;
8 8
 import com.reactnativenavigation.parse.params.Bool;
9 9
 import com.reactnativenavigation.parse.params.Button;
10 10
 import com.reactnativenavigation.parse.params.NullBool;
11
+import com.reactnativenavigation.parse.params.NullNumber;
11 12
 import com.reactnativenavigation.parse.params.NullText;
13
+import com.reactnativenavigation.parse.params.Number;
12 14
 import com.reactnativenavigation.parse.params.Text;
13 15
 import com.reactnativenavigation.parse.parsers.BoolParser;
16
+import com.reactnativenavigation.parse.parsers.NumberParser;
14 17
 import com.reactnativenavigation.parse.parsers.TextParser;
15 18
 import com.reactnativenavigation.utils.TypefaceLoader;
16 19
 
@@ -34,6 +37,7 @@ public class TopBarOptions {
34 37
         options.rightButtons = Button.parseJsonArray(json.optJSONArray("rightButtons"), typefaceLoader);
35 38
         options.leftButtons = Button.parseJsonArray(json.optJSONArray("leftButtons"), typefaceLoader);
36 39
         options.testId = TextParser.parse(json, "testID");
40
+        options.height = NumberParser.parse(json, "height");
37 41
 
38 42
         options.validate();
39 43
         return options;
@@ -47,6 +51,7 @@ public class TopBarOptions {
47 51
     public Bool animate = new NullBool();
48 52
     public Bool hideOnScroll = new NullBool();
49 53
     public Bool drawBehind = new NullBool();
54
+    public Number height = new NullNumber();
50 55
     @Nullable public ArrayList<Button> leftButtons;
51 56
     @Nullable public ArrayList<Button> rightButtons;
52 57
 
@@ -61,6 +66,7 @@ public class TopBarOptions {
61 66
         if (other.drawBehind.hasValue()) drawBehind = other.drawBehind;
62 67
         if (other.leftButtons != null) leftButtons = other.leftButtons;
63 68
         if (other.rightButtons != null) rightButtons = other.rightButtons;
69
+        if (other.height.hasValue()) height = other.height;
64 70
         validate();
65 71
     }
66 72
 
@@ -75,6 +81,7 @@ public class TopBarOptions {
75 81
         if (leftButtons == null) leftButtons = defaultOptions.leftButtons;
76 82
         if (rightButtons == null) rightButtons = defaultOptions.rightButtons;
77 83
         if (!testId.hasValue()) testId = defaultOptions.testId;
84
+        if (!height.hasValue()) height = defaultOptions.height;
78 85
         validate();
79 86
     }
80 87
 

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

@@ -2,6 +2,7 @@ package com.reactnativenavigation.presentation;
2 2
 
3 3
 import android.app.Activity;
4 4
 import android.graphics.Color;
5
+import android.view.ViewGroup.LayoutParams;
5 6
 
6 7
 import com.reactnativenavigation.parse.AnimationsOptions;
7 8
 import com.reactnativenavigation.parse.Options;
@@ -44,6 +45,9 @@ public class StackOptionsPresenter {
44 45
     }
45 46
 
46 47
     private void applyTopBarOptions(TopBarOptions options, AnimationsOptions animationOptions, Component component, Options componentOptions) {
48
+        topBar.setHeight(options.height.get(LayoutParams.WRAP_CONTENT));
49
+
50
+        topBar.setTitleHeight(options.title.height.get(LayoutParams.WRAP_CONTENT));
47 51
         topBar.setTitle(options.title.text.get(""));
48 52
         if (options.title.component.hasValue()) topBar.setTitleComponent(options.title.component);
49 53
         topBar.setTitleFontSize(options.title.fontSize.get(defaultTitleFontSize));

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/utils/ViewUtils.java View File

@@ -74,4 +74,8 @@ public class ViewUtils {
74 74
         }
75 75
         return false;
76 76
     }
77
+
78
+    public static int getPreferredHeight(View view) {
79
+        return view.getLayoutParams().height < 0 ? view.getHeight() : view.getLayoutParams().height;
80
+    }
77 81
 }

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

@@ -9,6 +9,7 @@ import android.widget.RelativeLayout;
9 9
 
10 10
 import com.reactnativenavigation.interfaces.ScrollEventListener;
11 11
 import com.reactnativenavigation.parse.Options;
12
+import com.reactnativenavigation.utils.ViewUtils;
12 13
 import com.reactnativenavigation.viewcontrollers.IReactView;
13 14
 import com.reactnativenavigation.viewcontrollers.TopBarButtonController;
14 15
 import com.reactnativenavigation.views.topbar.TopBar;
@@ -87,7 +88,7 @@ public class ComponentLayout extends FrameLayout implements ReactComponent, TopB
87 88
     public void drawBelowTopBar(TopBar topBar) {
88 89
         if (getParent() instanceof RelativeLayout) {
89 90
             RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
90
-            layoutParams.topMargin = topBar.getHeight();
91
+            layoutParams.topMargin = ViewUtils.getPreferredHeight(topBar);
91 92
             setLayoutParams(layoutParams);
92 93
         }
93 94
     }

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

@@ -7,6 +7,7 @@ import android.graphics.Typeface;
7 7
 import android.support.v7.widget.Toolbar;
8 8
 import android.util.Log;
9 9
 import android.view.Gravity;
10
+import android.view.ViewGroup;
10 11
 import android.widget.TextView;
11 12
 
12 13
 import com.reactnativenavigation.parse.Alignment;
@@ -213,4 +214,10 @@ public class TitleBar extends Toolbar {
213 214
         }
214 215
         return lp;
215 216
     }
217
+
218
+    public void setHeight(int height) {
219
+        ViewGroup.LayoutParams lp = getLayoutParams();
220
+        lp.height = (int) UiUtils.dpToPx(getContext(), height);
221
+        setLayoutParams(lp);
222
+    }
216 223
 }

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

@@ -10,6 +10,7 @@ import android.support.design.widget.AppBarLayout;
10 10
 import android.support.v4.view.ViewPager;
11 11
 import android.support.v7.widget.Toolbar;
12 12
 import android.view.View;
13
+import android.view.ViewGroup;
13 14
 import android.widget.RelativeLayout;
14 15
 import android.widget.TextView;
15 16
 
@@ -23,6 +24,8 @@ import com.reactnativenavigation.parse.params.Button;
23 24
 import com.reactnativenavigation.parse.params.Color;
24 25
 import com.reactnativenavigation.parse.params.Number;
25 26
 import com.reactnativenavigation.utils.CompatUtils;
27
+import com.reactnativenavigation.utils.UiUtils;
28
+import com.reactnativenavigation.utils.ViewUtils;
26 29
 import com.reactnativenavigation.viewcontrollers.ReactViewCreator;
27 30
 import com.reactnativenavigation.viewcontrollers.TopBarButtonController;
28 31
 import com.reactnativenavigation.viewcontrollers.topbar.TopBarBackgroundViewController;
@@ -70,6 +73,16 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
70 73
         return new TitleBar(context, buttonCreator, reactViewCreator, onClickListener);
71 74
     }
72 75
 
76
+    public void setHeight(int height) {
77
+        ViewGroup.LayoutParams lp = getLayoutParams();
78
+        lp.height = (int) UiUtils.dpToPx(getContext(), height);
79
+        setLayoutParams(lp);
80
+    }
81
+
82
+    public void setTitleHeight(int height) {
83
+        titleBar.setHeight(height);
84
+    }
85
+
73 86
     public void setTitle(String title) {
74 87
         titleBar.setTitle(title);
75 88
     }
@@ -125,7 +138,7 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
125 138
     public void setBackgroundComponent(Component component) {
126 139
         if (component.hasValue()) {
127 140
             topBarBackgroundViewController.setComponent(component);
128
-            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(MATCH_PARENT, getHeight());
141
+            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(MATCH_PARENT, ViewUtils.getPreferredHeight(this));
129 142
             root.addView(topBarBackgroundViewController.getView(), 0, lp);
130 143
         }
131 144
     }
@@ -213,7 +226,7 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
213 226
         if (visibility == View.GONE) {
214 227
             this.parentView.removeView(this);
215 228
         } else if (visibility == View.VISIBLE && this.getParent() == null) {
216
-            this.parentView.addView(this, MATCH_PARENT, WRAP_CONTENT);
229
+            this.parentView.addView(this);
217 230
         }
218 231
     }
219 232