Skip to content

Commit

Permalink
[TIMOB-24639] Refactor TextInputLayout implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Mathews committed Aug 30, 2017
1 parent 3443ff4 commit 42d72fc
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
TiC.PROPERTY_FULLSCREEN,
TiC.PROPERTY_HINT_TEXT,
TiC.PROPERTY_HINT_TEXT_COLOR,
TiC.PROPERTY_HINT_TYPE,
TiC.PROPERTY_KEYBOARD_TYPE,
TiC.PROPERTY_MAX_LENGTH,
TiC.PROPERTY_PASSWORD_MASK,
Expand All @@ -56,6 +57,7 @@ public TextAreaProxy()
defaultValues.put(TiC.PROPERTY_VALUE, "");
defaultValues.put(TiC.PROPERTY_MAX_LENGTH, -1);
defaultValues.put(TiC.PROPERTY_FULLSCREEN, true);
defaultValues.put(TiC.PROPERTY_HINT_TYPE, UIModule.HINT_TYPE_STATIC);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import ti.modules.titanium.ui.UIModule;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Build;
Expand All @@ -47,7 +48,6 @@
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
Expand Down Expand Up @@ -137,7 +137,8 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
}

textInputLayout = new TextInputLayout(proxy.getActivity());
textInputLayout.addView(tv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textInputLayout.addView(tv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

setNativeView(textInputLayout);
}

Expand All @@ -162,19 +163,19 @@ public void processProperties(KrollDict d)
tv.setText("");
}

if (d.containsKey(TiC.PROPERTY_BACKGROUND_COLOR)) {
tv.setBackgroundColor(Color.TRANSPARENT);
}

if (d.containsKey(TiC.PROPERTY_COLOR)) {
tv.setTextColor(TiConvert.toColor(d, TiC.PROPERTY_COLOR));
}

