Browse Source

Implement collapsingToolBarExpendedColor and showTitleWhenExpended (#793)

collapsingToolBarExpendedColor - determines the TitleBar color in expended mode
showTitleWhenExpended - defaults to true when expended color is defined.
Guy Carmeli 7 years ago
parent
commit
651bdae0a6

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/params/CollapsingTopBarParams.java View File

@@ -10,7 +10,9 @@ public class CollapsingTopBarParams {
10 10
     public StyleParams.Color scrimColor;
11 11
     public CollapseBehaviour collapseBehaviour;
12 12
     public boolean expendOnTopTabChange;
13
+    public boolean showTitleWhenExpended;
13 14
     public boolean showTitleWhenCollapsed;
15
+    public StyleParams.Color expendedTitleBarColor;
14 16
 
15 17
     public boolean hasBackgroundImage() {
16 18
         return imageUri != null;

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/params/StyleParams.java View File

@@ -35,6 +35,10 @@ public class StyleParams {
35 35
         public String getHexColor() {
36 36
             return String.format("#%06X", (0xFFFFFF & getColor()));
37 37
         }
38
+
39
+        public int getColor(int defaultColor) {
40
+            return hasColor() ? getColor() : defaultColor;
41
+        }
38 42
     }
39 43
 
40 44
     public Color statusBarColor;

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/CollapsingTopBarParamsParser.java View File

@@ -33,7 +33,9 @@ class CollapsingTopBarParamsParser extends Parser {
33 33
         result.reactViewId = params.getString("collapsingToolBarComponent", null);
34 34
         result.expendOnTopTabChange = params.getBoolean("expendCollapsingToolBarOnTopTabChange");
35 35
         result.scrimColor = getColor(params, "collapsingToolBarCollapsedColor", new StyleParams.Color());
36
+        result.expendedTitleBarColor = getColor(params, "collapsingToolBarExpendedColor", new StyleParams.Color());
36 37
         result.showTitleWhenCollapsed = hasReactView;
38
+        result.showTitleWhenExpended = params.getBoolean("showTitleWhenExpended", result.expendedTitleBarColor.hasColor());
37 39
         result.collapseBehaviour = getCollapseBehaviour();
38 40
         return result;
39 41
     }

+ 1
- 11
android/app/src/main/java/com/reactnativenavigation/views/TitleBarBackground.java View File

@@ -1,11 +1,8 @@
1 1
 package com.reactnativenavigation.views;
2 2
 
3
-import android.graphics.drawable.ColorDrawable;
4 3
 import android.graphics.drawable.Drawable;
5 4
 import android.graphics.drawable.TransitionDrawable;
6 5
 
7
-import com.reactnativenavigation.params.StyleParams;
8
-
9 6
 public class TitleBarBackground extends TransitionDrawable {
10 7
     private static final int DURATION = 200;
11 8
 
@@ -13,17 +10,10 @@ public class TitleBarBackground extends TransitionDrawable {
13 10
 
14 11
     private DrawableType displayedDrawable = DrawableType.Translucent;
15 12
 
16
-    TitleBarBackground(Drawable... drawables) {
13
+    public TitleBarBackground(Drawable... drawables) {
17 14
         super(drawables);
18 15
     }
19 16
 
20
-    public TitleBarBackground(StyleParams.Color color) {
21
-        super(new Drawable[] {
22
-                new TranslucentDrawable(),
23
-                new ColorDrawable(color.getColor())
24
-        });
25
-    }
26
-
27 17
     public void showTranslucentBackground() {
28 18
         if (displayedDrawable == DrawableType.Translucent) {
29 19
             return;

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/views/TranslucentDrawable.java View File

@@ -7,9 +7,9 @@ import android.graphics.drawable.PaintDrawable;
7 7
 import android.graphics.drawable.ShapeDrawable;
8 8
 import android.graphics.drawable.shapes.RectShape;
9 9
 
10
-class TranslucentDrawable extends PaintDrawable {
10
+public class TranslucentDrawable extends PaintDrawable {
11 11
 
12
-    TranslucentDrawable() {
12
+    public TranslucentDrawable() {
13 13
         setShape(new RectShape());
14 14
         createShader();
15 15
     }

+ 0
- 11
android/app/src/main/java/com/reactnativenavigation/views/TranslucentTitleBarBackground.java View File

@@ -1,11 +0,0 @@
1
-package com.reactnativenavigation.views;
2
-
3
-import android.graphics.Color;
4
-import android.graphics.drawable.ColorDrawable;
5
-
6
-public class TranslucentTitleBarBackground extends TitleBarBackground {
7
-    public TranslucentTitleBarBackground() {
8
-        super(new TranslucentDrawable(), new ColorDrawable(Color.TRANSPARENT));
9
-        setCrossFadeEnabled(true);
10
-    }
11
-}

+ 30
- 23
android/app/src/main/java/com/reactnativenavigation/views/collapsingToolbar/CollapsingTitleBar.java View File

@@ -1,6 +1,9 @@
1 1
 package com.reactnativenavigation.views.collapsingToolbar;
2 2
 
3 3
 import android.content.Context;
4
+import android.graphics.Color;
5
+import android.graphics.drawable.ColorDrawable;
6
+import android.graphics.drawable.Drawable;
4 7
 import android.view.MotionEvent;
5 8
 import android.view.View;
6 9
 
@@ -9,7 +12,7 @@ import com.reactnativenavigation.params.StyleParams;
9 12
 import com.reactnativenavigation.utils.ViewUtils;
10 13
 import com.reactnativenavigation.views.TitleBar;
11 14
 import com.reactnativenavigation.views.TitleBarBackground;
12
-import com.reactnativenavigation.views.TranslucentTitleBarBackground;
15
+import com.reactnativenavigation.views.TranslucentDrawable;
13 16
 
14 17
 public class CollapsingTitleBar extends TitleBar implements View.OnTouchListener {
15 18
     private CollapsingTextView title;
@@ -25,26 +28,32 @@ public class CollapsingTitleBar extends TitleBar implements View.OnTouchListener
25 28
         this.params = params;
26 29
         addCollapsingTitle();
27 30
         setOnTouchListener(this);
28
-        hideTitle(params);
31
+        setInitialTitleViewVisibility();
29 32
     }
30 33
 
31
-    private void hideTitle(CollapsingTopBarParams params) {
32
-        if (params.showTitleWhenCollapsed) {
33
-            ViewUtils.runOnPreDraw(this, new Runnable() {
34
-                @Override
35
-                public void run() {
36
-                    View titleView = getTitleView();
37
-                    if (titleView != null) {
38
-                        titleView.setAlpha(0);
39
-                    }
34
+    private void setInitialTitleViewVisibility() {
35
+        ViewUtils.runOnPreDraw(this, new Runnable() {
36
+            @Override
37
+            public void run() {
38
+                View titleView = getTitleView();
39
+                if (titleView == null) {
40
+                    return;
40 41
                 }
41
-            });
42
-        }
42
+                if (params.showTitleWhenExpended) {
43
+                    titleView.setAlpha(1);
44
+                } else if (params.showTitleWhenCollapsed) {
45
+                    titleView.setAlpha(0);
46
+                }
47
+            }
48
+        });
49
+
43 50
     }
44 51
 
45 52
     @Override
46 53
     public void hideTitle() {
47
-        super.hideTitle();
54
+        if (!params.showTitleWhenExpended) {
55
+            super.hideTitle();
56
+        }
48 57
         titleBarBackground.showTranslucentBackground();
49 58
     }
50 59
 
@@ -88,18 +97,16 @@ public class CollapsingTitleBar extends TitleBar implements View.OnTouchListener
88 97
 
89 98
     @Override
90 99
     protected void setBackground(StyleParams params) {
91
-        titleBarBackground = createBackground(params);
100
+        titleBarBackground = createBackground(params,
101
+                params.collapsingTopBarParams.expendedTitleBarColor,
102
+                params.collapsingTopBarParams.scrimColor);
92 103
         setBackground(titleBarBackground);
93 104
     }
94 105
 
95
-    private TitleBarBackground createBackground(StyleParams params) {
96
-        return hasTranslucentAndSolidBackground(params) ?
97
-            new TitleBarBackground(params.collapsingTopBarParams.scrimColor) :
98
-            new TranslucentTitleBarBackground();
99
-    }
100
-
101
-    private boolean hasTranslucentAndSolidBackground(StyleParams params) {
102
-        return params.topBarTranslucent && params.collapsingTopBarParams.scrimColor.hasColor();
106
+    private TitleBarBackground createBackground(StyleParams styleParams, StyleParams.Color expendedColor, StyleParams.Color collapsedColor) {
107
+        final Drawable expendedDrawable = styleParams.topBarTranslucent ? new TranslucentDrawable() : new ColorDrawable(expendedColor.getColor(Color.TRANSPARENT));
108
+        final Drawable collapsedDrawable = new ColorDrawable(collapsedColor.getColor(Color.TRANSPARENT));
109
+        return new TitleBarBackground(expendedDrawable, collapsedDrawable);
103 110
     }
104 111
 
105 112
     public void collapse(CollapseAmount amount) {

+ 2
- 0
src/deprecated/platformSpecificDeprecated.android.js View File

@@ -130,6 +130,8 @@ function convertStyleParams(originalStyleObject) {
130 130
     collapsingToolBarComponent: originalStyleObject.collapsingToolBarComponent,
131 131
     collapsingToolBarComponentHeight: originalStyleObject.collapsingToolBarComponentHeight,
132 132
     collapsingToolBarCollapsedColor: processColor(originalStyleObject.collapsingToolBarCollapsedColor),
133
+    collapsingToolBarExpendedColor: processColor(originalStyleObject.collapsingToolBarExpendedColor),
134
+    showTitleWhenExpended: originalStyleObject.showTitleWhenExpended,
133 135
     expendCollapsingToolBarOnTopTabChange: originalStyleObject.expendCollapsingToolBarOnTopTabChange,
134 136
     titleBarHidden: originalStyleObject.navBarHidden,
135 137
     titleBarHideOnScroll: originalStyleObject.navBarHideOnScroll,