Skip to content

Commit

Permalink
[Refactor] Better naming attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaosLeung committed Jun 21, 2017
1 parent d894c84 commit 6d712f8
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 72 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
// NOTE: Do not place your application dependencies here; they belong
Expand Down
204 changes: 149 additions & 55 deletions pinview/src/main/java/com/chaos/view/PinView.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,7 @@
*
* @author Chaos Leong
* 01/04/2017
*
* <p>
* <b>XML attributes</b>
* <p>
* See <a href="https://developer.android.com/reference/android/R.styleable.html#EditText">EditText Attributes</a>,
* <a href="https://developer.android.com/reference/android/R.styleable.html#TextView">TextView Attributes</a>,
* <a href="https://developer.android.com/reference/android/R.styleable.html#View">View Attributes</a>
* @attr ref R.styleable#PinView_boxCount
* @attr ref R.styleable#PinView_boxHeight
* @attr ref R.styleable#PinView_boxRadius
* @attr ref R.styleable#PinView_boxMargin
* @attr ref R.styleable#PinView_borderWidth
* @attr ref R.styleable#PinView_borderColor
*/

public class PinView extends AppCompatEditText {

private static final String TAG = "PinView";
Expand All @@ -70,11 +56,11 @@ public class PinView extends AppCompatEditText {

private static final InputFilter[] NO_FILTERS = new InputFilter[0];

private int mPinBoxCount;
private int mPinItemCount;

private float mPinBoxHeight;
private int mPinBoxRadius;
private int mPinBoxMargin;
private float mPinItemSize;
private int mPinItemRadius;
private int mPinItemSpacing;

private final Paint mPaint;
private final TextPaint mTextPaint;
Expand Down Expand Up @@ -119,20 +105,37 @@ public PinView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)

TypedArray a = theme.obtainStyledAttributes(attrs, R.styleable.PinView, defStyleAttr, 0);

mPinBoxCount = a.getInt(R.styleable.PinView_boxCount, DEFAULT_COUNT);
mPinBoxHeight = a.getDimensionPixelSize(R.styleable.PinView_boxHeight,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_box_height));
mPinBoxMargin = a.getDimensionPixelOffset(R.styleable.PinView_boxMargin,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_box_margin));
mPinItemCount = a.getInt(R.styleable.PinView_boxCount, DEFAULT_COUNT);
mPinItemSize = a.getDimensionPixelSize(R.styleable.PinView_boxHeight,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_size));
mPinItemSpacing = a.getDimensionPixelOffset(R.styleable.PinView_boxMargin,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_spacing));
mPinItemRadius = a.getDimensionPixelOffset(R.styleable.PinView_boxRadius,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_radius));

if (a.hasValue(R.styleable.PinView_itemCount)) {
mPinItemCount = a.getInt(R.styleable.PinView_itemCount, DEFAULT_COUNT);
}
if (a.hasValue(R.styleable.PinView_itemSize)) {
mPinItemSize = a.getDimensionPixelSize(R.styleable.PinView_itemSize,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_size));
}
if (a.hasValue(R.styleable.PinView_itemSpacing)) {
mPinItemSpacing = a.getDimensionPixelOffset(R.styleable.PinView_itemSpacing,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_spacing));
}
if (a.hasValue(R.styleable.PinView_itemRadius)) {
mPinItemRadius = a.getDimensionPixelOffset(R.styleable.PinView_itemRadius,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_radius));
}

mBorderWidth = a.getDimensionPixelOffset(R.styleable.PinView_borderWidth,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_box_border_width));
mPinBoxRadius = a.getDimensionPixelOffset(R.styleable.PinView_boxRadius,
res.getDimensionPixelOffset(R.dimen.pv_pin_view_box_radius));
res.getDimensionPixelOffset(R.dimen.pv_pin_view_item_border_width));
mBorderColor = a.getColorStateList(R.styleable.PinView_borderColor);

a.recycle();

setMaxLength(mPinBoxCount);
setMaxLength(mPinItemCount);
mPaint.setStrokeWidth(mBorderWidth);
setupAnimator();

Expand Down Expand Up @@ -174,16 +177,16 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width;
int height;

float boxHeight = mPinBoxHeight;
float boxHeight = mPinItemSize;

