Skip to content

Commit ab06bbc

Browse files
authored
add boolean return value to SparkEventListener that controls animation (#2)
1 parent 30fd0a2 commit ab06bbc

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

sparkbutton/src/main/java/at/connyduck/sparkbutton/SparkButton.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -254,25 +254,26 @@ public void setEventListener(SparkEventListener listener) {
254254

255255
@Override
256256
public void onClick(View v) {
257-
if (imageResourceIdInactive != INVALID_RESOURCE_ID) {
258-
isChecked = !isChecked;
257+
boolean shouldPlayAnimation = listener == null || listener.onEvent(this, isChecked);
259258

260-
imageView.setImageResource(isChecked ? imageResourceIdActive : imageResourceIdInactive);
259+
if(shouldPlayAnimation) {
260+
if (imageResourceIdInactive != INVALID_RESOURCE_ID) {
261+
isChecked = !isChecked;
261262

262-
if (animatorSet != null) {
263-
animatorSet.cancel();
264-
}
265-
if (isChecked) {
266-
sparkAnimationView.setVisibility(VISIBLE);
267-
playAnimation();
263+
imageView.setImageResource(isChecked ? imageResourceIdActive : imageResourceIdInactive);
264+
265+
if (animatorSet != null) {
266+
animatorSet.cancel();
267+
}
268+
if (isChecked) {
269+
sparkAnimationView.setVisibility(VISIBLE);
270+
playAnimation();
271+
} else {
272+
sparkAnimationView.setVisibility(INVISIBLE);
273+
}
268274
} else {
269-
sparkAnimationView.setVisibility(INVISIBLE);
275+
playAnimation();
270276
}
271-
} else {
272-
playAnimation();
273-
}
274-
if (listener != null) {
275-
listener.onEvent(this, isChecked);
276277
}
277278
}
278279

sparkbutton/src/main/java/at/connyduck/sparkbutton/SparkEventListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
import androidx.annotation.NonNull;
1818

1919
public interface SparkEventListener {
20-
void onEvent(@NonNull SparkButton button, boolean buttonState);
20+
boolean onEvent(@NonNull SparkButton button, boolean buttonState);
2121
}

0 commit comments

Comments
 (0)