Browse Source

Additional snackbar style params (#735)

Implement backgroundColor and textColor style params
Guy Carmeli 7 years ago
parent
commit
8833c25016

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

4
     public String text;
4
     public String text;
5
     public String buttonText;
5
     public String buttonText;
6
     public StyleParams.Color buttonColor;
6
     public StyleParams.Color buttonColor;
7
+    public StyleParams.Color backgroundColor;
7
     public String eventId;
8
     public String eventId;
8
     public int duration;
9
     public int duration;
10
+    public StyleParams.Color textColor;
9
 }
11
 }

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

31
         public static Color parse(Bundle bundle, String key) {
31
         public static Color parse(Bundle bundle, String key) {
32
             return bundle.containsKey(key) ? new Color(bundle.getInt(key)) : new Color();
32
             return bundle.containsKey(key) ? new Color(bundle.getInt(key)) : new Color();
33
         }
33
         }
34
+
35
+        public String getHexColor() {
36
+            return String.format("#%06X", (0xFFFFFF & getColor()));
37
+        }
34
     }
38
     }
35
 
39
 
36
     public Color statusBarColor;
40
     public Color statusBarColor;

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

37
         return result;
37
         return result;
38
     }
38
     }
39
 
39
 
40
+    protected StyleParams.Color getColor(Bundle params, String backgroundColor) {
41
+        return getColor(params, backgroundColor, null);
42
+    }
43
+
40
     protected StyleParams.Color getColor(Bundle bundle, String key, StyleParams.Color defaultColor) {
44
     protected StyleParams.Color getColor(Bundle bundle, String key, StyleParams.Color defaultColor) {
41
         StyleParams.Color color = StyleParams.Color.parse(bundle, key);
45
         StyleParams.Color color = StyleParams.Color.parse(bundle, key);
42
         return color.hasColor() || defaultColor == null ? color : defaultColor;
46
         return color.hasColor() || defaultColor == null ? color : defaultColor;

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

1
 package com.reactnativenavigation.params.parsers;
1
 package com.reactnativenavigation.params.parsers;
2
 
2
 
3
+import android.graphics.Color;
3
 import android.os.Bundle;
4
 import android.os.Bundle;
4
 import android.support.design.widget.Snackbar;
5
 import android.support.design.widget.Snackbar;
5
 
6
 
6
 import com.reactnativenavigation.params.AppStyle;
7
 import com.reactnativenavigation.params.AppStyle;
7
 import com.reactnativenavigation.params.SnackbarParams;
8
 import com.reactnativenavigation.params.SnackbarParams;
9
+import com.reactnativenavigation.params.StyleParams;
8
 
10
 
9
 public class SnackbarParamsParser extends Parser {
11
 public class SnackbarParamsParser extends Parser {
10
     public SnackbarParams parse(Bundle params) {
12
     public SnackbarParams parse(Bundle params) {
11
         SnackbarParams result = new SnackbarParams();
13
         SnackbarParams result = new SnackbarParams();
12
         result.text = params.getString("text");
14
         result.text = params.getString("text");
15
+        result.textColor = getColor(params, "textColor", new StyleParams.Color(Color.WHITE));
13
         result.buttonText = params.getString("actionText");
16
         result.buttonText = params.getString("actionText");
14
         result.buttonColor = getColor(params, "actionColor", AppStyle.appStyle.snackbarButtonColor);
17
         result.buttonColor = getColor(params, "actionColor", AppStyle.appStyle.snackbarButtonColor);
18
+        result.backgroundColor = getColor(params, "backgroundColor");
15
         result.duration = getDuration(params.getString("duration", "short"));
19
         result.duration = getDuration(params.getString("duration", "short"));
16
         result.eventId = params.getString("actionId");
20
         result.eventId = params.getString("actionId");
17
         return result;
21
         return result;

+ 8
- 5
android/app/src/main/java/com/reactnativenavigation/views/FloatingActionButtonCoordinator.java View File

36
     FloatingActionButtonAnimator fabAnimator;
36
     FloatingActionButtonAnimator fabAnimator;
37
     private final ArrayList<FloatingActionButton> actions;
37
     private final ArrayList<FloatingActionButton> actions;
38
 
38
 
39
-    public FloatingActionButtonCoordinator(CoordinatorLayout parent) {
39
+    FloatingActionButtonCoordinator(CoordinatorLayout parent) {
40
         this.parent = parent;
40
         this.parent = parent;
41
         actions = new ArrayList<>();
41
         actions = new ArrayList<>();
42
         crossFadeAnimationDuration = parent.getResources().getInteger(android.R.integer.config_shortAnimTime);
42
         crossFadeAnimationDuration = parent.getResources().getInteger(android.R.integer.config_shortAnimTime);
45
 
45
 
46
     public void add(final FabParams params) {
46
     public void add(final FabParams params) {
47
         Log.i(TAG, "add() called with: params = [" + params + "]");
47
         Log.i(TAG, "add() called with: params = [" + params + "]");
48
-        if (parent.getChildCount() > 0) {
48
+        if (hasFab()) {
49
             remove(new Runnable() {
49
             remove(new Runnable() {
50
                 @Override
50
                 @Override
51
                 public void run() {
51
                 public void run() {
66
         fabAnimator.show();
66
         fabAnimator.show();
67
     }
67
     }
68
 
68
 
69
-    public void remove(@Nullable final Runnable onComplete) {
70
-        Log.w(TAG, "remove: ");
71
-        if (parent.getChildCount() == 0) {
69
+    void remove(@Nullable final Runnable onComplete) {
70
+        if (!hasFab()) {
72
             if (onComplete != null) {
71
             if (onComplete != null) {
73
                 onComplete.run();
72
                 onComplete.run();
74
             }
73
             }
88
         fabAnimator.removeActionsFromScreen(actions);
87
         fabAnimator.removeActionsFromScreen(actions);
89
     }
88
     }
90
 
89
 
90
+    private boolean hasFab() {
91
+        return collapsedFab != null || expendedFab != null;
92
+    }
93
+
91
     private void removeAllViews() {
94
     private void removeAllViews() {
92
         parent.removeView(collapsedFab);
95
         parent.removeView(collapsedFab);
93
         parent.removeView(expendedFab);
96
         parent.removeView(expendedFab);

+ 15
- 1
android/app/src/main/java/com/reactnativenavigation/views/Snakbar.java View File

1
 package com.reactnativenavigation.views;
1
 package com.reactnativenavigation.views;
2
 
2
 
3
 import android.support.design.widget.Snackbar;
3
 import android.support.design.widget.Snackbar;
4
+import android.text.Html;
5
+import android.text.Spanned;
4
 import android.view.View;
6
 import android.view.View;
5
 
7
 
6
 import com.reactnativenavigation.NavigationApplication;
8
 import com.reactnativenavigation.NavigationApplication;
36
     }
38
     }
37
 
39
 
38
     private void create() {
40
     private void create() {
39
-        snackbar = Snackbar.make((View) parent, params.text, params.duration);
41
+        snackbar = Snackbar.make((View) parent, getStyledText(), params.duration);
40
         setAction(navigatorEventId, params, snackbar);
42
         setAction(navigatorEventId, params, snackbar);
41
         setStyle(snackbar, params);
43
         setStyle(snackbar, params);
42
         setOnDismissListener();
44
         setOnDismissListener();
43
     }
45
     }
44
 
46
 
47
+    private Spanned getStyledText() {
48
+        String styledText = "<font color=\"" +
49
+                            params.textColor.getHexColor() +
50
+                            "\">" +
51
+                            params.text +
52
+                            "</font>";
53
+        return Html.fromHtml(styledText);
54
+    }
55
+
45
     private void setAction(final String navigatorEventId, final SnackbarParams params, Snackbar snackbar) {
56
     private void setAction(final String navigatorEventId, final SnackbarParams params, Snackbar snackbar) {
46
         if (params.eventId != null) {
57
         if (params.eventId != null) {
47
             snackbar.setAction(params.buttonText, new View.OnClickListener() {
58
             snackbar.setAction(params.buttonText, new View.OnClickListener() {
57
         if (params.buttonColor.hasColor()) {
68
         if (params.buttonColor.hasColor()) {
58
             snackbar.setActionTextColor(params.buttonColor.getColor());
69
             snackbar.setActionTextColor(params.buttonColor.getColor());
59
         }
70
         }
71
+        if (params.backgroundColor.hasColor()) {
72
+            snackbar.getView().setBackgroundColor(params.backgroundColor.getColor());
73
+        }
60
     }
74
     }
61
 
75
 
62
     private void setOnDismissListener() {
76
     private void setOnDismissListener() {

+ 11
- 1
src/deprecated/platformSpecificDeprecated.android.js View File

549
 }
549
 }
550
 
550
 
551
 function showSnackbar(navigator, params) {
551
 function showSnackbar(navigator, params) {
552
-  return newPlatformSpecific.showSnackbar(params);
552
+  const adapted = _.cloneDeep(params);
553
+  if (adapted.backgroundColor) {
554
+    adapted.backgroundColor = processColor(adapted.backgroundColor);
555
+  }
556
+  if (adapted.actionColor) {
557
+    adapted.actionColor = processColor(adapted.actionColor);
558
+  }
559
+  if (adapted.textColor) {
560
+    adapted.textColor = processColor(adapted.textColor);
561
+  }
562
+  return newPlatformSpecific.showSnackbar(adapted);
553
 }
563
 }
554
 
564
 
555
 function showContextualMenu(navigator, params) {
565
 function showContextualMenu(navigator, params) {