From e1b3fb2eb177463fbde1e1b78037014c208b248f Mon Sep 17 00:00:00 2001 From: Rolandas Date: Mon, 29 Mar 2021 15:21:14 +0300 Subject: [PATCH 1/3] added additional line states --- .../src/main/java/com/chaos/view/PinView.java | 17 ++++++++++++++--- pinview/src/main/res/values/attrs.xml | 5 +++-- sample/src/main/res/color/line_colors.xml | 4 +++- sample/src/main/res/values/colors.xml | 2 ++ 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/pinview/src/main/java/com/chaos/view/PinView.java b/pinview/src/main/java/com/chaos/view/PinView.java index 9ff2d73..861ae5c 100644 --- a/pinview/src/main/java/com/chaos/view/PinView.java +++ b/pinview/src/main/java/com/chaos/view/PinView.java @@ -71,8 +71,9 @@ public class PinView extends AppCompatEditText { private static final InputFilter[] NO_FILTERS = new InputFilter[0]; - private static final int[] HIGHLIGHT_STATES = new int[]{ - android.R.attr.state_selected}; + private static final int[] HIGHLIGHT_STATES = new int[]{android.R.attr.state_selected}; + private static final int[] FILLED_STATES = new int[]{R.attr.state_filled}; + private static final int[] ERROR_STATES = new int[]{R.attr.state_error}; private static final int VIEW_TYPE_RECTANGLE = 0; private static final int VIEW_TYPE_LINE = 1; @@ -369,7 +370,16 @@ private void drawPinView(Canvas canvas) { int highlightIdx = getText().length(); for (int i = 0; i < mPinItemCount; i++) { boolean highlight = isFocused() && highlightIdx == i; - mPaint.setColor(highlight ? getLineColorForState(HIGHLIGHT_STATES) : mCurLineColor); + boolean filled = getText().length() > i; + if (getError() != null) { + mPaint.setColor(getLineColorForState(ERROR_STATES)); + } else { + if (highlight) { + mPaint.setColor(getLineColorForState(HIGHLIGHT_STATES)); + } else { + mPaint.setColor(filled ? getLineColorForState(FILLED_STATES) : mCurLineColor); + } + } updateItemRectF(i); updateCenterPoint(); @@ -901,6 +911,7 @@ public void setTextSize(int unit, float size) { } //region ItemBackground + /** * Set the item background to a given resource. The resource should refer to * a Drawable object or 0 to remove the item background. diff --git a/pinview/src/main/res/values/attrs.xml b/pinview/src/main/res/values/attrs.xml index 135d002..d09a937 100644 --- a/pinview/src/main/res/values/attrs.xml +++ b/pinview/src/main/res/values/attrs.xml @@ -1,5 +1,4 @@ - - - + + + diff --git a/sample/src/main/res/values/colors.xml b/sample/src/main/res/values/colors.xml index d0db3f4..2a3df55 100644 --- a/sample/src/main/res/values/colors.xml +++ b/sample/src/main/res/values/colors.xml @@ -27,4 +27,6 @@ #E91E63 #3F51B5 #C5CAE9 + #C2D422 + #FFDE1A From 90fdf189039a98b002158973795bcbee6ce43d56 Mon Sep 17 00:00:00 2001 From: Rolandas Date: Mon, 29 Mar 2021 15:57:21 +0300 Subject: [PATCH 2/3] modified error state --- pinview/src/main/java/com/chaos/view/PinView.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pinview/src/main/java/com/chaos/view/PinView.java b/pinview/src/main/java/com/chaos/view/PinView.java index 861ae5c..62ddbe9 100644 --- a/pinview/src/main/java/com/chaos/view/PinView.java +++ b/pinview/src/main/java/com/chaos/view/PinView.java @@ -104,6 +104,7 @@ public class PinView extends AppCompatEditText { private ValueAnimator mDefaultAddAnimator; private boolean isAnimationEnable = false; private boolean isPasswordHidden; + private boolean isError; private Blink mBlink; private boolean isCursorVisible; @@ -198,6 +199,11 @@ public void setPasswordHidden(boolean hidden) { requestLayout(); } + public void setError(boolean error) { + isError = error; + requestLayout(); + } + /** * new behavior: reveal or hide the pins programmatically * @@ -370,8 +376,8 @@ private void drawPinView(Canvas canvas) { int highlightIdx = getText().length(); for (int i = 0; i < mPinItemCount; i++) { boolean highlight = isFocused() && highlightIdx == i; - boolean filled = getText().length() > i; - if (getError() != null) { + boolean filled = highlightIdx > i; + if (isError) { mPaint.setColor(getLineColorForState(ERROR_STATES)); } else { if (highlight) { From 7e29173f9e0ba1a11b57dfce4c744d7a436cf016 Mon Sep 17 00:00:00 2001 From: Rolandas Date: Mon, 29 Mar 2021 16:08:04 +0300 Subject: [PATCH 3/3] added documentation about error state --- README.md | 1 + pinview/src/main/java/com/chaos/view/PinView.java | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/README.md b/README.md index ecead47..49ef29c 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ pinView.setItemBackground(getResources().getDrawable(R.drawable.item_background) pinView.setItemBackgroundResources(R.drawable.item_background); pinView.setHideLineWhenFilled(false); pinView.setPasswordHidden(false); +pinView.setError(false); pinView.setTransformationMethod(new PasswordTransformationMethod()); ``` diff --git a/pinview/src/main/java/com/chaos/view/PinView.java b/pinview/src/main/java/com/chaos/view/PinView.java index 62ddbe9..bc4f2ff 100644 --- a/pinview/src/main/java/com/chaos/view/PinView.java +++ b/pinview/src/main/java/com/chaos/view/PinView.java @@ -199,6 +199,11 @@ public void setPasswordHidden(boolean hidden) { requestLayout(); } + /** + * Set error state for the PinView + * + * @param error True to enable the error state, false to remove the error state + */ public void setError(boolean error) { isError = error; requestLayout();