if (widthMode == MeasureSpec.EXACTLY) {
// Parent has told us how big to be. So be it.
width = widthSize;
} else {
float boxesWidth = (mPinBoxCount - 1) * mPinBoxMargin + mPinBoxCount * boxHeight;
float boxesWidth = (mPinItemCount - 1) * mPinItemSpacing + mPinItemCount * boxHeight;
width = Math.round(boxesWidth + getPaddingRight() + getPaddingLeft());
if (mPinBoxMargin == 0) {
width -= 2 * (mPinBoxCount - 1) * mBorderWidth;
if (mPinItemSpacing == 0) {
width -= 2 * (mPinItemCount - 1) * mBorderWidth;
}
}

Expand Down Expand Up @@ -264,7 +267,7 @@ private void updatePaints() {
}

private void drawPinView(Canvas canvas) {
for (int i = 0; i < mPinBoxCount; i++) {
for (int i = 0; i < mPinItemCount; i++) {
updateBoxRectF(i);
updateCenterPoint();

Expand All @@ -280,7 +283,7 @@ private void drawPinView(Canvas canvas) {
} else {
drawText(canvas, i);
}
} else if (!TextUtils.isEmpty(getHint()) && getHint().length() == mPinBoxCount) {
} else if (!TextUtils.isEmpty(getHint()) && getHint().length() == mPinItemCount) {
drawHint(canvas, i);
}
}
Expand All @@ -289,12 +292,12 @@ private void drawPinView(Canvas canvas) {
private void drawPinBox(Canvas canvas, int i) {
boolean l, r;
l = r = true;
if (mPinBoxMargin == 0) {
if (mPinBoxCount > 1) {
if (mPinItemSpacing == 0) {
if (mPinItemCount > 1) {
if (i == 0) {
// draw only left round
r = false;
} else if (i == mPinBoxCount - 1) {
} else if (i == mPinItemCount - 1) {
// draw only right round
l = false;
} else {
Expand All @@ -303,7 +306,7 @@ private void drawPinBox(Canvas canvas, int i) {
}
}
}
updateRoundRectPath(mBoxBorderRect, mPinBoxRadius, mPinBoxRadius, l, r);
updateRoundRectPath(mBoxBorderRect, mPinItemRadius, mPinItemRadius, l, r);
canvas.drawPath(mPath, mPaint);
}

Expand Down Expand Up @@ -368,17 +371,17 @@ private void updateRoundRectPath(RectF rectF, float rx, float ry,
}

private void updateBoxRectF(int i) {
float startX = (getWidth() - (mPinBoxCount - 1) * mPinBoxMargin - mPinBoxCount * mPinBoxHeight) / 2;
if (mPinBoxMargin == 0) {
startX += (mPinBoxCount - 1) * mBorderWidth;
float startX = (getWidth() - (mPinItemCount - 1) * mPinItemSpacing - mPinItemCount * mPinItemSize) / 2;
if (mPinItemSpacing == 0) {
startX += (mPinItemCount - 1) * mBorderWidth;
}
float left = startX + mPinBoxHeight * i + mPinBoxMargin * i + mBorderWidth;
if (mPinBoxMargin == 0 && i > 0) {
float left = startX + mPinItemSize * i + mPinItemSpacing * i + mBorderWidth;
if (mPinItemSpacing == 0 && i > 0) {
left = left - 2 * mBorderWidth * i;
}
float right = left + mPinBoxHeight - 2 * mBorderWidth;
float right = left + mPinItemSize - 2 * mBorderWidth;
float top = mBorderWidth + getPaddingTop();
float bottom = top + mPinBoxHeight - 2 * mBorderWidth;
float bottom = top + mPinItemSize - 2 * mBorderWidth;

mBoxBorderRect.set(left, top, right, bottom);
}
Expand Down Expand Up @@ -570,76 +573,167 @@ public int getBorderWidth() {
*
* @attr ref R.styleable#PinView_boxCount
* @see #getBoxCount()
* @deprecated Use {@link #setItemCount(int)} instead.
*/
@Deprecated
public void setBoxCount(int len) {
mPinBoxCount = len;
setMaxLength(len);
requestLayout();
setItemCount(len);
}

/**
* @return Returns the count of the boxes.
* @see #setBoxCount(int)
* @deprecated Use {@link #getItemCount()} instead.
*/
@Deprecated
public int getBoxCount() {
return mPinBoxCount;
return getItemCount();
}

/**
* Sets the count of items.
*
* @attr ref R.styleable#PinView_itemCount
* @see #getItemCount()
*/
public void setItemCount(int count) {
mPinItemCount = count;
setMaxLength(count);
requestLayout();
}

/**
* @return Returns the count of items.
* @see #setItemCount(int)
*/
public int getItemCount() {
return mPinItemCount;
}

/**
* Sets the radius of box's border.
*
* @attr ref R.styleable#PinView_boxRadius
* @see #getBoxRadius()
* @deprecated Use {@link #setItemRadius(int)} instead.
*/
@Deprecated
public void setBoxRadius(@Px int pinBoxRadius) {
mPinBoxRadius = pinBoxRadius;
setItemRadius(pinBoxRadius);
}

/**
* @return Returns the radius of boxes's border.
* @see #setBoxRadius(int)
* @deprecated Use {@link #getItemRadius()} instead.
*/
@Px
@Deprecated
public int getBoxRadius() {
return mPinBoxRadius;
return getItemRadius();
}

/**
* Sets the radius of square.
*
* @attr ref R.styleable#PinView_itemRadius
* @see #getItemRadius()
*/
public void setItemRadius(@Px int itemRadius) {
mPinItemRadius = itemRadius;
}

/**
* @return Returns the radius of square.
* @see #setItemRadius(int)
*/
@Px
public int getItemRadius() {
return mPinItemRadius;
}

/**
* Specifies extra space between the boxes.
*
* @attr ref R.styleable#PinView_boxMargin
* @see #getBoxMargin()
* @deprecated Use {@link #setItemSpacing(int)} instead.
*/
@Deprecated
public void setBoxMargin(@Px int pinBoxMargin) {
mPinBoxMargin = pinBoxMargin;
requestLayout();
setItemSpacing(pinBoxMargin);
}

/**
* @return Returns the margin between of the boxes.
* @see #setBoxMargin(int)
* @deprecated Use {@link #getItemSpacing()} instead.
*/
@Px
@Deprecated
public int getBoxMargin() {
return mPinBoxMargin;
return getItemSpacing();
}


/**
* Specifies extra space between two items.
*
* @attr ref R.styleable#PinView_itemSpacing
* @see #getItemSpacing()
*/
public void setItemSpacing(@Px int itemSpacing) {
mPinItemSpacing = itemSpacing;
requestLayout();
}

/**
* @return Returns the spacing between two items.
* @see #setItemSpacing(int)
*/
@Px
public int getItemSpacing() {
return mPinItemSpacing;
}

/**
* Sets the height and width of box.
*
* @attr ref R.styleable#PinView_boxHeight
* @see #getBoxHeight()
* @deprecated Use {@link #setItemSize(float)} instead.
*/
@Deprecated
public void setBoxHeight(float boxHeight) {
mPinBoxHeight = boxHeight;
setItemSize(boxHeight);
}

/**
* @return Returns the height of box.
* @see #setBoxHeight(float)
* @deprecated Use {@link #getItemSize()} instead.
*/
@Deprecated
public float getBoxHeight() {
return mPinBoxHeight;
return getItemSize();
}

/**
* Sets the height and width of item.
*
* @attr ref R.styleable#PinView_itemSize
* @see #getItemSize()
*/
public void setItemSize(float itemSize) {
mPinItemSize = itemSize;
}

/**
* @return Returns the size of item.
* @see #setBoxHeight(float)
*/
public float getItemSize() {
return mPinItemSize;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions pinview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@
</declare-styleable>

<declare-styleable name="PinView">
<!-- {@deprecated Use itemCount instead.} -->
<attr name="boxCount" format="integer" />
<!-- {@deprecated Use itemSize instead.} -->
<attr name="boxHeight" format="dimension" />
<!-- {@deprecated Use itemRadius instead.} -->
<attr name="boxRadius" format="dimension" />
<!-- {@deprecated Use itemSpacing instead.} -->
<attr name="boxMargin" format="dimension" />
<attr name="itemCount" format="integer" />
<attr name="itemSize" format="dimension" />
<attr name="itemRadius" format="dimension" />
<attr name="itemSpacing" format="dimension" />
<attr name="borderWidth" format="dimension" />
<attr name="borderColor" format="reference|color" />
</declare-styleable>
Expand Down
8 changes: 4 additions & 4 deletions pinview/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
-->

<resources>
<dimen name="pv_pin_view_box_height">48dp</dimen>
<dimen name="pv_pin_view_box_radius">5dp</dimen>
<dimen name="pv_pin_view_box_margin">5dp</dimen>
<dimen name="pv_pin_view_box_border_width">2dp</dimen>
<dimen name="pv_pin_view_item_size">48dp</dimen>
<dimen name="pv_pin_view_item_radius">5dp</dimen>
<dimen name="pv_pin_view_item_spacing">5dp</dimen>
<dimen name="pv_pin_view_item_border_width">2dp</dimen>
</resources>
Loading

0 comments on commit 6d712f8

Please sign in to comment.