Skip to content

Commit

Permalink
Prevent LottieAnimationView from overwriting user actions when restor…
Browse files Browse the repository at this point in the history
…ing from saved instance state (#2002)

Fixes #1841
  • Loading branch information
gpeal authored Jan 17, 2022
1 parent eac5647 commit 757db12
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.AttrRes;
import androidx.annotation.DrawableRes;
Expand All @@ -26,7 +25,6 @@
import androidx.annotation.RequiresApi;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.view.ViewCompat;

import com.airbnb.lottie.model.KeyPath;
import com.airbnb.lottie.utils.Logger;
Expand Down Expand Up @@ -104,6 +102,10 @@ public void onResult(Throwable result) {
private boolean cacheComposition = true;
private RenderMode renderMode = RenderMode.AUTOMATIC;
private boolean useSoftwareRendering = false;
/**
* Keeps track of explicit user actions taken and prevents onRestoreInstanceState from overwriting already set values.
*/
private final Set<UserActionTaken> userActionsTaken = new HashSet<>();
private final Set<LottieOnCompositionLoadedListener> lottieOnCompositionLoadedListeners = new HashSet<>();
/**
* Prevents a StackOverflowException on 4.4 in which getDrawingCache() calls buildDrawingCache().
Expand Down Expand Up @@ -284,20 +286,28 @@ private void init(@Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
animationName = ss.animationName;
if (!TextUtils.isEmpty(animationName)) {
if (!userActionsTaken.contains(UserActionTaken.SET_ANIMATION) && !TextUtils.isEmpty(animationName)) {
setAnimation(animationName);
}
animationResId = ss.animationResId;
if (animationResId != 0) {
if (!userActionsTaken.contains(UserActionTaken.SET_ANIMATION) && animationResId != 0) {
setAnimation(animationResId);
}
setProgress(ss.progress);
if (ss.isAnimating) {
if (!userActionsTaken.contains(UserActionTaken.SET_PROGRESS)) {
setProgress(ss.progress);
}
if (!userActionsTaken.contains(UserActionTaken.PLAY_OPTION) && ss.isAnimating) {
playAnimation();
}
lottieDrawable.setImagesAssetsFolder(ss.imageAssetsFolder);
setRepeatMode(ss.repeatMode);
setRepeatCount(ss.repeatCount);
if (!userActionsTaken.contains(UserActionTaken.SET_IMAGE_ASSETS)) {
setImageAssetsFolder(ss.imageAssetsFolder);
}
if (!userActionsTaken.contains(UserActionTaken.SET_REPEAT_MODE)) {
setRepeatMode(ss.repeatMode);
}
if (!userActionsTaken.contains(UserActionTaken.SET_REPEAT_COUNT)) {
setRepeatCount(ss.repeatCount);
}
}

@Override protected void onAttachedToWindow() {
Expand Down Expand Up @@ -513,6 +523,7 @@ public void setFallbackResource(@DrawableRes int fallbackResource) {
}

private void setCompositionTask(LottieTask<LottieComposition> compositionTask) {
userActionsTaken.add(UserActionTaken.SET_ANIMATION);
clearComposition();
cancelLoaderTask();
this.compositionTask = compositionTask
Expand Down Expand Up @@ -589,6 +600,7 @@ public boolean hasMatte() {
*/
@MainThread
public void playAnimation() {
userActionsTaken.add(UserActionTaken.PLAY_OPTION);
lottieDrawable.playAnimation();
computeRenderMode();
}
Expand All @@ -599,6 +611,7 @@ public void playAnimation() {
*/
@MainThread
public void resumeAnimation() {
userActionsTaken.add(UserActionTaken.PLAY_OPTION);
lottieDrawable.resumeAnimation();
computeRenderMode();
}
Expand Down Expand Up @@ -781,6 +794,7 @@ public void loop(boolean loop) {
* @param mode {@link LottieDrawable#RESTART} or {@link LottieDrawable#REVERSE}
*/
public void setRepeatMode(@LottieDrawable.RepeatMode int mode) {
userActionsTaken.add(UserActionTaken.SET_REPEAT_MODE);
lottieDrawable.setRepeatMode(mode);
}

Expand All @@ -803,6 +817,7 @@ public int getRepeatMode() {
* @param count the number of times the animation should be repeated
*/
public void setRepeatCount(int count) {
userActionsTaken.add(UserActionTaken.SET_REPEAT_COUNT);
lottieDrawable.setRepeatCount(count);
}

Expand Down Expand Up @@ -965,6 +980,7 @@ public float getScale() {

@MainThread
public void cancelAnimation() {
userActionsTaken.add(UserActionTaken.PLAY_OPTION);
lottieDrawable.cancelAnimation();
computeRenderMode();
}
Expand Down Expand Up @@ -993,6 +1009,7 @@ public int getFrame() {
}

public void setProgress(@FloatRange(from = 0f, to = 1f) float progress) {
userActionsTaken.add(UserActionTaken.SET_PROGRESS);
lottieDrawable.setProgress(progress);
}

Expand Down Expand Up @@ -1194,4 +1211,13 @@ public SavedState[] newArray(int size) {
}
};
}

private enum UserActionTaken {
SET_ANIMATION,
SET_PROGRESS,
SET_REPEAT_MODE,
SET_REPEAT_COUNT,
SET_IMAGE_ASSETS,
PLAY_OPTION,
}
}

0 comments on commit 757db12

Please sign in to comment.