if (d.containsKey(TiC.PROPERTY_HINT_TEXT)) {
String hintText = d.getString(TiC.PROPERTY_HINT_TEXT);
if (d.containsKey(TiC.PROPERTY_HINT_TEXT) || d.containsKey(TiC.PROPERTY_HINT_TYPE)) {
String hintText = TiConvert.toString(d.get(TiC.PROPERTY_HINT_TEXT), "");
if (hintText != null) {
int type = TiConvert.toInt(d.get(TiC.PROPERTY_HINT_TYPE), UIModule.HINT_TYPE_STATIC);
if (type == UIModule.HINT_TYPE_STATIC) {
tv.setHint(hintText);
} else if (type == UIModule.HINT_TYPE_ANIMATED) {
textInputLayout.setHint(hintText);
}
setHintText(type, hintText);
}
}

Expand Down Expand Up @@ -248,10 +249,10 @@ public void processProperties(KrollDict d)

private void setTextPadding(HashMap<String, Object> d)
{
int paddingLeft = tv.getPaddingLeft();
int paddingRight = tv.getPaddingRight();
int paddingTop = tv.getPaddingTop();
int paddingBottom = tv.getPaddingBottom();
int paddingLeft = textInputLayout.getPaddingLeft();
int paddingRight = textInputLayout.getPaddingRight();
int paddingTop = textInputLayout.getPaddingTop();
int paddingBottom = textInputLayout.getPaddingBottom();

if (d.containsKey(TiC.PROPERTY_LEFT)) {
paddingLeft = TiConvert.toInt(d.get(TiC.PROPERTY_LEFT), 0);
Expand All @@ -269,7 +270,7 @@ private void setTextPadding(HashMap<String, Object> d)
paddingBottom = TiConvert.toInt(d.get(TiC.PROPERTY_BOTTOM), 0);
}

tv.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
textInputLayout.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
}

@Override
Expand All @@ -295,22 +296,24 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
tv.setText(truncateText);
tv.setSelection(cursor);
}
} else if (key.equals(TiC.PROPERTY_BACKGROUND_COLOR)) {
tv.setBackgroundColor(Color.TRANSPARENT);
} else if (key.equals(TiC.PROPERTY_COLOR)) {
tv.setTextColor(TiConvert.toColor((String) newValue));
} else if (key.equals(TiC.PROPERTY_HINT_TEXT)) {
tv.setHint(TiConvert.toString(newValue));
int type = proxy.getProperties().getInt(TiC.PROPERTY_HINT_TYPE);
setHintText(type, TiConvert.toString(newValue));
} else if (key.equals(TiC.PROPERTY_HINT_TEXT_COLOR)) {
tv.setHintTextColor(TiConvert.toColor((String) newValue));
} else if (key.equals(TiC.PROPERTY_HINT_TYPE)) {
String hintText = TiConvert.toString(proxy.getProperty(TiC.PROPERTY_HINT_TEXT));
if (hintText != null) {
int type = TiConvert.toInt(newValue);
if (type == UIModule.HINT_TYPE_STATIC) {
textInputLayout.setHint("");
tv.setHint(hintText);
} else if (type == UIModule.HINT_TYPE_ANIMATED) {
tv.setHint("");
textInputLayout.setHint(hintText);
Object attributedHintText = d.get(TiC.PROPERTY_ATTRIBUTED_HINT_TEXT);
if (attributedHintText instanceof AttributedStringProxy) {
setAttributedStringHint((AttributedStringProxy) attributedHintText);
} else {
String hintText = d.getString(TiC.PROPERTY_HINT_TEXT);
if (hintText != null) {
int type = TiConvert.toInt(newValue);
setHintText(type, hintText);
}
}
} else if (key.equals(TiC.PROPERTY_ELLIPSIZE)) {
Expand Down Expand Up @@ -422,13 +425,13 @@ public void onTextChanged(CharSequence s, int start, int before, int count)
public void focus()
{
super.focus();
if (nativeView != null) {
if (tv != null) {
if (proxy.hasProperty(TiC.PROPERTY_EDITABLE)
&& !(TiConvert.toBoolean(proxy.getProperty(TiC.PROPERTY_EDITABLE)))) {
TiUIHelper.showSoftKeyboard(nativeView, false);
TiUIHelper.showSoftKeyboard(tv, false);
}
else {
TiUIHelper.requestSoftInputChange(proxy, nativeView);
TiUIHelper.requestSoftInputChange(proxy, tv);
}
}
}
Expand All @@ -439,11 +442,11 @@ public void onFocusChange(View v, boolean hasFocus)
if (hasFocus) {
Boolean clearOnEdit = (Boolean) proxy.getProperty(TiC.PROPERTY_CLEAR_ON_EDIT);
if (clearOnEdit != null && clearOnEdit) {
((EditText) nativeView).setText("");
tv.setText("");
}
Rect r = new Rect();
nativeView.getFocusedRect(r);
nativeView.requestRectangleOnScreen(r);
tv.getFocusedRect(r);
tv.requestRectangleOnScreen(r);

}
super.onFocusChange(v, hasFocus);
Expand Down Expand Up @@ -805,7 +808,20 @@ public void setAttributedStringText(AttributedStringProxy attrString) {
public void setAttributedStringHint(AttributedStringProxy attrString) {
Spannable spannableText = AttributedStringProxy.toSpannable(attrString, TiApplication.getAppCurrentActivity());
if (spannableText != null) {
tv.setHint(spannableText);
int type = getProxy().getProperties().getInt(TiC.PROPERTY_HINT_TYPE);
setHintText(type, spannableText);
}
}

public void setHintText(int type, CharSequence hintText) {
if (type == UIModule.HINT_TYPE_STATIC) {
textInputLayout.setHint("");
textInputLayout.setHintEnabled(false);
tv.setHint(hintText);
} else if (type == UIModule.HINT_TYPE_ANIMATED) {
tv.setHint("");
textInputLayout.setHint(hintText);
textInputLayout.setHintEnabled(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import ti.modules.titanium.ui.widget.TiUIText;
import android.graphics.drawable.Drawable;
import android.support.design.widget.TextInputLayout;
import android.support.v4.widget.NestedScrollView;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.KeyEvent;
Expand Down Expand Up @@ -48,11 +49,12 @@ public TiUISearchBar(final TiViewProxy proxy)

View nativeView = getNativeView();
if (nativeView instanceof EditText) {
this.tv = (EditText)nativeView;
this.tv = (EditText) nativeView;
} else if (nativeView instanceof TextInputLayout) {
this.tv = ((TextInputLayout)nativeView).getEditText();
} else {
throw new IllegalStateException();
this.tv = ((TextInputLayout) nativeView).getEditText();
}
if (this.tv == null) {
throw new Error("could not obtain EditText component");
}

this.tv.setImeOptions(EditorInfo.IME_ACTION_DONE);
Expand Down
65 changes: 64 additions & 1 deletion apidoc/Titanium/UI/TextArea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,53 @@ properties:
default: <Titanium.UI.KEYBOARD_APPEARANCE_DEFAULT>
platforms: [iphone, ipad]

- name: attributedHintText
summary: Hint text attributed string.
description: |
The attributed hint text by the text area. If set, avoid setting common attributes
in text area, such as `hintText`, `color` and `font`, as unexpected behaviors may result.
This has no effect when used with `hintType` Ti.UI.HINT_TYPE_ANIMATED.
Prior to Release 3.6.0, assign this property an object from the
<Titanium.UI.iOS.AttributedString> class.
Since Appcelerator CLI 4.1.0 (Alloy 1.7.0), for Alloy, you can use an `<AttributedHintText>`
element inside a `<TextField>` element and set the text property as node text:
<Alloy>
<Window>
<TextField>
<AttributedHintText>
Alloy is great!
</AttributedHintText>
</TextField>
</Window>
</Alloy>
Then set attributes in the TSS file:
"AttributedString" : {
attributes: [
{
type: Ti.UI.ATTRIBUTE_FOREGROUND_COLOR,
value: 'red',
range: [0, 5]
},
{
type: Ti.UI.ATTRIBUTE_UNDERLINES_STYLE,
value: Ti.UI.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
range: [9, 5]
}
]
}
type: Titanium.UI.AttributedString
platforms: [android, iphone, ipad]
since:
android: 3.6.0
iphone: 3.2.0
ipad: 3.2.0

- name: attributedString
summary: TextArea attributed string.
description: |
Expand Down Expand Up @@ -247,9 +294,22 @@ properties:
</android>
Another way to change the hint text color is to use this property and specify a color.
This has no effect when used with `hintType` Ti.UI.HINT_TYPE_ANIMATED. Instead a theme must be
used that defines the `android:textColorHint` attribute.
type: String
default: Default Android theme's hint text color.

- name: hintType
summary: Hint type to display on the text field.
platforms: [android]
since: {android: "6.2.0"}
description: |
Setting this to <Titanium.UI.HINT_TYPE_ANIMATED> will use the animated TextInputLayout on Android.
type: Number
constants: [Titanium.UI.HINT_TYPE_*]
default: <Titanium.UI.HINT_TYPE_STATIC>

- name: handleLinks
summary: Specifies if the text area should allow user interaction with the given URL in the given range of text.
description: |
Expand Down Expand Up @@ -346,7 +406,10 @@ properties:
platforms: [iphone, ipad]

- name: textAlign
summary: Text alignment within this text area.
summary: |
Text alignment within this text area.
This has no effect on `hintText` when `hintType` is Ti.UI.HINT_TYPE_ANIMATED.
type: [String, Number]
constants: Titanium.UI.TEXT_ALIGNMENT_*
default: <Titanium.UI.TEXT_ALIGNMENT_LEFT>
Expand Down
10 changes: 9 additions & 1 deletion apidoc/Titanium/UI/TextField.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ properties:
The attributed hint text by the textField. If set, avoid setting common attributes
in textField, such as `hintText`, `color` and `font`, as unexpected behaviors may result.
This has no effect when used with `hintType` Ti.UI.HINT_TYPE_ANIMATED.
Prior to Release 3.6.0, assign this property an object from the
<Titanium.UI.iOS.AttributedString> class.
Expand Down Expand Up @@ -300,6 +302,9 @@ properties:
by the <Titanium.UI.TextField.attributedHintText> which provides an advanced configuration to
style hint texts.
This has no effect when used with `hintType` Ti.UI.HINT_TYPE_ANIMATED. Instead a theme must be
used that defines the `android:textColorHint` attribute.
*Note for Android*:
The hint text color in Android is determined by the theme of the application. By default, the
theme is 'Theme.AppCompat' which is a Dark theme. When you create the background to be white,
Expand Down Expand Up @@ -545,7 +550,10 @@ properties:
since: 5.0.0

- name: textAlign
summary: Text alignment within this text field.
summary: |
Text alignment within this text field.
This has no effect on `hintText` when `hintType` is Ti.UI.HINT_TYPE_ANIMATED.
type: [String, Number]
constants: Titanium.UI.TEXT_ALIGNMENT_*
default: <Titanium.UI.TEXT_ALIGNMENT_LEFT>
Expand Down

0 comments on commit 42d72fc

Please sign in to comment.