Skip to content

Commit 87c4462

Browse files
author
zagum
committed
code refactoring
1 parent 59d68c8 commit 87c4462

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

expandicon/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
apply plugin: 'com.android.library'
22

3+
dependencies {
4+
compile 'com.android.support:support-annotations:25.1.0'
5+
}
6+
37
android {
48
compileSdkVersion 25
59
buildToolsVersion "25.0.2"

expandicon/src/main/java/com/github/zagum/expandicon/ExpandIconView.java

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
import android.graphics.Path;
1111
import android.graphics.Point;
1212
import android.os.Build;
13+
import android.support.annotation.IntDef;
14+
import android.support.annotation.RequiresApi;
1315
import android.util.AttributeSet;
1416
import android.view.View;
1517
import android.view.animation.DecelerateInterpolator;
18+
import java.lang.annotation.Retention;
19+
import java.lang.annotation.RetentionPolicy;
1620

1721
import static android.graphics.Paint.ANTI_ALIAS_FLAG;
1822

@@ -25,10 +29,22 @@ public class ExpandIconView extends View {
2529
private static final float PADDING_PROPORTION = 4f / 24f;
2630
private static final long DEFAULT_ANIMATION_DURATION = 150;
2731

28-
public static final int STATE_MORE = 0;
29-
public static final int STATE_LESS = 1;
30-
private static final int STATE_INTERMEDIATE = 2;
32+
@IntDef({
33+
MORE,
34+
LESS,
35+
INTERMEDIATE
36+
})
3137

38+
@Retention(RetentionPolicy.SOURCE)
39+
40+
public @interface State {
41+
}
42+
43+
public static final int MORE = 0;
44+
public static final int LESS = 1;
45+
private static final int INTERMEDIATE = 2;
46+
47+
@State
3248
private int state;
3349
private int width;
3450
private int height;
@@ -64,10 +80,10 @@ public void switchState() {
6480
* @param animate Indicates thaw state will be changed with animation or not
6581
*/
6682
public void switchState(boolean animate) {
67-
if (state == STATE_MORE) {
68-
setState(STATE_LESS, animate);
69-
} else if (state == STATE_LESS) {
70-
setState(STATE_MORE, animate);
83+
if (state == MORE) {
84+
setState(LESS, animate);
85+
} else if (state == LESS) {
86+
setState(MORE, animate);
7187
} else {
7288
setState(getFinalStateByFraction(), animate);
7389
}
@@ -76,23 +92,26 @@ public void switchState(boolean animate) {
7692
/**
7793
* Set one of two states and updates view
7894
*
79-
* @param state {@link #STATE_MORE} or {@link #STATE_LESS}
95+
* @param state {@link #MORE} or {@link #LESS}
8096
* @param animate Indicates thaw state will be changed with animation or not
97+
* @throws IllegalArgumentException if {@param state} is invalid
8198
*/
82-
public void setState(int state, boolean animate) {
99+
public void setState(@State int state, boolean animate) {
83100
this.state = state;
84-
if (state == STATE_MORE) {
101+
if (state == MORE) {
85102
fraction = 0f;
86-
} else if (state == STATE_LESS) {
103+
} else if (state == LESS) {
87104
fraction = 1f;
105+
} else {
106+
throw new IllegalArgumentException("Unknown state, must be one of STATE_MORE = 0, STATE_LESS = 1");
88107
}
89108
updateArrow(animate);
90109
}
91110

92111
/**
93112
* Set current fraction for arrow and updates view
94113
*
95-
* @param fraction Must be value from 0f to 1f {@link #STATE_MORE} state value is 0f, {@link #STATE_LESS}
114+
* @param fraction Must be value from 0f to 1f {@link #MORE} state value is 0f, {@link #LESS}
96115
* state value is 1f
97116
* @throws IllegalArgumentException if fraction is less than 0f or more than 1f
98117
*/
@@ -103,11 +122,11 @@ public void setFraction(float fraction, boolean animate) {
103122
if (this.fraction == fraction) return;
104123
this.fraction = fraction;
105124
if (fraction == 0f) {
106-
state = STATE_MORE;
125+
state = MORE;
107126
} else if (fraction == 1f) {
108-
state = STATE_LESS;
127+
state = LESS;
109128
} else {
110-
state = STATE_INTERMEDIATE;
129+
state = INTERMEDIATE;
111130
}
112131
updateArrow(animate);
113132
}
@@ -129,6 +148,13 @@ public ExpandIconView(Context context, AttributeSet attrs, int defStyleAttr) {
129148
init();
130149
}
131150

151+
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
152+
public ExpandIconView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
153+
super(context, attrs, defStyleAttr, defStyleRes);
154+
readAttributes(attrs);
155+
init();
156+
}
157+
132158
@Override
133159
protected void onDraw(Canvas canvas) {
134160
super.onDraw(canvas);
@@ -198,7 +224,7 @@ private void init() {
198224

199225
animationSpeed = DELTA_ALPHA / animationDuration;
200226

201-
setState(STATE_MORE, false);
227+
setState(MORE, false);
202228
}
203229

204230
private void updateArrow(boolean animate) {
@@ -274,11 +300,12 @@ private Point rotate(Point startPosition, double degrees) {
274300
return new Point(x, y);
275301
}
276302

303+
@State
277304
private int getFinalStateByFraction() {
278305
if (fraction <= .5f) {
279-
return STATE_MORE;
306+
return MORE;
280307
} else {
281-
return STATE_LESS;
308+
return LESS;
282309
}
283310
}
284311

0 commit comments

Comments
 (0)