Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added error and filled states for pin view lines #60

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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());
```

Expand Down
28 changes: 25 additions & 3 deletions pinview/src/main/java/com/chaos/view/PinView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,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;
Expand Down Expand Up @@ -197,6 +199,16 @@ 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();
}

/**
* new behavior: reveal or hide the pins programmatically
*
Expand Down Expand Up @@ -369,7 +381,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 = highlightIdx > i;
if (isError) {
mPaint.setColor(getLineColorForState(ERROR_STATES));
} else {
if (highlight) {
mPaint.setColor(getLineColorForState(HIGHLIGHT_STATES));
} else {
mPaint.setColor(filled ? getLineColorForState(FILLED_STATES) : mCurLineColor);
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use a local variable to save the color, so we only need to write setColor once.

Like:

color = mCurLineColor;
if (isError) {
    color = getLineColorForState(ERROR_STATES);
} else if (highlight) {
    color = getLineColorForState(HIGHLIGHT_STATES);
} else if (filled) {
    color = getLineColorForState(FILLED_STATES);
}
mPaint.setColor(color);


updateItemRectF(i);
updateCenterPoint();
Expand Down Expand Up @@ -901,6 +922,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.
Expand Down
5 changes: 3 additions & 2 deletions pinview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copyright must start on a new line.

~ Copyright 2017 Chaos Leong
~
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -38,5 +37,7 @@
<attr name="cursorColor" format="reference|color" />
<attr name="android:itemBackground" />
<attr name="hideLineWhenFilled" format="boolean" />
<attr name="state_filled" format="boolean" />
<attr name="state_error" format="boolean" />
</declare-styleable>
</resources>
4 changes: 3 additions & 1 deletion sample/src/main/res/color/line_colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
~ limitations under the License.
-->

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:color="@color/line_error" app:state_error="true" />
<item android:color="@color/line_filled" app:state_filled="true" />
<item android:color="@color/line_selected" android:state_selected="true" />
<item android:color="@color/line_focused" android:state_focused="true" />
<item android:color="@color/line_default" />
Expand Down
2 changes: 2 additions & 0 deletions sample/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@
<color name="line_selected">#E91E63</color>
<color name="line_focused">#3F51B5</color>
<color name="line_default">#C5CAE9</color>
<color name="line_filled">#C2D422</color>
<color name="line_error">#FFDE1A</color>
</resources>