Skip to content

Commit

Permalink
Make all LottieAnimationView setters idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
gpeal committed Dec 30, 2023
1 parent c9c8bb7 commit c05b567
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;

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

import com.airbnb.lottie.model.KeyPath;
import com.airbnb.lottie.utils.Logger;
import com.airbnb.lottie.utils.Utils;
Expand All @@ -36,7 +34,6 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -609,6 +606,10 @@ public void setFallbackResource(@DrawableRes int fallbackResource) {
}

private void setCompositionTask(LottieTask<LottieComposition> compositionTask) {
LottieResult<LottieComposition> result = compositionTask.getResult();
if (result != null && result.getValue() == composition) {
return;
}
userActionsTaken.add(UserActionTaken.SET_ANIMATION);
clearComposition();
cancelLoaderTask();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ private static LottieTask<LottieComposition> cache(@Nullable final String cacheK
LottieTask<LottieComposition> task = null;
final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey);
if (cachedComposition != null) {
task = new LottieTask<>(() -> new LottieResult<>(cachedComposition));
task = new LottieTask<>(cachedComposition);
}
if (cacheKey != null && taskCache.containsKey(cacheKey)) {
task = taskCache.get(cacheKey);
Expand Down
12 changes: 11 additions & 1 deletion lottie/src/main/java/com/airbnb/lottie/LottieTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
* <p>
* A task will produce a single result or a single failure.
*/
@SuppressWarnings("UnusedReturnValue") public class LottieTask<T> {
@SuppressWarnings("UnusedReturnValue")
public class LottieTask<T> {

/**
* Set this to change the executor that LottieTasks are run on. This will be the executor that composition parsing and url
Expand All @@ -48,6 +49,10 @@ public LottieTask(Callable<LottieResult<T>> runnable) {
this(runnable, false);
}

public LottieTask(T result) {
setResult(new LottieResult<>(result));
}

/**
* runNow is only used for testing.
*/
Expand Down Expand Up @@ -124,6 +129,11 @@ public synchronized LottieTask<T> removeFailureListener(LottieListener<Throwable
return this;
}

@Nullable
public LottieResult<T> getResult() {
return result;
}

private void notifyListeners() {
// Listeners should be called on the main thread.
handler.post(() -> {
Expand Down

0 comments on commit c05b567

Please sign in to comment.