Bläddra i källkod

Additional snackbar style params (#735)

Implement backgroundColor and textColor style params
Guy Carmeli 7 år sedan
förälder
incheckning
8833c25016

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/params/SnackbarParams.java Visa fil

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

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/params/StyleParams.java Visa fil

@@ -31,6 +31,10 @@ public class StyleParams {
31 31
         public static Color parse(Bundle bundle, String key) {
32 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 40
     public Color statusBarColor;

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

@@ -37,6 +37,10 @@ public class Parser {
37 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 44
     protected StyleParams.Color getColor(Bundle bundle, String key, StyleParams.Color defaultColor) {
41 45
         StyleParams.Color color = StyleParams.Color.parse(bundle, key);
42 46
         return color.hasColor() || defaultColor == null ? color : defaultColor;

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

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

+ 8
- 5
android/app/src/main/java/com/reactnativenavigation/views/FloatingActionButtonCoordinator.java Visa fil

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

+ 15
- 1
android/app/src/main/java/com/reactnativenavigation/views/Snakbar.java Visa fil

@@ -1,6 +1,8 @@
1 1
 package com.reactnativenavigation.views;
2 2
 
3 3
 import android.support.design.widget.Snackbar;
4
+import android.text.Html;
5
+import android.text.Spanned;
4 6
 import android.view.View;
5 7
 
6 8
 import com.reactnativenavigation.NavigationApplication;
@@ -36,12 +38,21 @@ class Snakbar {
36 38
     }
37 39
 
38 40
     private void create() {
39
-        snackbar = Snackbar.make((View) parent, params.text, params.duration);
41
+        snackbar = Snackbar.make((View) parent, getStyledText(), params.duration);
40 42
         setAction(navigatorEventId, params, snackbar);
41 43
         setStyle(snackbar, params);
42 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 56
     private void setAction(final String navigatorEventId, final SnackbarParams params, Snackbar snackbar) {
46 57
         if (params.eventId != null) {
47 58
             snackbar.setAction(params.buttonText, new View.OnClickListener() {
@@ -57,6 +68,9 @@ class Snakbar {
57 68
         if (params.buttonColor.hasColor()) {
58 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 76
     private void setOnDismissListener() {

+ 11
- 1
src/deprecated/platformSpecificDeprecated.android.js Visa fil

@@ -549,7 +549,17 @@ function addNavigationStyleParams(screen) {
549 549
 }
550 550
 
551 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 565
 function showContextualMenu(navigator, params) {