Skip to content

Commit

Permalink
feat(android): track color of the Ti.UI.Switch
Browse files Browse the repository at this point in the history
  • Loading branch information
m1ga committed Jun 3, 2024
1 parent 2f1212f commit adbba4d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
TiC.PROPERTY_COLOR,
TiC.PROPERTY_FONT,
TiC.PROPERTY_TEXT_ALIGN,
TiC.PROPERTY_TINT_COLOR,
TiC.PROPERTY_ACTIVE_TINT_COLOR,
TiC.PROPERTY_VERTICAL_ALIGN
})
public class SwitchProxy extends TiViewProxy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
package ti.modules.titanium.ui.widget;

import android.app.Activity;
import android.content.res.ColorStateList;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import androidx.appcompat.widget.AppCompatToggleButton;

import com.google.android.material.checkbox.MaterialCheckBox;
import com.google.android.material.chip.Chip;
import com.google.android.material.switchmaterial.SwitchMaterial;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.common.Log;
Expand All @@ -23,21 +27,23 @@
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.view.TiUIView;

import ti.modules.titanium.ui.UIModule;

public class TiUISwitch extends TiUIView implements OnCheckedChangeListener
{
private static final String TAG = "TiUISwitch";

private View.OnLayoutChangeListener layoutListener;
private final View.OnLayoutChangeListener layoutListener;
private boolean oldValue = false;

public TiUISwitch(TiViewProxy proxy)
{
super(proxy);
Log.d(TAG, "Creating a switch", Log.DEBUG_MODE);

this.layoutListener = new View.OnLayoutChangeListener() {
this.layoutListener = new View.OnLayoutChangeListener()
{
@Override
public void onLayoutChange(
View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
Expand All @@ -58,6 +64,34 @@ public void processProperties(KrollDict d)
setStyle(TiConvert.toInt(d.get(TiC.PROPERTY_STYLE), UIModule.SWITCH_STYLE_SLIDER));
}

if (d.containsKeyAndNotNull(TiC.PROPERTY_TINT_COLOR)
|| d.containsKeyAndNotNull(TiC.PROPERTY_ACTIVE_TINT_COLOR)) {
CompoundButton currentButton = (CompoundButton) getNativeView();
if (currentButton instanceof SwitchMaterial) {

int colActive = d.containsKeyAndNotNull(TiC.PROPERTY_ACTIVE_TINT_COLOR)
? TiConvert.toColor(d, TiC.PROPERTY_ACTIVE_TINT_COLOR)
: TiConvert.toColor(d, TiC.PROPERTY_TINT_COLOR);
int colNormal = d.containsKeyAndNotNull(TiC.PROPERTY_TINT_COLOR)
? TiConvert.toColor(d, TiC.PROPERTY_TINT_COLOR)
: TiConvert.toColor(d, TiC.PROPERTY_ACTIVE_TINT_COLOR);

ColorStateList trackStates = new ColorStateList(
new int[][] {
new int[] { -android.R.attr.state_enabled },
new int[] { android.R.attr.state_checked },
new int[] {}
},
new int[] {
colNormal,
colActive,
colNormal
}
);
((SwitchMaterial) currentButton).setTrackTintList(trackStates);
}
}

if (d.containsKey(TiC.PROPERTY_VALUE)) {
oldValue = TiConvert.toBoolean(d, TiC.PROPERTY_VALUE);
}
Expand Down Expand Up @@ -151,7 +185,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
} else {
super.propertyChanged(key, oldValue, newValue, proxy);
}
}
}

@Override
public void onCheckedChanged(CompoundButton btn, boolean value)
Expand Down
19 changes: 17 additions & 2 deletions apidoc/Titanium/UI/Switch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,25 @@ properties:

- name: tintColor
summary: The color used to tint the outline of the switch when it is turned off.
description: |
The color used to tint the outline of the switch when it is turned off.
Android: Track color of the Material Switch.
type: [String, Titanium.UI.Color]
default: undefined
platforms: [iphone, ipad, macos]
since: "3.3.0"
platforms: [android, iphone, ipad, macos]
since: {android: 12.4.0, iphone: 3.3.0, ipad: 3.3.0, macos: 3.3.0}

- name: activeTintColor
summary: The color used to tint the track of the switch when it is turned on.
description: |
The color used to tint the track of the switch when it is turned on.
Android: Active track color of the Material Switch.
type: [String, Titanium.UI.Color]
default: undefined
platforms: [android]
since: {android: 12.4.0}

- name: onTintColor
summary: The color used to tint the appearance of the switch when it is turned on.
Expand Down

0 comments on commit adbba4d

Please sign in to comment.