Skip to content

Commit

Permalink
feat(android): use material based Ti.UI.Switch and add Chip style
Browse files Browse the repository at this point in the history
Fixes TIMOB-28300
  • Loading branch information
jquick-axway authored and sgtcoolguy committed Mar 5, 2021
1 parent a828779 commit 4295acc
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 112 deletions.
6 changes: 0 additions & 6 deletions android/modules/ui/res/layout/titanium_ui_checkbox.xml

This file was deleted.

6 changes: 0 additions & 6 deletions android/modules/ui/res/layout/titanium_ui_switchcompat.xml

This file was deleted.

27 changes: 27 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 @@ -195,6 +195,19 @@ public class UIModule extends KrollModule
@Kroll.constant
public static final int BLEND_MODE_PLUS_LIGHTER = 27;

@Kroll.constant
public static final int BUTTON_STYLE_FILLED = 1;
@Kroll.constant
public static final int BUTTON_STYLE_OUTLINED = 2;
@Kroll.constant
public static final int BUTTON_STYLE_TEXT = 3;
@Kroll.constant
public static final int BUTTON_STYLE_OPTION_POSITIVE = 4;
@Kroll.constant
public static final int BUTTON_STYLE_OPTION_NEGATIVE = 5;
@Kroll.constant
public static final int BUTTON_STYLE_OPTION_NEUTRAL = 6;

@Kroll.constant
public static final int INPUT_BORDERSTYLE_NONE = 0;
@Kroll.constant
Expand All @@ -203,6 +216,11 @@ public class UIModule extends KrollModule
public static final int INPUT_BORDERSTYLE_BEZEL = 2;
@Kroll.constant
public static final int INPUT_BORDERSTYLE_LINE = 3;
@Kroll.constant
public static final int INPUT_BORDERSTYLE_UNDERLINED = 4;
@Kroll.constant
public static final int INPUT_BORDERSTYLE_FILLED = 5;

@Kroll.constant
public static final int INPUT_BUTTONMODE_ONFOCUS = 0;
@Kroll.constant
Expand All @@ -228,6 +246,15 @@ public class UIModule extends KrollModule
@Kroll.constant
public static final int MAP_VIEW_HYBRID = 3;

@Kroll.constant
public static final int SWITCH_STYLE_CHECKBOX = 0;
@Kroll.constant
public static final int SWITCH_STYLE_TOGGLEBUTTON = 1;
@Kroll.constant
public static final int SWITCH_STYLE_SLIDER = 2;
@Kroll.constant
public static final int SWITCH_STYLE_CHIP = 3;

@Kroll.constant
public static final int TABLEVIEW_POSITION_ANY = 0;
@Kroll.constant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ public class AndroidModule extends KrollModule
public static final int SOFT_KEYBOARD_SHOW_ON_FOCUS = TiUIView.SOFT_KEYBOARD_SHOW_ON_FOCUS;

@Kroll.constant
public static final int SWITCH_STYLE_CHECKBOX = 0;
public static final int SWITCH_STYLE_CHECKBOX = UIModule.SWITCH_STYLE_CHECKBOX;
@Kroll.constant
public static final int SWITCH_STYLE_TOGGLEBUTTON = 1;
public static final int SWITCH_STYLE_TOGGLEBUTTON = UIModule.SWITCH_STYLE_TOGGLEBUTTON;
@Kroll.constant
public static final int SWITCH_STYLE_SWITCH = 2;
public static final int SWITCH_STYLE_SWITCH = UIModule.SWITCH_STYLE_SLIDER;

@Kroll.constant
public static final int WEBVIEW_PLUGINS_OFF = TiUIWebView.PLUGIN_STATE_OFF;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,46 @@
*/
package ti.modules.titanium.ui.widget;

import android.app.Activity;
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;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiRHelper;
import org.appcelerator.titanium.util.TiRHelper.ResourceNotFoundException;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.view.TiUIView;

import ti.modules.titanium.ui.android.AndroidModule;
import androidx.appcompat.widget.SwitchCompat;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;
import ti.modules.titanium.ui.UIModule;

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

private 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() {
@Override
public void onLayoutChange(
View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
{
TiUIHelper.firePostLayoutEvent(getProxy());
}
};

propertyChanged(TiC.PROPERTY_STYLE, null, proxy.getProperty(TiC.PROPERTY_STYLE), proxy);
}

Expand All @@ -46,7 +55,7 @@ public void processProperties(KrollDict d)
super.processProperties(d);

if (d.containsKey(TiC.PROPERTY_STYLE)) {
setStyle(TiConvert.toInt(d.get(TiC.PROPERTY_STYLE), AndroidModule.SWITCH_STYLE_SWITCH));
setStyle(TiConvert.toInt(d.get(TiC.PROPERTY_STYLE), UIModule.SWITCH_STYLE_SLIDER));
}

