Skip to content

Commit

Permalink
feat(android): notify Ti.UI.Color about UI style change
Browse files Browse the repository at this point in the history
  • Loading branch information
drauggres committed Mar 21, 2022
1 parent de2c852 commit fda1f98
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
28 changes: 28 additions & 0 deletions android/modules/ui/src/java/ti/modules/titanium/ui/UIModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;

import java.util.WeakHashMap;

@Kroll.module
public class UIModule extends KrollModule implements TiApplication.ConfigurationChangedListener
{
Expand Down Expand Up @@ -436,6 +438,31 @@ public class UIModule extends KrollModule implements TiApplication.Configuration

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

protected static WeakHashMap<UIStyleChangedListener, Object> uiStyleChangedListeners = new WeakHashMap<>();

public interface UIStyleChangedListener {
void onUserInterfaceStyleChanged(int styleId);
}

public static void addUIStyleChangedListener(UIStyleChangedListener a)
{
Log.d(TAG, "addUIStyleChangedListener");
uiStyleChangedListeners.put(a, null);
}

public static void removeUIStyleChangedListener(UIStyleChangedListener a)
{
Log.d(TAG, "removeUIStyleChangedListener");
uiStyleChangedListeners.remove(a);
}

public static void notifyUIStyleChangedListeners(int styleId)
{
for (UIStyleChangedListener listener: uiStyleChangedListeners.keySet()) {
listener.onUserInterfaceStyleChanged(styleId);
}
}

public UIModule()
{
super();
Expand Down Expand Up @@ -610,6 +637,7 @@ public void onConfigurationChanged(@NonNull Configuration config)
KrollDict event = new KrollDict();
event.put(TiC.PROPERTY_VALUE, lastEmittedStyle);
fireEvent(TiC.EVENT_USER_INTERFACE_STYLE, event);
notifyUIStyleChangedListeners(lastEmittedStyle);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,42 @@

import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.util.TiColorHelper;

import androidx.annotation.ColorInt;

import ti.modules.titanium.ui.UIModule;

@Kroll.proxy
/**
* This is a proxy representation of the Android Color type.
* Refer to <a href="https://developer.android.com/reference/android/graphics/Color">Android Color</a> for more details.
*/
public class ColorProxy extends KrollProxy
public class ColorProxy extends KrollProxy implements UIModule.UIStyleChangedListener
{
private final @ColorInt int color;
private @ColorInt int color;
private final String name;
private boolean hasActualColorIntValue = true;

public ColorProxy(@ColorInt int colorInt)
{
this.color = colorInt;
this.name = null;
this(colorInt, null);
}

public ColorProxy(@ColorInt int colorInt, String name)
{
this.color = colorInt;
this.name = name;
UIModule.addUIStyleChangedListener(this);
}

private @ColorInt int getColor()
{
if (!hasActualColorIntValue) {
color = TiColorHelper.getColorResource(name);
hasActualColorIntValue = true;
}
return color;
}

public String getName()
Expand All @@ -41,13 +54,27 @@ public String getName()
@Kroll.method
public String toHex()
{
@ColorInt int value = getColor();
// Convert to ARGB hex string.
return String.format(
"#%02X%02X%02X%02X",
this.color >>> 24,
(this.color >>> 16) & 0xFF,
(this.color >>> 8) & 0xFF,
this.color & 0xFF);
value >>> 24,
(value >>> 16) & 0xFF,
(value >>> 8) & 0xFF,
value & 0xFF);
}

@Override
public void onUserInterfaceStyleChanged(int styleId)
{
this.hasActualColorIntValue = false;
}

@Override
public void release()
{
super.release();
UIModule.removeUIStyleChangedListener(this);
}

@Kroll.method
Expand Down

0 comments on commit fda1f98

Please sign in to comment.