Browse Source

Create AppStyle and set button color from screen

Guy Carmeli 8 years ago
parent
commit
04302d349b

+ 13
- 0
android/app/src/main/java/com/reactnativenavigation/params/AppStyle.java View File

@@ -0,0 +1,13 @@
1
+package com.reactnativenavigation.params;
2
+
3
+import android.os.Bundle;
4
+
5
+import com.reactnativenavigation.params.parsers.StyleParamsParser;
6
+
7
+public class AppStyle {
8
+    public static StyleParams appStyle;
9
+
10
+    public static void setAppStyle(Bundle params) {
11
+        appStyle = new StyleParamsParser(params.getBundle("appStyle")).parse();
12
+    }
13
+}

+ 6
- 0
android/app/src/main/java/com/reactnativenavigation/params/TitleBarButtonParams.java View File

@@ -23,4 +23,10 @@ public class TitleBarButtonParams {
23 23
     public StyleParams.Color color;
24 24
     public ShowAsAction showAsAction;
25 25
     public boolean enabled = true;
26
+
27
+    public void setColorFromScreenStyle(StyleParams.Color titleBarButtonColor) {
28
+        if (!color.hasColor() && titleBarButtonColor.hasColor()) {
29
+            color = titleBarButtonColor;
30
+        }
31
+    }
26 32
 }

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

@@ -3,12 +3,13 @@ package com.reactnativenavigation.params.parsers;
3 3
 import android.os.Bundle;
4 4
 
5 5
 import com.reactnativenavigation.params.ActivityParams;
6
+import com.reactnativenavigation.params.AppStyle;
6 7
 
