Skip to content

Commit 74b0703

Browse files
committed
feat(android): parity Ti.UI.Color. allow use in properties
- "Ti.UI.fetchSemanticColor" returns "Ti.UI.Color" - Support "Ti.UI.Color" instances for components properties fix #13272
1 parent bdfd640 commit 74b0703

File tree

29 files changed

+211
-222
lines changed

29 files changed

+211
-222
lines changed

android/modules/android/src/java/ti/modules/titanium/android/notificationmanager/NotificationProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public void setLargeIcon(Object icon)
196196
}
197197

198198
@Kroll.setProperty
199-
public void setColor(String color)
199+
public void setColor(Object color)
200200
{
201201
notificationBuilder.setColor(TiColorHelper.parseColor(color, getActivity()));
202202
setProperty(TiC.PROPERTY_COLOR, color);

android/modules/ui/src/java/ti/modules/titanium/ui/ImageViewProxy.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
@Kroll.proxy(creatableInModule = UIModule.class,
1818
propertyAccessors = {
19-
TiC.PROPERTY_DECODE_RETRIES,
2019
TiC.PROPERTY_AUTOROTATE,
20+
TiC.PROPERTY_DECODE_RETRIES,
2121
TiC.PROPERTY_DEFAULT_IMAGE,
2222
TiC.PROPERTY_DURATION,
2323
TiC.PROPERTY_ENABLE_ZOOM_CONTROLS,
@@ -26,7 +26,8 @@
2626
TiC.PROPERTY_IMAGE_TOUCH_FEEDBACK_COLOR,
2727
TiC.PROPERTY_IMAGES,
2828
TiC.PROPERTY_REPEAT_COUNT,
29-
TiC.PROPERTY_SCALING_MODE
29+
TiC.PROPERTY_SCALING_MODE,
30+
TiC.PROPERTY_TINT_COLOR
3031
})
3132
public class ImageViewProxy extends ViewProxy
3233
{
@@ -108,18 +109,6 @@ public TiBlob toBlob()
108109
return getImageView().toBlob();
109110
}
110111

111-
@Kroll.setProperty(runOnUiThread = true)
112-
public void setTintColor(String color)
113-
{
114-
getImageView().setTintColor(color);
115-
}
116-
117-
@Kroll.getProperty
118-
public int getTintColor()
119-
{
120-
return getImageView().getTintColor();
121-
}
122-
123112
@Override
124113
public String getApiName()
125114
{

android/modules/ui/src/java/ti/modules/titanium/ui/PickerRowProxy.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public void handleCreationDict(KrollDict options)
4747
}
4848

4949
@Kroll.getProperty
50-
public String getColor()
50+
public Object getColor()
5151
{
52-
return (String) getProperty(TiC.PROPERTY_COLOR);
52+
return getProperty(TiC.PROPERTY_COLOR);
5353
}
5454

5555
@Kroll.setProperty
56-
public void setColor(String color)
56+
public void setColor(Object color)
5757
{
5858
setPropertyAndFire(TiC.PROPERTY_COLOR, color);
5959
onRowChanged();

android/modules/ui/src/java/ti/modules/titanium/ui/RefreshControlProxy.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import android.graphics.Color;
1010
import java.util.HashSet;
1111
import org.appcelerator.kroll.annotations.Kroll;
12-
import org.appcelerator.kroll.common.Log;
1312
import org.appcelerator.kroll.KrollDict;
1413
import org.appcelerator.kroll.KrollProxy;
1514
import org.appcelerator.titanium.TiC;
@@ -125,11 +124,8 @@ private void onTintColorChanged(Object colorName)
125124
// Fetch and store the new tint color value.
126125
if (colorName == null) {
127126
this.tintColor = RefreshControlProxy.DEFAULT_TINT_COLOR;
128-
} else if (colorName instanceof String) {
129-
this.tintColor = TiColorHelper.parseColor((String) colorName, getActivity());
130127
} else {
131-
Log.e(TAG, "Property '" + TiC.PROPERTY_TINT_COLOR + "' must be of type string.");
132-
return;
128+
this.tintColor = TiColorHelper.parseColor(colorName, getActivity());
133129
}
134130

135131
// Do not continue if a view has not been assigned to the refresh control yet.

android/modules/ui/src/java/ti/modules/titanium/ui/UIModule.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.appcelerator.titanium.TiC;
1818
import org.appcelerator.titanium.TiDimension;
1919
import org.appcelerator.titanium.TiRootActivity;
20+
import org.appcelerator.titanium.proxy.ColorProxy;
2021
import org.appcelerator.titanium.util.TiAnimationCurve;
2122
import org.appcelerator.titanium.util.TiColorHelper;
2223
import org.appcelerator.titanium.util.TiDeviceOrientation;
@@ -455,12 +456,12 @@ public void onDisposing(KrollRuntime runtime)
455456
}
456457

457458
@Kroll.setProperty(runOnUiThread = true)
458-
public void setBackgroundColor(String color)
459+
public void setBackgroundColor(Object color)
459460
{
460461
doSetBackgroundColor(color);
461462
}
462463

463-
protected void doSetBackgroundColor(String color)
464+
protected void doSetBackgroundColor(Object color)
464465
{
465466
TiRootActivity root = TiApplication.getInstance().getRootActivity();
466467
if (root != null) {
@@ -571,6 +572,17 @@ public int getUserInterfaceStyle()
571572
return getUserInterfaceStyle(TiApplication.getInstance().getResources().getConfiguration());
572573
}
573574

575+
@Kroll.method
576+
public ColorProxy fetchSemanticColor(Object value)
577+
{
578+
if (value instanceof ColorProxy) {
579+
return (ColorProxy) value;
580+
} else if (value instanceof String) {
581+
return TiColorHelper.getColorProxy(value);
582+
}
583+
return null;
584+
}
585+
574586
@Override
575587
public String getApiName()
576588
{

android/modules/ui/src/java/ti/modules/titanium/ui/WindowProxy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public void windowCreated(TiBaseActivity activity, Bundle savedInstanceState)
305305

306306
// Handle barColor property.
307307
if (hasProperty(TiC.PROPERTY_BAR_COLOR)) {
308-
int colorInt = TiColorHelper.parseColor(TiConvert.toString(getProperty(TiC.PROPERTY_BAR_COLOR)), activity);
308+
int colorInt = TiColorHelper.parseColor(getProperty(TiC.PROPERTY_BAR_COLOR), activity);
309309
ActionBar actionBar = activity.getSupportActionBar();
310310
// Guard for using a theme with actionBar disabled.
311311
if (actionBar != null) {
@@ -402,7 +402,7 @@ public void onPropertyChanged(String name, Object value)
402402
if (actionBar != null) {
403403
// Change to background to the new color.
404404
actionBar.setBackgroundDrawable(
405-
new ColorDrawable(TiColorHelper.parseColor(TiConvert.toString(value), activity)));
405+
new ColorDrawable(TiColorHelper.parseColor(value, activity)));
406406
} else {
407407
// Log a warning if there is no ActionBar available.
408408
Log.w(TAG, "There is no ActionBar available for this Window.");

android/modules/ui/src/java/ti/modules/titanium/ui/android/AndroidModule.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@
2121
import ti.modules.titanium.ui.widget.webview.TiUIWebView;
2222

2323
import android.app.Activity;
24-
import android.content.Context;
2524
import android.content.Intent;
2625
import android.graphics.PixelFormat;
2726
import android.view.Gravity;
2827
import android.view.WindowManager;
2928
import android.webkit.WebSettings;
3029

31-
import androidx.annotation.ColorInt;
32-
import androidx.core.content.ContextCompat;
3330
import androidx.core.view.GravityCompat;
3431

3532
@SuppressWarnings("deprecation")
@@ -274,27 +271,7 @@ public void run()
274271
@Kroll.method
275272
public ColorProxy getColorResource(Object idOrName)
276273
{
277-
try {
278-
// Color by resource id
279-
if (idOrName instanceof Number) {
280-
int colorResId = ((Number) idOrName).intValue();
281-
Context context = TiApplication.getAppRootOrCurrentActivity();
282-
if (context == null) {
283-
context = TiApplication.getInstance();
284-
}
285-
@ColorInt int packedColorInt = ContextCompat.getColor(context, colorResId);
286-
return new ColorProxy(packedColorInt);
287-
}
288-
// Color by name
289-
String colorName = idOrName.toString();
290-
if (TiColorHelper.hasColorResource(colorName)) {
291-
@ColorInt int packedColorInt = TiColorHelper.getColorResource(colorName);
292-
return new ColorProxy(packedColorInt);
293-
}
294-
} catch (Exception e) {
295-
// ignore
296-
}
297-
return null;
274+
return TiColorHelper.getColorProxy(idOrName);
298275
}
299276

300277
@Override

android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiImageView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,9 @@ public void setOrientation(TiExifOrientation exifOrientation)
395395
updateScaleType();
396396
}
397397

398-
public void setTintColor(String color)
398+
public void setTintColor(Object color)
399399
{
400-
if (color == null || color.isEmpty()) {
400+
if (color == null) {
401401
imageView.clearColorFilter();
402402
return;
403403
}

android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiToolbar.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ public void setItems(TiViewProxy[] proxies)
124124

125125
/**
126126
* Sets the background color of the toolbar
127-
* @param color String in Titanium color format
127+
* @param value Color in any format supported by Titanium.
128128
*/
129-
public void setToolbarColor(String color)
129+
public void setToolbarColor(Object value)
130130
{
131-
toolbar.setBackgroundColor((TiColorHelper.parseColor(color, proxy.getActivity())));
131+
toolbar.setBackgroundColor((TiColorHelper.parseColor(value, proxy.getActivity())));
132132
if (proxy.hasProperty(TiC.PROPERTY_TRANSLUCENT)) {
133133
if ((Boolean) proxy.getProperty(TiC.PROPERTY_TRANSLUCENT)) {
134134
toolbar.getBackground().setAlpha(BACKGROUND_TRANSLUCENT_VALUE);
@@ -322,7 +322,7 @@ private void setTitle(String value)
322322
* Sets title's text color
323323
* @param value Color in any format supported by Titanium.
324324
*/
325-
private void setTitleTextColor(String value)
325+
private void setTitleTextColor(Object value)
326326
{
327327
toolbar.setTitleTextColor(TiColorHelper.parseColor(value, proxy.getActivity()));
328328
}
@@ -340,7 +340,7 @@ private void setSubtitle(String value)
340340
* Sets subtitle's text color
341341
* @param value Color in any format supported by Titanium.
342342
*/
343-
private void setSubtitleTextColor(String value)
343+
private void setSubtitleTextColor(Object value)
344344
{
345345
toolbar.setSubtitleTextColor(TiColorHelper.parseColor(value, proxy.getActivity()));
346346
}
@@ -394,7 +394,7 @@ public void processProperties(KrollDict d)
394394
{
395395
//region process common properties
396396
if (d.containsKey(TiC.PROPERTY_BAR_COLOR)) {
397-
setToolbarColor(d.getString(TiC.PROPERTY_BAR_COLOR));
397+
setToolbarColor(d.get(TiC.PROPERTY_BAR_COLOR));
398398
}
399399
if (d.containsKey(TiC.PROPERTY_EXTEND_BACKGROUND)) {
400400
if (d.getBoolean(TiC.PROPERTY_EXTEND_BACKGROUND)) {
@@ -422,7 +422,7 @@ public void processProperties(KrollDict d)
422422
setTitle(d.getString(TiC.PROPERTY_TITLE));
423423
}
424424
if (d.containsKey(TiC.PROPERTY_TITLE_TEXT_COLOR)) {
425-
setTitleTextColor(d.getString(TiC.PROPERTY_TITLE_TEXT_COLOR));
425+
setTitleTextColor(d.get(TiC.PROPERTY_TITLE_TEXT_COLOR));
426426
}
427427
if (d.containsKey(TiC.PROPERTY_SUBTITLE)) {
428428
setSubtitle(d.getString(TiC.PROPERTY_SUBTITLE));
@@ -452,7 +452,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
452452
{
453453
super.propertyChanged(key, oldValue, newValue, proxy);
454454
if (key.equals(TiC.PROPERTY_BAR_COLOR)) {
455-
setToolbarColor(((String) newValue));
455+
setToolbarColor(newValue);
456456
}
457457
if (key.equals(TiC.PROPERTY_TRANSLUCENT)) {
458458
setTranslucent(((Boolean) newValue));
@@ -473,13 +473,13 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
473473
setTitle((String) newValue);
474474
}
475475
if (key.equals(TiC.PROPERTY_TITLE_TEXT_COLOR)) {
476-
setTitleTextColor((String) newValue);
476+
setTitleTextColor(newValue);
477477
}
478478
if (key.equals(TiC.PROPERTY_SUBTITLE)) {
479479
setSubtitle((String) newValue);
480480
}
481481
if (key.equals(TiC.PROPERTY_SUBTITLE_TEXT_COLOR)) {
482-
setSubtitleTextColor((String) newValue);
482+
setSubtitleTextColor(newValue);
483483
}
484484
if (key.equals(TiC.PROPERTY_CONTENT_INSET_END_WITH_ACTIONS)) {
485485
setContentInsetEndWithActions((Integer) newValue);

android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIButton.java

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,7 @@
66
*/
77
package ti.modules.titanium.ui.widget;
88

9-
import android.app.Activity;
10-
import android.content.res.ColorStateList;
11-
import android.graphics.Color;
12-
import android.graphics.PorterDuff.Mode;
13-
import android.graphics.drawable.Drawable;
14-
import android.text.TextUtils;
15-
import android.view.Gravity;
16-
import android.view.MotionEvent;
17-
import android.view.View;
18-
19-
import androidx.annotation.NonNull;
20-
import androidx.appcompat.widget.AppCompatButton;
21-
22-
import com.google.android.material.button.MaterialButton;
9+
import java.util.HashMap;
2310

2411
import org.appcelerator.kroll.KrollDict;
2512
import org.appcelerator.kroll.KrollProxy;
@@ -32,11 +19,23 @@
3219
import org.appcelerator.titanium.util.TiUIHelper;
3320
import org.appcelerator.titanium.view.TiUIView;
3421

35-
import java.util.HashMap;
36-
3722
import ti.modules.titanium.ui.AttributedStringProxy;
3823
import ti.modules.titanium.ui.UIModule;
3924

25+
import android.app.Activity;
26+
import android.content.res.ColorStateList;
27+
import android.graphics.Color;
28+
import android.graphics.PorterDuff.Mode;
29+
import android.graphics.drawable.Drawable;
30+
import android.text.TextUtils;
31+
import android.view.Gravity;
32+
import android.view.MotionEvent;
33+
import android.view.View;
34+
import androidx.annotation.NonNull;
35+
import androidx.appcompat.widget.AppCompatButton;
36+
37+
import com.google.android.material.button.MaterialButton;
38+
4039
public class TiUIButton extends TiUIView
4140
{
4241
private static final String TAG = "TiUIButton";
@@ -82,7 +81,7 @@ public TiUIButton(final TiViewProxy proxy)
8281

8382
// Determine if a background drawable will be applied to the button.
8483
boolean hasCustomBackground
85-
= hasImage(proxy.getProperties())
84+
= hasImage(proxy.getProperties())
8685
|| hasColorState(proxy.getProperties())
8786
|| hasBorder(proxy.getProperties())
8887
|| hasGradient(proxy.getProperties());
@@ -91,8 +90,7 @@ public TiUIButton(final TiViewProxy proxy)
9190
// Note: MaterialButton does not support replacing its background drawable. Will log a nasty warning.
9291
AppCompatButton btn;
9392
if (hasCustomBackground) {
94-
btn = new AppCompatButton(proxy.getActivity())
95-
{
93+
btn = new AppCompatButton(proxy.getActivity()) {
9694
@Override
9795
public boolean onFilterTouchEventForSecurity(MotionEvent event)
9896
{
@@ -104,8 +102,7 @@ public boolean onFilterTouchEventForSecurity(MotionEvent event)
104102
}
105103
};
106104
} else {
107-
btn = new MaterialButton(proxy.getActivity(), null, styleId)
108-
{
105+
btn = new MaterialButton(proxy.getActivity(), null, styleId) {
109106
@Override
110107
public boolean onFilterTouchEventForSecurity(MotionEvent event)
111108
{
@@ -118,8 +115,7 @@ public boolean onFilterTouchEventForSecurity(MotionEvent event)
118115
};
119116
this.defaultRippleColor = ((MaterialButton) btn).getRippleColor();
120117
}
121-
btn.addOnLayoutChangeListener(new View.OnLayoutChangeListener()
122-
{
118+
btn.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
123119
@Override
124120
public void onLayoutChange(
125121
View v, int left, int top, int right, int bottom,

0 commit comments

Comments
 (0)