Skip to content

Commit fda1f98

Browse files
committed
feat(android): notify Ti.UI.Color about UI style change
1 parent de2c852 commit fda1f98

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import androidx.annotation.NonNull;
3838
import androidx.appcompat.app.AppCompatDelegate;
3939

40+
import java.util.WeakHashMap;
41+
4042
@Kroll.module
4143
public class UIModule extends KrollModule implements TiApplication.ConfigurationChangedListener
4244
{
@@ -436,6 +438,31 @@ public class UIModule extends KrollModule implements TiApplication.Configuration
436438

437439
protected static final int MSG_LAST_ID = KrollProxy.MSG_LAST_ID + 101;
438440

441+
protected static WeakHashMap<UIStyleChangedListener, Object> uiStyleChangedListeners = new WeakHashMap<>();
442+
443+
public interface UIStyleChangedListener {
444+
void onUserInterfaceStyleChanged(int styleId);
445+
}
446+
447+
public static void addUIStyleChangedListener(UIStyleChangedListener a)
448+
{
449+
Log.d(TAG, "addUIStyleChangedListener");
450+
uiStyleChangedListeners.put(a, null);
451+
}
452+
453+
public static void removeUIStyleChangedListener(UIStyleChangedListener a)
454+
{
455+
Log.d(TAG, "removeUIStyleChangedListener");
456+
uiStyleChangedListeners.remove(a);
457+
}
458+
459+
public static void notifyUIStyleChangedListeners(int styleId)
460+
{
461+
for (UIStyleChangedListener listener: uiStyleChangedListeners.keySet()) {
462+
listener.onUserInterfaceStyleChanged(styleId);
463+
}
464+
}
465+
439466
public UIModule()
440467
{
441468
super();
@@ -610,6 +637,7 @@ public void onConfigurationChanged(@NonNull Configuration config)
610637
KrollDict event = new KrollDict();
611638
event.put(TiC.PROPERTY_VALUE, lastEmittedStyle);
612639
fireEvent(TiC.EVENT_USER_INTERFACE_STYLE, event);
640+
notifyUIStyleChangedListeners(lastEmittedStyle);
613641

614642
}
615643
}

android/titanium/src/java/org/appcelerator/titanium/proxy/ColorProxy.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,42 @@
88

99
import org.appcelerator.kroll.KrollProxy;
1010
import org.appcelerator.kroll.annotations.Kroll;
11+
import org.appcelerator.titanium.util.TiColorHelper;
1112

1213
import androidx.annotation.ColorInt;
1314

15+
import ti.modules.titanium.ui.UIModule;
16+
1417
@Kroll.proxy
1518
/**
1619
* This is a proxy representation of the Android Color type.
1720
* Refer to <a href="https://developer.android.com/reference/android/graphics/Color">Android Color</a> for more details.
1821
*/
19-
public class ColorProxy extends KrollProxy
22+
public class ColorProxy extends KrollProxy implements UIModule.UIStyleChangedListener
2023
{
21-
private final @ColorInt int color;
24+
private @ColorInt int color;
2225
private final String name;
26+
private boolean hasActualColorIntValue = true;
2327

2428
public ColorProxy(@ColorInt int colorInt)
2529
{
26-
this.color = colorInt;
27-
this.name = null;
30+
this(colorInt, null);
2831
}
2932

3033
public ColorProxy(@ColorInt int colorInt, String name)
3134
{
3235
this.color = colorInt;
3336
this.name = name;
37+
UIModule.addUIStyleChangedListener(this);
38+
}
39+
40+
private @ColorInt int getColor()
41+
{
42+
if (!hasActualColorIntValue) {
43+
color = TiColorHelper.getColorResource(name);
44+
hasActualColorIntValue = true;
45+
}
46+
return color;
3447
}
3548

3649
public String getName()
@@ -41,13 +54,27 @@ public String getName()
4154
@Kroll.method
4255
public String toHex()
4356
{
57+
@ColorInt int value = getColor();
4458
// Convert to ARGB hex string.
4559
return String.format(
4660
"#%02X%02X%02X%02X",
47-
this.color >>> 24,
48-
(this.color >>> 16) & 0xFF,
49-
(this.color >>> 8) & 0xFF,
50-
this.color & 0xFF);
61+
value >>> 24,
62+
(value >>> 16) & 0xFF,
63+
(value >>> 8) & 0xFF,
64+
value & 0xFF);
65+
}
66+
67+
@Override
68+
public void onUserInterfaceStyleChanged(int styleId)
69+
{
70+
this.hasActualColorIntValue = false;
71+
}
72+
73+
@Override
74+
public void release()
75+
{
76+
super.release();
77+
UIModule.removeUIStyleChangedListener(this);
5178
}
5279

5380
@Kroll.method

0 commit comments

Comments
 (0)