From 87c04cd15fb25f2ef5cdd1761d24f48cb99f620f Mon Sep 17 00:00:00 2001 From: Ivan Baranov Date: Mon, 3 Sep 2018 14:52:57 +0300 Subject: [PATCH] Update methods for setting favorite state. New methods are: setFavorite(boolean favorite), setFavoriteAnimated(boolean favorite), setFavoriteSuppressListener(boolean favorite). #16 --- .travis.yml | 2 +- .../ivbaranov/mfb/MaterialFavoriteButton.java | 93 ++++++++++--------- 2 files changed, 52 insertions(+), 43 deletions(-) diff --git a/.travis.yml b/.travis.yml index 61495ee..06a4f62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ android: before_install: - yes | sdkmanager "platforms;android-27" - + before_script: - chmod +x gradlew diff --git a/materialfavoritebutton/src/main/java/com/github/ivbaranov/mfb/MaterialFavoriteButton.java b/materialfavoritebutton/src/main/java/com/github/ivbaranov/mfb/MaterialFavoriteButton.java index eb45857..726f88f 100644 --- a/materialfavoritebutton/src/main/java/com/github/ivbaranov/mfb/MaterialFavoriteButton.java +++ b/materialfavoritebutton/src/main/java/com/github/ivbaranov/mfb/MaterialFavoriteButton.java @@ -166,7 +166,8 @@ private void initAttributes(Context context, AttributeSet attributeSet) { if (attr != null) { try { mButtonSize = Utils.dpToPx( - attr.getInt(R.styleable.MaterialFavoriteButton_mfb_size, DEFAULT_BUTTON_SIZE), getResources()); + attr.getInt(R.styleable.MaterialFavoriteButton_mfb_size, DEFAULT_BUTTON_SIZE), + getResources()); mAnimateFavorite = attr.getBoolean(R.styleable.MaterialFavoriteButton_mfb_animate_favorite, mAnimateFavorite); mAnimateUnfavorite = @@ -248,25 +249,30 @@ public boolean isFavorite() { } /** - * Changes the favorite state of this button. + * Changes the favorite state of this button without animation. * * @param favorite true to favorite the button, false to uncheck it */ public void setFavorite(boolean favorite) { - if (mFavorite != favorite) { - mFavorite = favorite; - // Avoid infinite recursions if setChecked() is called from a listener - if (mBroadcasting) { - return; - } + updateFavoriteButton(favorite, false, false); + } - mBroadcasting = true; - if (mOnFavoriteChangeListener != null) { - mOnFavoriteChangeListener.onFavoriteChanged(this, mFavorite); - } - updateFavoriteButton(favorite); - mBroadcasting = false; - } + /** + * Changes the favorite state of this button with animation. + * + * @param favorite true to favorite the button, false to uncheck it + */ + public void setFavoriteAnimated(boolean favorite) { + updateFavoriteButton(favorite, true, false); + } + + /** + * Changes the favorite state of this button without calling OnFavoriteChangeListener. + * + * @param favorite true to favorite the button, false to uncheck it + */ + public void setFavoriteSuppressListener(boolean favorite) { + updateFavoriteButton(favorite, mAnimateFavorite, true); } /** @@ -275,18 +281,8 @@ public void setFavorite(boolean favorite) { * @param favorite true to favorite the button, false to uncheck it * @param animated true to force animated change, false to force not animated one */ - public void setFavorite(boolean favorite, boolean animated) { - if (favorite) { - boolean orig = mAnimateFavorite; - mAnimateFavorite = animated; - setFavorite(favorite); - mAnimateFavorite = orig; - } else { - boolean orig = mAnimateUnfavorite; - mAnimateUnfavorite = animated; - setFavorite(favorite); - mAnimateUnfavorite = orig; - } + @Deprecated public void setFavorite(boolean favorite, boolean animated) { + updateFavoriteButton(favorite, animated, false); } /** @@ -315,25 +311,38 @@ public void toggleFavorite(boolean animated) { } } - private void updateFavoriteButton(boolean favorite) { - if (favorite) { - if (mAnimateFavorite) { - animateButton(favorite); - } else { - super.setImageResource(mFavoriteResource); - if (mOnFavoriteAnimationEndListener != null) { - mOnFavoriteAnimationEndListener.onAnimationEnd(this, mFavorite); - } + private void updateFavoriteButton(boolean favorite, boolean animate, boolean suppressOnChange) { + if (mFavorite != favorite) { + mFavorite = favorite; + // Avoid infinite recursions if setChecked() is called from a listener + if (mBroadcasting) { + return; } - } else { - if (mAnimateUnfavorite) { - animateButton(favorite); + + mBroadcasting = true; + if (mOnFavoriteChangeListener != null && !suppressOnChange) { + mOnFavoriteChangeListener.onFavoriteChanged(this, mFavorite); + } + if (favorite) { + if (animate) { + animateButton(favorite); + } else { + super.setImageResource(mFavoriteResource); + if (mOnFavoriteAnimationEndListener != null) { + mOnFavoriteAnimationEndListener.onAnimationEnd(this, mFavorite); + } + } } else { - super.setImageResource(mNotFavoriteResource); - if (mOnFavoriteAnimationEndListener != null) { - mOnFavoriteAnimationEndListener.onAnimationEnd(this, mFavorite); + if (animate) { + animateButton(favorite); + } else { + super.setImageResource(mNotFavoriteResource); + if (mOnFavoriteAnimationEndListener != null) { + mOnFavoriteAnimationEndListener.onAnimationEnd(this, mFavorite); + } } } + mBroadcasting = false; } }