From 98842a6bbc87646f79cebd59f65377670e6806e6 Mon Sep 17 00:00:00 2001 From: Elena Bobkova Date: Fri, 3 Mar 2017 16:22:01 +0300 Subject: [PATCH 1/4] added input type check for edit text --- .../components/views/TypefacedEditText.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java b/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java index 86db412..080613b 100644 --- a/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java +++ b/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java @@ -25,6 +25,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.widget.AppCompatEditText; import android.text.Editable; +import android.text.InputType; import android.text.TextUtils; import android.text.TextWatcher; import android.text.method.SingleLineTransformationMethod; @@ -160,6 +161,12 @@ public class TypefacedEditText extends AppCompatEditText { "textSize required parameter. If it's dynamic then use '0sp'"); AttributesUtils.checkAttribute(typedArray, errors, AttributesUtils.getField(androidRes, "TextView_inputType"), true, "inputType required parameter"); + + final int inputType = typedArray.getInt(AttributesUtils.getField(androidRes, "TextView_inputType"), -1); + if (inputType == InputType.TYPE_CLASS_NUMBER || inputType == InputType.TYPE_DATETIME_VARIATION_NORMAL) { + errors.add("use inputType phone instead of number"); + } + AttributesUtils.checkAttribute(typedArray, errors, AttributesUtils.getField(androidRes, "TextView_imeOptions"), true, "imeOptions required parameter"); } @@ -299,6 +306,14 @@ public class TypefacedEditText extends AppCompatEditText { Lc.assertion(new IllegalStateException(AttributesUtils.viewError(this, "Do not specify ellipsize for EditText"))); } + @Override + public void setInputType(final int type) { + if (type == InputType.TYPE_CLASS_NUMBER || type == InputType.TYPE_NUMBER_VARIATION_NORMAL) { + Lc.assertion(new IllegalStateException(AttributesUtils.viewError(this, "Do not specify number InputType for EditText, use phone instead"))); + } + super.setInputType(type); + } + /** * Sets typeface from 'assets/fonts' folder by name. * From cbaf06ca3d36e1e1f2129efaa0fd29e1f37e57b8 Mon Sep 17 00:00:00 2001 From: Elena Bobkova Date: Mon, 6 Mar 2017 19:20:01 +0300 Subject: [PATCH 2/4] refactored --- .../roboswag/components/views/TypefacedEditText.java | 8 ++++---- .../components/views/internal/AttributesUtils.java | 11 +++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java b/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java index 080613b..ff6f573 100644 --- a/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java +++ b/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java @@ -25,7 +25,6 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.widget.AppCompatEditText; import android.text.Editable; -import android.text.InputType; import android.text.TextUtils; import android.text.TextWatcher; import android.text.method.SingleLineTransformationMethod; @@ -163,7 +162,7 @@ public class TypefacedEditText extends AppCompatEditText { "inputType required parameter"); final int inputType = typedArray.getInt(AttributesUtils.getField(androidRes, "TextView_inputType"), -1); - if (inputType == InputType.TYPE_CLASS_NUMBER || inputType == InputType.TYPE_DATETIME_VARIATION_NORMAL) { + if (AttributesUtils.isNumberInputType(inputType)) { errors.add("use inputType phone instead of number"); } @@ -308,8 +307,9 @@ public class TypefacedEditText extends AppCompatEditText { @Override public void setInputType(final int type) { - if (type == InputType.TYPE_CLASS_NUMBER || type == InputType.TYPE_NUMBER_VARIATION_NORMAL) { - Lc.assertion(new IllegalStateException(AttributesUtils.viewError(this, "Do not specify number InputType for EditText, use phone instead"))); + if (AttributesUtils.isNumberInputType(type)) { + Lc.assertion(new IllegalStateException(AttributesUtils.viewError(this, + "Do not specify number InputType for EditText, use phone instead"))); } super.setInputType(type); } diff --git a/src/main/java/ru/touchin/roboswag/components/views/internal/AttributesUtils.java b/src/main/java/ru/touchin/roboswag/components/views/internal/AttributesUtils.java index a001fc8..e1e8bbe 100644 --- a/src/main/java/ru/touchin/roboswag/components/views/internal/AttributesUtils.java +++ b/src/main/java/ru/touchin/roboswag/components/views/internal/AttributesUtils.java @@ -23,6 +23,7 @@ import android.content.Context; import android.content.res.TypedArray; import android.support.annotation.NonNull; import android.support.annotation.StyleableRes; +import android.text.InputType; import android.text.TextUtils; import android.util.AttributeSet; import android.view.View; @@ -149,6 +150,16 @@ public final class AttributesUtils { return "Errors for view id=" + UiUtils.OfViews.getViewIdString(view) + ":\n" + errorText; } + /** + * Returns true if input type equals number input type. + * + * @param inputType Input type to check; + * @return true if input type equals number input type. + */ + public static boolean isNumberInputType(final int inputType) { + return inputType == InputType.TYPE_CLASS_NUMBER || inputType == InputType.TYPE_DATETIME_VARIATION_NORMAL; + } + private AttributesUtils() { } From 08c3e73a28a1302925f5920477316c7f7050c1d9 Mon Sep 17 00:00:00 2001 From: Elena Bobkova Date: Mon, 6 Mar 2017 20:25:02 +0300 Subject: [PATCH 3/4] set Phone type --- .../touchin/roboswag/components/views/TypefacedEditText.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java b/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java index ff6f573..821a824 100644 --- a/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java +++ b/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java @@ -25,6 +25,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.widget.AppCompatEditText; import android.text.Editable; +import android.text.InputType; import android.text.TextUtils; import android.text.TextWatcher; import android.text.method.SingleLineTransformationMethod; @@ -308,8 +309,8 @@ public class TypefacedEditText extends AppCompatEditText { @Override public void setInputType(final int type) { if (AttributesUtils.isNumberInputType(type)) { - Lc.assertion(new IllegalStateException(AttributesUtils.viewError(this, - "Do not specify number InputType for EditText, use phone instead"))); + super.setInputType(InputType.TYPE_CLASS_PHONE); + return; } super.setInputType(type); } From 78b244897f028c5f5a7df48ce4eafb1057f3465c Mon Sep 17 00:00:00 2001 From: Elena Bobkova Date: Mon, 6 Mar 2017 20:29:56 +0300 Subject: [PATCH 4/4] added assertion --- .../ru/touchin/roboswag/components/views/TypefacedEditText.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java b/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java index 821a824..33c8493 100644 --- a/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java +++ b/src/main/java/ru/touchin/roboswag/components/views/TypefacedEditText.java @@ -309,6 +309,8 @@ public class TypefacedEditText extends AppCompatEditText { @Override public void setInputType(final int type) { if (AttributesUtils.isNumberInputType(type)) { + Lc.assertion(new IllegalStateException(AttributesUtils.viewError(this, + "Do not specify number InputType for EditText, use phone instead"))); super.setInputType(InputType.TYPE_CLASS_PHONE); return; }