if (d.containsKey(TiC.PROPERTY_VALUE)) {
Expand All @@ -61,25 +70,26 @@ public void processProperties(KrollDict d)

protected void updateButton(CompoundButton cb, KrollDict d)
{
if (d.containsKey(TiC.PROPERTY_TITLE) && cb instanceof CheckBox) {
cb.setText(TiConvert.toString(d, TiC.PROPERTY_TITLE));
if (d.containsKey(TiC.PROPERTY_TITLE)) {
if ((cb instanceof MaterialCheckBox) || (cb instanceof Chip)) {
cb.setText(TiConvert.toString(d, TiC.PROPERTY_TITLE));
}
}
if (d.containsKey(TiC.PROPERTY_TITLE_OFF)) {
if (cb instanceof ToggleButton) {
((ToggleButton) cb).setTextOff(TiConvert.toString(d, TiC.PROPERTY_TITLE_OFF));
} else if (cb instanceof SwitchCompat) {
((SwitchCompat) cb).setTextOff(TiConvert.toString(d, TiC.PROPERTY_TITLE_OFF));
if (cb instanceof AppCompatToggleButton) {
((AppCompatToggleButton) cb).setTextOff(TiConvert.toString(d, TiC.PROPERTY_TITLE_OFF));
} else if (cb instanceof SwitchMaterial) {
((SwitchMaterial) cb).setTextOff(TiConvert.toString(d, TiC.PROPERTY_TITLE_OFF));
}
}
if (d.containsKey(TiC.PROPERTY_TITLE_ON)) {
if (cb instanceof ToggleButton) {
((ToggleButton) cb).setTextOn(TiConvert.toString(d, TiC.PROPERTY_TITLE_ON));
} else if (cb instanceof SwitchCompat) {
((SwitchCompat) cb).setTextOn(TiConvert.toString(d, TiC.PROPERTY_TITLE_ON));
if (cb instanceof AppCompatToggleButton) {
((AppCompatToggleButton) cb).setTextOn(TiConvert.toString(d, TiC.PROPERTY_TITLE_ON));
} else if (cb instanceof SwitchMaterial) {
((SwitchMaterial) cb).setTextOn(TiConvert.toString(d, TiC.PROPERTY_TITLE_ON));
}
}
if (d.containsKey(TiC.PROPERTY_VALUE)) {

cb.setChecked(TiConvert.toBoolean(d, TiC.PROPERTY_VALUE));
}
if (d.containsKey(TiC.PROPERTY_COLOR)) {
Expand Down Expand Up @@ -109,22 +119,22 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
CompoundButton cb = (CompoundButton) getNativeView();
if (key.equals(TiC.PROPERTY_STYLE) && newValue != null) {
setStyle(TiConvert.toInt(newValue));
} else if (key.equals(TiC.PROPERTY_TITLE) && cb instanceof CheckBox) {
cb.setText((String) newValue);
} else if (key.equals(TiC.PROPERTY_TITLE)) {
if ((cb instanceof MaterialCheckBox) || (cb instanceof Chip)) {
cb.setText(TiConvert.toString(newValue));
}
} else if (key.equals(TiC.PROPERTY_TITLE_OFF)) {
if (cb instanceof ToggleButton) {
((ToggleButton) cb).setTextOff((String) newValue);
} else if (cb instanceof SwitchCompat) {
((SwitchCompat) cb).setTextOff((String) newValue);
if (cb instanceof AppCompatToggleButton) {
((AppCompatToggleButton) cb).setTextOff((String) newValue);
} else if (cb instanceof SwitchMaterial) {
((SwitchMaterial) cb).setTextOff((String) newValue);
}

} else if (key.equals(TiC.PROPERTY_TITLE_ON)) {
if (cb instanceof ToggleButton) {
((ToggleButton) cb).setTextOn((String) newValue);
} else if (cb instanceof SwitchCompat) {
((SwitchCompat) cb).setTextOn((String) newValue);
if (cb instanceof AppCompatToggleButton) {
((AppCompatToggleButton) cb).setTextOn((String) newValue);
} else if (cb instanceof SwitchMaterial) {
((SwitchMaterial) cb).setTextOn((String) newValue);
}

} else if (key.equals(TiC.PROPERTY_VALUE)) {
cb.setChecked(TiConvert.toBoolean(newValue));
} else if (key.equals(TiC.PROPERTY_COLOR)) {
Expand All @@ -140,7 +150,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 All @@ -161,65 +171,38 @@ protected void setStyle(int style)
CompoundButton currentButton = (CompoundButton) getNativeView();
CompoundButton button = null;

Activity activity = proxy.getActivity();
if (activity == null) {
activity = TiApplication.getAppCurrentActivity();
if (activity == null) {
return;
}
}

switch (style) {
case AndroidModule.SWITCH_STYLE_CHECKBOX:
if (!(currentButton instanceof CheckBox)) {
int buttonId;
try {
buttonId = TiRHelper.getResource("layout.titanium_ui_checkbox");
} catch (ResourceNotFoundException e) {
if (Log.isDebugModeEnabled()) {
Log.e(TAG, "XML resources could not be found!!!");
}
return;
}
button =
(CheckBox) TiApplication.getAppCurrentActivity().getLayoutInflater().inflate(buttonId, null);
button.addOnLayoutChangeListener(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)
{
TiUIHelper.firePostLayoutEvent(proxy);
}
});
case UIModule.SWITCH_STYLE_CHECKBOX:
if (!(currentButton instanceof MaterialCheckBox)) {
button = new MaterialCheckBox(activity);
}
break;

case UIModule.SWITCH_STYLE_TOGGLEBUTTON:
if (!(currentButton instanceof AppCompatToggleButton)) {
button = new AppCompatToggleButton(activity);
}
break;

case AndroidModule.SWITCH_STYLE_TOGGLEBUTTON:
if (!(currentButton instanceof ToggleButton)) {
button = new ToggleButton(proxy.getActivity()) {
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
super.onLayout(changed, left, top, right, bottom);
TiUIHelper.firePostLayoutEvent(proxy);
}
};
case UIModule.SWITCH_STYLE_SLIDER:
if (!(currentButton instanceof SwitchMaterial)) {
button = new SwitchMaterial(activity);
}
break;

case AndroidModule.SWITCH_STYLE_SWITCH:
if (!(currentButton instanceof SwitchCompat)) {
int buttonId;
try {
buttonId = TiRHelper.getResource("layout.titanium_ui_switchcompat");
} catch (ResourceNotFoundException e) {
if (Log.isDebugModeEnabled()) {
Log.e(TAG, "XML resources could not be found!!!");
}
return;
}
button = (SwitchCompat) TiApplication.getAppCurrentActivity().getLayoutInflater().inflate(buttonId,
null);
button.addOnLayoutChangeListener(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)
{
TiUIHelper.firePostLayoutEvent(proxy);
}
});
case UIModule.SWITCH_STYLE_CHIP:
if (!(currentButton instanceof Chip)) {
Chip chip = new Chip(activity);
chip.setCheckable(true);
button = chip;
}
break;

Expand All @@ -230,6 +213,7 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
if (button != null) {
setNativeView(button);
updateButton(button, proxy.getProperties());
button.addOnLayoutChangeListener(this.layoutListener);
button.setOnCheckedChangeListener(this);
}
}
Expand Down
15 changes: 10 additions & 5 deletions apidoc/Titanium/UI/Android/Android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -680,23 +680,28 @@ properties:
description: Use with the <Titanium.UI.Switch.style> property.
type: Number
permission: read-only
deprecated:
since: "10.0.0"
notes: Use <Titanium.UI.SWITCH_STYLE_CHECKBOX> instead.

- name: SWITCH_STYLE_TOGGLEBUTTON
summary: Display a toggle button.
description: Use with the <Titanium.UI.Switch.style> property.
type: Number
permission: read-only
deprecated:
since: "10.0.0"
notes: Use <Titanium.UI.SWITCH_STYLE_TOGGLEBUTTON> instead.

- name: SWITCH_STYLE_SWITCH
summary: Display a switch.
description: |
Use with the <Titanium.UI.Switch.style> property.
Due to [Android issue #78262](https://code.google.com/p/android/issues/detail?id=78262),
the thumb (circle icon) may not appear on the switch.
description: Use with the <Titanium.UI.Switch.style> property.
type: Number
permission: read-only
since: "4.0.0"
deprecated:
since: "10.0.0"
notes: Use <Titanium.UI.SWITCH_STYLE_SLIDER> instead.

- name: WEBVIEW_PLUGINS_OFF
summary: |
Expand Down
11 changes: 6 additions & 5 deletions apidoc/Titanium/UI/Switch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ properties:
- name: style
summary: Style of the switch.
description: |
Prior to Release 4.0, you can only specify either a toggle button or checkbox.
For Titanium versions older than 10.0.0, this is an Android only property and must be assigned
a `SWITCH_STYLE_*` constant from the <Titanium.UI.Android> module.
type: Number
constants: Titanium.UI.Android.SWITCH_STYLE_*
platforms: [android]
default: |
Titanium.UI.Android.SWITCH_STYLE_SWITCH; prior to Release 4.0, Titanium.UI.Android.SWITCH_STYLE_TOGGLEBUTTON
constants: Titanium.UI.SWITCH_STYLE_*
platforms: [android, iphone, ipad, macos]
since: {android: 0.8.0, iphone: 10.0.0, ipad: 10.0.0, macos: 10.0.0}
default: <Titanium.UI.SWITCH_STYLE_SLIDER>

- name: textAlign
summary: |
Expand Down

0 comments on commit 4295acc

Please sign in to comment.