7 8
 public class ActivityParamsParser extends Parser {
8 9
     public static ActivityParams parse(Bundle params) {
9 10
         ActivityParams result = new ActivityParams();
10 11
 
11
-        StyleParamsParser.setAppStyle(params);
12
+        AppStyle.setAppStyle(params);
12 13
 
13 14
         if (hasKey(params, "screen")) {
14 15
             result.type = ActivityParams.Type.SingleScreen;

+ 15
- 19
android/app/src/main/java/com/reactnativenavigation/params/parsers/StyleParamsParser.java View File

@@ -4,16 +4,12 @@ import android.os.Bundle;
4 4
 import android.support.annotation.NonNull;
5 5
 import android.support.annotation.Nullable;
6 6
 
7
+import com.reactnativenavigation.params.AppStyle;
7 8
 import com.reactnativenavigation.params.StyleParams;
8 9
 
9 10
 public class StyleParamsParser {
10
-    private static StyleParams appStyle;
11 11
     private Bundle params;
12 12
 
13
-    public static void setAppStyle(Bundle params) {
14
-        StyleParamsParser.appStyle = new StyleParamsParser(params.getBundle("appStyle")).parse();
15
-    }
16
-
17 13
     public StyleParamsParser(Bundle params) {
18 14
         this.params = params;
19 15
     }
@@ -50,66 +46,66 @@ public class StyleParamsParser {
50 46
 
51 47
     @Nullable
52 48
     private StyleParams.Color getDefaultNavigationColor() {
53
-        return appStyle == null ? new StyleParams.Color() : appStyle.navigationBarColor;
49
+        return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.navigationBarColor;
54 50
     }
55 51
 
56 52
     @Nullable
57 53
     private StyleParams.Color getDefaultSelectedBottomTabsButtonColor() {
58
-        return appStyle == null ? new StyleParams.Color() : appStyle.selectedBottomTabsButtonColor;
54
+        return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.selectedBottomTabsButtonColor;
59 55
     }
60 56
 
61 57
     @Nullable
62 58
     private StyleParams.Color getDefaultBottomTabsButtonColor() {
63
-        return appStyle == null ? new StyleParams.Color() : appStyle.bottomTabsButtonColor;
59
+        return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.bottomTabsButtonColor;
64 60
     }
65 61
 
66 62
     @Nullable
67 63
     private StyleParams.Color getDefaultBottomTabsColor() {
68
-        return appStyle == null ? new StyleParams.Color() : appStyle.bottomTabsColor;
64
+        return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.bottomTabsColor;
69 65
     }
70 66
 
71 67
     private boolean isDefaultBottomTabsHiddenOnScroll() {
72
-        return appStyle != null && appStyle.bottomTabsHiddenOnScroll;
68
+        return AppStyle.appStyle != null && AppStyle.appStyle.bottomTabsHiddenOnScroll;
73 69
     }
74 70
 
75 71
     private boolean isDefaultBottomTabsHidden() {
76
-        return appStyle != null && appStyle.bottomTabsHidden;
72
+        return AppStyle.appStyle != null && AppStyle.appStyle.bottomTabsHidden;
77 73
     }
78 74
 
79 75
     private boolean isDefaultScreenBelowTopBar() {
80
-        return appStyle == null || appStyle.drawScreenBelowTopBar;
76
+        return AppStyle.appStyle == null || AppStyle.appStyle.drawScreenBelowTopBar;
81 77
     }
82 78
 
83 79
     private boolean isDefaultTopTabsHidden() {
84
-        return appStyle != null && appStyle.topTabsHidden;
80
+        return AppStyle.appStyle != null && AppStyle.appStyle.topTabsHidden;
85 81
     }
86 82
 
87 83
     private boolean isDefaultBackButtonHidden() {
88
-        return appStyle != null && appStyle.backButtonHidden;
84
+        return AppStyle.appStyle != null && AppStyle.appStyle.backButtonHidden;
89 85
     }
90 86
 
91 87
     @Nullable
92 88
     private StyleParams.Color getDefaultTitleBarColor() {
93
-        return appStyle == null ? new StyleParams.Color() : appStyle.titleBarTitleColor;
89
+        return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.titleBarTitleColor;
94 90
     }
95 91
 
96 92
     @Nullable
97 93
     private StyleParams.Color getTitleBarButtonColor() {
98
-        return appStyle == null ? new StyleParams.Color() : appStyle.titleBarButtonColor;
94
+        return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.titleBarButtonColor;
99 95
     }
100 96
 
101 97
     private boolean isDefaultTopBarHidden() {
102
-        return appStyle != null && appStyle.titleBarHidden;
98
+        return AppStyle.appStyle != null && AppStyle.appStyle.titleBarHidden;
103 99
     }
104 100
 
105 101
     @Nullable
106 102
     private StyleParams.Color getDefaultTopBarColor() {
107
-        return appStyle == null ? new StyleParams.Color() : appStyle.topBarColor;
103
+        return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.topBarColor;
108 104
     }
109 105
 
110 106
     @Nullable
111 107
     private StyleParams.Color getDefaultStatusBarColor() {
112
-        return appStyle == null ? new StyleParams.Color() : appStyle.statusBarColor;
108
+        return AppStyle.appStyle == null ? new StyleParams.Color() : AppStyle.appStyle.statusBarColor;
113 109
     }
114 110
 
115 111
     private boolean getBoolean(String titleBarHidden, boolean defaultValue) {

+ 7
- 1
android/app/src/main/java/com/reactnativenavigation/params/parsers/TitleBarButtonParamsParser.java View File

@@ -2,6 +2,7 @@ package com.reactnativenavigation.params.parsers;
2 2
 
3 3
 import android.os.Bundle;
4 4
 
5
+import com.reactnativenavigation.params.AppStyle;
5 6
 import com.reactnativenavigation.params.StyleParams;
6 7
 import com.reactnativenavigation.params.TitleBarButtonParams;
7 8
 import com.reactnativenavigation.react.ImageLoader;
@@ -25,13 +26,18 @@ public class TitleBarButtonParamsParser extends Parser {
25 26
         if (hasKey(bundle,"icon")) {
26 27
             result.icon = ImageLoader.loadImage(bundle.getString("icon"));
27 28
         }
28
-        result.color = StyleParams.Color.parse(bundle.getString("color"));
29
+        result.color = getColor(bundle, AppStyle.appStyle.titleBarButtonColor);
29 30
         result.showAsAction = parseShowAsAction(bundle.getString("showAsAction"));
30 31
         result.enabled = bundle.getBoolean("enabled", true);
31 32
         result.eventId = bundle.getString("id");
32 33
         return result;
33 34
     }
34 35
 
36
+    private StyleParams.Color getColor(Bundle bundle, StyleParams.Color defaultColor) {
37
+        StyleParams.Color color = StyleParams.Color.parse(bundle.getString("color"));
38
+        return color.hasColor() || defaultColor == null ? color : defaultColor;
39
+    }
40
+
35 41
     private static TitleBarButtonParams.ShowAsAction parseShowAsAction(String showAsAction) {
36 42
         if (showAsAction == null) {
37 43
             return TitleBarButtonParams.ShowAsAction.IfRoom;

+ 7
- 0
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java View File

@@ -125,6 +125,7 @@ public abstract class Screen extends RelativeLayout {
125 125
     }
126 126
 
127 127
     public void setTitleBarRightButtons(String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
128
+        setButtonColorFromScreen(titleBarButtons);
128 129
         topBar.setTitleBarRightButtons(navigatorEventId, titleBarButtons);
129 130
     }
130 131
 
@@ -136,4 +137,10 @@ public abstract class Screen extends RelativeLayout {
136 137
     public StyleParams getStyleParams() {
137 138
         return screenParams.styleParams;
138 139
     }
140
+
141
+    private void setButtonColorFromScreen(List<TitleBarButtonParams> titleBarButtonParamses) {
142
+        for (TitleBarButtonParams titleBarButtonParamse : titleBarButtonParamses) {
143
+            titleBarButtonParamse.setColorFromScreenStyle(screenParams.styleParams.titleBarButtonColor);
144
+        }
145
+    }
139 146
 }