Browse Source

Add Support for all common color formats on Android (#497)

closes #454
Guy Carmeli 8 years ago
parent
commit
7074da20a0

+ 9
- 1
android/app/src/main/java/com/reactnativenavigation/bridge/BundleConverter.java View File

@@ -19,7 +19,7 @@ public class BundleConverter {
19 19
                     bundle.putBoolean(key, map.getBoolean(key));
20 20
                     break;
21 21
                 case Number:
22
-                    bundle.putDouble(key, map.getDouble(key));
22
+                    putNumber(bundle, map, key);
23 23
                     break;
24 24
                 case String:
25 25
                     bundle.putString(key, map.getString(key));
@@ -37,6 +37,14 @@ public class BundleConverter {
37 37
         return bundle;
38 38
     }
39 39
 
40
+    private static void putNumber(Bundle bundle, ReadableMap map, String key) {
41
+        try {
42
+            bundle.putInt(key, map.getInt(key));
43
+        } catch (Exception e) {
44
+            bundle.putDouble(key, map.getDouble(key));
45
+        }
46
+    }
47
+
40 48
     public static Bundle toBundle(ReadableArray array) {
41 49
         Bundle bundle = new Bundle();
42 50
         for (int i = 0; i < array.size(); i++) {

+ 3
- 5
android/app/src/main/java/com/reactnativenavigation/params/StyleParams.java View File

@@ -1,5 +1,6 @@
1 1
 package com.reactnativenavigation.params;
2 2
 
3
+import android.os.Bundle;
3 4
 import android.support.annotation.ColorInt;
4 5
 
5 6
 public class StyleParams {
@@ -27,11 +28,8 @@ public class StyleParams {
27 28
             return color;
28 29
         }
29 30
 
30
-        public static Color parse(String str) {
31
-            if (str == null) {
32
-                return new Color();
33
-            }
34
-            return new Color(android.graphics.Color.parseColor(str));
31
+        public static Color parse(Bundle bundle, String key) {
32
+            return bundle.containsKey(key) ? new Color(bundle.getInt(key)) : new Color();
35 33
         }
36 34
     }
37 35
 

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

@@ -22,7 +22,7 @@ public class ContextualMenuButtonParamsParser extends TitleBarButtonParamsParser
22 22
         ContextualMenuButtonParams result = new ContextualMenuButtonParams();
23 23
         result.icon = ImageLoader.loadImage(button.getString("icon"));
24 24
         result.showAsAction = parseShowAsAction(button.getString("showAsAction"));
25
-        result.color = StyleParams.Color.parse(button.getString("color"));
25
+        result.color = StyleParams.Color.parse(button, "color");
26 26
         result.label = button.getString("label");
27 27
         result.index = (int) button.getDouble("index");
28 28
         return result;

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

@@ -12,7 +12,7 @@ public class FabActionParamsParser extends Parser {
12 12
         fabActionParams.id = params.getString("id");
13 13
         fabActionParams.navigatorEventId = navigatorEventId;
14 14
         fabActionParams.icon = ImageLoader.loadImage(params.getString("icon"));
15
-        fabActionParams.backgroundColor = StyleParams.Color.parse(params.getString("backgroundColor"));
15
+        fabActionParams.backgroundColor = StyleParams.Color.parse(params, "backgroundColor");
16 16
         return fabActionParams;
17 17
     }
18 18
 }

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

@@ -38,7 +38,7 @@ public class Parser {
38 38
     }
39 39
 
40 40
     protected StyleParams.Color getColor(Bundle bundle, String key, StyleParams.Color defaultColor) {
41
-        StyleParams.Color color = StyleParams.Color.parse(bundle.getString(key));
41
+        StyleParams.Color color = StyleParams.Color.parse(bundle, key);
42 42
         return color.hasColor() || defaultColor == null ? color : defaultColor;
43 43
     }
44 44
 }

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

@@ -193,7 +193,7 @@ public class StyleParamsParser {
193 193
     }
194 194
 
195 195
     private StyleParams.Color getColor(String key, StyleParams.Color defaultColor) {
196
-        StyleParams.Color color = StyleParams.Color.parse(params.getString(key));
196
+        StyleParams.Color color = StyleParams.Color.parse(params, key);
197 197
         if (color.hasColor()) {
198 198
             return color;
199 199
         } else {

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

@@ -1,7 +1,6 @@
1 1
 package com.reactnativenavigation.views;
2 2
 
3 3
 import android.content.Context;
4
-import android.util.Log;
5 4
 import android.view.View;
6 5
 
7 6
 import com.facebook.react.ReactRootView;
@@ -52,7 +51,6 @@ public class ContentView extends ReactRootView {
52 51
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
53 52
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
54 53
         int measuredHeight = viewMeasurer.getMeasuredHeight(heightMeasureSpec);
55
-        Log.i("Guy", "measuredHeight: " + measuredHeight + " " + getClass().getSimpleName());
56 54
         setMeasuredDimension(viewMeasurer.getMeasuredWidth(widthMeasureSpec),
57 55
                 measuredHeight);
58 56
     }

+ 20
- 20
src/deprecated/platformSpecificDeprecated.android.js View File

@@ -116,45 +116,45 @@ function convertStyleParams(originalStyleObject) {
116 116
   }
117 117
 
118 118
   let ret = {
119
-    statusBarColor: originalStyleObject.statusBarColor,
120
-    topBarColor: originalStyleObject.navBarBackgroundColor,
119
+    statusBarColor: processColor(originalStyleObject.statusBarColor),
120
+    topBarColor: processColor(originalStyleObject.navBarBackgroundColor),
121 121
     topBarTransparent: originalStyleObject.navBarTransparent,
122 122
     topBarTranslucent: originalStyleObject.navBarTranslucent,
123 123
     collapsingToolBarImage: originalStyleObject.collapsingToolBarImage,
124
-    collapsingToolBarCollapsedColor: originalStyleObject.collapsingToolBarCollapsedColor,
124
+    collapsingToolBarCollapsedColor: processColor(originalStyleObject.collapsingToolBarCollapsedColor),
125 125
     titleBarHidden: originalStyleObject.navBarHidden,
126 126
     titleBarHideOnScroll: originalStyleObject.navBarHideOnScroll,
127
-    titleBarTitleColor: originalStyleObject.navBarTextColor,
128
-    titleBarSubtitleColor: originalStyleObject.navBarTextSubtitleColor,
129
-    titleBarButtonColor: originalStyleObject.navBarButtonColor,
130
-    titleBarDisabledButtonColor: originalStyleObject.titleBarDisabledButtonColor,
127
+    titleBarTitleColor: processColor(originalStyleObject.navBarTextColor),
128
+    titleBarSubtitleColor: processColor(originalStyleObject.navBarTextSubtitleColor),
129
+    titleBarButtonColor: processColor(originalStyleObject.navBarButtonColor),
130
+    titleBarDisabledButtonColor: processColor(originalStyleObject.titleBarDisabledButtonColor),
131 131
     backButtonHidden: originalStyleObject.backButtonHidden,
132 132
     topTabsHidden: originalStyleObject.topTabsHidden,
133
-    contextualMenuStatusBarColor: originalStyleObject.contextualMenuStatusBarColor,
134
-    contextualMenuBackgroundColor: originalStyleObject.contextualMenuBackgroundColor,
135
-    contextualMenuButtonsColor: originalStyleObject.contextualMenuButtonsColor,
133
+    contextualMenuStatusBarColor: processColor(originalStyleObject.contextualMenuStatusBarColor),
134
+    contextualMenuBackgroundColor: processColor(originalStyleObject.contextualMenuBackgroundColor),
135
+    contextualMenuButtonsColor: processColor(originalStyleObject.contextualMenuButtonsColor),
136 136
 
137 137
     drawBelowTopBar: !originalStyleObject.drawUnderNavBar,
138 138
 
139
-    topTabTextColor: originalStyleObject.topTabTextColor,
140
-    selectedTopTabTextColor: originalStyleObject.selectedTopTabTextColor,
139
+    topTabTextColor: processColor(originalStyleObject.topTabTextColor),
140
+    selectedTopTabTextColor: processColor(originalStyleObject.selectedTopTabTextColor),
141 141
     selectedTopTabIndicatorHeight: originalStyleObject.selectedTopTabIndicatorHeight,
142
-    selectedTopTabIndicatorColor: originalStyleObject.selectedTopTabIndicatorColor,
142
+    selectedTopTabIndicatorColor: processColor(originalStyleObject.selectedTopTabIndicatorColor),
143 143
 
144
-    screenBackgroundColor: originalStyleObject.screenBackgroundColor,
144
+    screenBackgroundColor: processColor(originalStyleObject.screenBackgroundColor),
145 145
 
146 146
     drawScreenAboveBottomTabs: !originalStyleObject.drawUnderTabBar,
147 147
 
148
-    bottomTabsColor: originalStyleObject.tabBarBackgroundColor,
149
-    bottomTabsButtonColor: originalStyleObject.tabBarButtonColor,
150
-    bottomTabsSelectedButtonColor: originalStyleObject.tabBarSelectedButtonColor,
148
+    bottomTabsColor: processColor(originalStyleObject.tabBarBackgroundColor),
149
+    bottomTabsButtonColor: processColor(originalStyleObject.tabBarButtonColor),
150
+    bottomTabsSelectedButtonColor: processColor(originalStyleObject.tabBarSelectedButtonColor),
151 151
     bottomTabsHidden: originalStyleObject.tabBarHidden,
152 152
     bottomTabsHiddenOnScroll: originalStyleObject.bottomTabsHiddenOnScroll,
153 153
     forceTitlesDisplay: originalStyleObject.forceTitlesDisplay,
154
-    bottomTabBadgeTextColor: originalStyleObject.bottomTabBadgeTextColor,
155
-    bottomTabBadgeBackgroundColor: originalStyleObject.bottomTabBadgeBackgroundColor,
154
+    bottomTabBadgeTextColor: processColor(originalStyleObject.bottomTabBadgeTextColor),
155
+    bottomTabBadgeBackgroundColor: processColor(originalStyleObject.bottomTabBadgeBackgroundColor),
156 156
 
157
-    navigationBarColor: originalStyleObject.navigationBarColor
157
+    navigationBarColor: processColor(originalStyleObject.navigationBarColor)
158 158
   }
159 159
 
160 160
   if (originalStyleObject.collapsingToolBarImage) {