Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not hold strong reference to LottieAnimationView in success/failure listeners #2293

Merged
merged 1 commit into from
May 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -74,18 +75,49 @@
throw new IllegalStateException("Unable to parse composition", throwable);
};

private final LottieListener<LottieComposition> loadedListener = this::setComposition;
private final LottieListener<LottieComposition> loadedListener = new WeakSuccessListener(this);

private final LottieListener<Throwable> wrappedFailureListener = new LottieListener<Throwable>() {
@Override
public void onResult(Throwable result) {
if (fallbackResource != 0) {
setImageResource(fallbackResource);
private static class WeakSuccessListener implements LottieListener<LottieComposition> {

private final WeakReference<LottieAnimationView> targetReference;

public WeakSuccessListener(LottieAnimationView target) {
this.targetReference = new WeakReference<>(target);
}

@Override public void onResult(LottieComposition result) {
LottieAnimationView targetView = targetReference.get();
if (targetView == null) {
return;
}
LottieListener<Throwable> l = failureListener == null ? DEFAULT_FAILURE_LISTENER : failureListener;
targetView.setComposition(result);
}
}

private final LottieListener<Throwable> wrappedFailureListener = new WeakFailureListener(this);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit on naming here. This has wrapped and loaded doesn't. I think you can omit it since there isn't a non-wrapped version that people could confuse this with but other than that, this LGTM.

Copy link
Contributor Author

@mateuszkwiecinski mateuszkwiecinski May 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can omit it since there isn't a non-wrapped version that people could confuse this with but other than that

I wanted to apply your suggestion, but realized there is actually a non-wrapped version called failureListener which comes from the user. It seems like the non-wrapped is the one passed by the user, and the wrapped is also responsible for setting a fallback if resource fails to load.
Looking at public api of LottieAnimationView - it's possible to set multiple LottieOnCompositionLoadedListener callbacks, but only one setFailureListener, which explains why wrapped variant is needed here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Haven't looked at this code in a while 😄


private static class WeakFailureListener implements LottieListener<Throwable> {

private final WeakReference<LottieAnimationView> targetReference;

public WeakFailureListener(LottieAnimationView target) {
this.targetReference = new WeakReference<>(target);
}

@Override public void onResult(Throwable result) {
LottieAnimationView targetView = targetReference.get();
if (targetView == null) {
return;
}

if (targetView.fallbackResource != 0) {
targetView.setImageResource(targetView.fallbackResource);
}
LottieListener<Throwable> l = targetView.failureListener == null ? DEFAULT_FAILURE_LISTENER : targetView.failureListener;
l.onResult(result);
}
};
}

@Nullable private LottieListener<Throwable> failureListener;
@DrawableRes private int fallbackResource = 0;

Expand Down