Skip to content

Commit

Permalink
replace States with Boolean flag
Browse files Browse the repository at this point in the history
add attr to set state in xml
  • Loading branch information
zagum committed Jan 11, 2017
1 parent 3dcc326 commit b03502a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,18 @@
import android.graphics.Region;
import android.os.Build;
import android.support.annotation.FloatRange;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.animation.DecelerateInterpolator;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class SwitchIconView extends AppCompatImageView {

public static final int ENABLED = 0;
public static final int DISABLED = 1;

private static final int DEFAULT_ANIMATION_DURATION = 300;
private static final float DASH_THICKNESS_PART = 1f / 12f;
private static final float DEFAULT_DISABLED_ALPHA = .5f;
private static final float SIN_45 = (float) Math.sin(Math.toRadians(45));

@IntDef({
ENABLED,
DISABLED
})
@Retention(RetentionPolicy.SOURCE)
public @interface State {
}

private final long animationDuration;
@FloatRange(from = 0f, to = 1f)
private final float disabledStateAlpha;
Expand All @@ -66,8 +51,7 @@ public class SwitchIconView extends AppCompatImageView {
private int dashLengthXProjection;
private int dashLengthYProjection;

@State
private int currentState = ENABLED;
private boolean enabled;
@FloatRange(from = 0f, to = 1f)
private float fraction = 0f;

Expand Down Expand Up @@ -96,6 +80,7 @@ public SwitchIconView(@NonNull Context context, @Nullable AttributeSet attrs, in
iconTintColor = array.getColor(R.styleable.SwitchIconView_si_tint_color, Color.BLACK);
animationDuration = array.getInteger(R.styleable.SwitchIconView_si_animation_duration, DEFAULT_ANIMATION_DURATION);
disabledStateAlpha = array.getFloat(R.styleable.SwitchIconView_si_disabled_alpha, DEFAULT_DISABLED_ALPHA);
enabled = array.getBoolean(R.styleable.SwitchIconView_si_disabled_alpha, true);
} finally {
array.recycle();
}
Expand All @@ -117,64 +102,57 @@ public SwitchIconView(@NonNull Context context, @Nullable AttributeSet attrs, in
clipPath = new Path();

initDashCoordinates();
setFraction(enabled ? 0f : 1f);
}

/**
* Changes state with animation
*
* @param state {@link #ENABLED} or {@link #DISABLED}
* @throws IllegalArgumentException if {@param state} is invalid
* @param enabled If TRUE - icon is enabled
*/
public void setState(@State int state) {
setState(state, true);
public void setIconEnabled(boolean enabled) {
setIconEnabled(enabled, true);
}

/**
* Changes state
*
* @param state {@link #ENABLED} or {@link #DISABLED}
* @param animate Indicates that state will be changed with or without animation
* @throws IllegalArgumentException if {@param state} is invalid
* @param enabled If TRUE - icon is enabled
*/
public void setState(@State int state, boolean animate) {
if (state == currentState) return;
if (state != ENABLED && state != DISABLED) {
throw new IllegalArgumentException("Unknown state [" + state + "]");
}
public void setIconEnabled(boolean enabled, boolean animate) {
if (this.enabled == enabled) return;
switchState(animate);
}

/**
* Check state
*
* @return TRUE if icon is enabled, otherwise FALSE
*/
public boolean isIconEnabled() {
return currentState == ENABLED;
return enabled;
}

/**
* Switches state between values {@link #ENABLED} or {@link #DISABLED}
* with animation
* Switches icon state with animation
*/
public void switchState() {
switchState(true);
}

/**
* Switches state between values {@link #ENABLED} or {@link #DISABLED}
* with animation
* Switches icon state
*
* @param animate Indicates that state will be changed with or without animation
*/
public void switchState(boolean animate) {
float newFraction;
if (currentState == ENABLED) {
if (enabled) {
newFraction = 1f;
currentState = DISABLED;
} else {
newFraction = 0f;
currentState = ENABLED;
}
enabled = !enabled;
if (animate) {
animateToFraction(newFraction);
} else {
Expand Down
1 change: 1 addition & 0 deletions switchicon/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
<attr name="si_tint_color" format="color"/>
<attr name="si_animation_duration" format="integer"/>
<attr name="si_disabled_alpha" format="float"/>
<attr name="si_enabled" format="boolean"/>
</declare-styleable>
</resources>

0 comments on commit b03502a

Please sign in to comment.