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

Allow configuring a default global value for async updates #2356

Merged
merged 2 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion lottie/src/main/java/com/airbnb/lottie/L.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.core.os.TraceCompat;

import com.airbnb.lottie.network.DefaultLottieNetworkFetcher;
import com.airbnb.lottie.network.LottieNetworkCacheProvider;
Expand All @@ -25,6 +24,7 @@ public class L {
private static boolean traceEnabled = false;
private static boolean networkCacheEnabled = true;
private static boolean disablePathInterpolatorCache = true;
private static AsyncUpdates defaultAsyncUpdates = AsyncUpdates.AUTOMATIC;

private static LottieNetworkFetcher fetcher;
private static LottieNetworkCacheProvider cacheProvider;
Expand Down Expand Up @@ -131,4 +131,12 @@ public static void setDisablePathInterpolatorCache(boolean disablePathInterpolat
public static boolean getDisablePathInterpolatorCache() {
return disablePathInterpolatorCache;
}

public static void setDefaultAsyncUpdates(AsyncUpdates asyncUpdates) {
L.defaultAsyncUpdates = asyncUpdates;
}

public static AsyncUpdates getDefaultAsyncUpdates() {
return L.defaultAsyncUpdates;
}
}
1 change: 1 addition & 0 deletions lottie/src/main/java/com/airbnb/lottie/Lottie.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ public static void initialize(@NonNull final LottieConfig lottieConfig) {
L.setTraceEnabled(lottieConfig.enableSystraceMarkers);
L.setNetworkCacheEnabled(lottieConfig.enableNetworkCache);
L.setDisablePathInterpolatorCache(lottieConfig.disablePathInterpolatorCache);
L.setDefaultAsyncUpdates(lottieConfig.defaultAsyncUpdates);
}
}
19 changes: 17 additions & 2 deletions lottie/src/main/java/com/airbnb/lottie/LottieConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ public class LottieConfig {
final boolean enableSystraceMarkers;
final boolean enableNetworkCache;
final boolean disablePathInterpolatorCache;
final AsyncUpdates defaultAsyncUpdates;

private LottieConfig(@Nullable LottieNetworkFetcher networkFetcher, @Nullable LottieNetworkCacheProvider cacheProvider,
boolean enableSystraceMarkers, boolean enableNetworkCache, boolean disablePathInterpolatorCache) {
boolean enableSystraceMarkers, boolean enableNetworkCache, boolean disablePathInterpolatorCache,
AsyncUpdates defaultAsyncUpdates) {
this.networkFetcher = networkFetcher;
this.cacheProvider = cacheProvider;
this.enableSystraceMarkers = enableSystraceMarkers;
this.enableNetworkCache = enableNetworkCache;
this.disablePathInterpolatorCache = disablePathInterpolatorCache;
this.defaultAsyncUpdates = defaultAsyncUpdates;
}

public static final class Builder {
Expand All @@ -39,6 +42,7 @@ public static final class Builder {
private boolean enableSystraceMarkers = false;
private boolean enableNetworkCache = true;
private boolean disablePathInterpolatorCache = true;
private AsyncUpdates defaultAsyncUpdates = AsyncUpdates.AUTOMATIC;

/**
* Lottie has a default network fetching stack built on {@link java.net.HttpURLConnection}. However, if you would like to hook into your own
Expand Down Expand Up @@ -127,9 +131,20 @@ public Builder setDisablePathInterpolatorCache(boolean disable) {
return this;
}

/**
* Sets the default value for async updates.
* @see LottieDrawable#setAsyncUpdates(AsyncUpdates)
*/
@NonNull
public Builder setDefaultAsyncUpdates(AsyncUpdates asyncUpdates) {
defaultAsyncUpdates = asyncUpdates;
return this;
}

@NonNull
public LottieConfig build() {
return new LottieConfig(networkFetcher, cacheProvider, enableSystraceMarkers, enableNetworkCache, disablePathInterpolatorCache);
return new LottieConfig(networkFetcher, cacheProvider, enableSystraceMarkers, enableNetworkCache, disablePathInterpolatorCache,
defaultAsyncUpdates);
}
}
}
12 changes: 9 additions & 3 deletions lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import androidx.annotation.RestrictTo;

import com.airbnb.lottie.animation.LPaint;
import com.airbnb.lottie.animation.keyframe.PathKeyframe;
import com.airbnb.lottie.manager.FontAssetManager;
import com.airbnb.lottie.manager.ImageAssetManager;
import com.airbnb.lottie.model.Font;
Expand Down Expand Up @@ -145,7 +146,8 @@ private enum OnVisibleAction {
private Matrix softwareRenderingOriginalCanvasMatrix;
private Matrix softwareRenderingOriginalCanvasMatrixInverse;

private AsyncUpdates asyncUpdates = AsyncUpdates.AUTOMATIC;
/** Use the getter so that it can fall back to {@link L#getDefaultAsyncUpdates()}. */
@Nullable private AsyncUpdates asyncUpdates;
private final ValueAnimator.AnimatorUpdateListener progressUpdateListener = animation -> {
if (getAsyncUpdatesEnabled()) {
// Render a new frame.
Expand Down Expand Up @@ -411,7 +413,11 @@ public void setRenderMode(RenderMode renderMode) {
* Returns the current value of {@link AsyncUpdates}. Refer to the docs for {@link AsyncUpdates} for more info.
*/
public AsyncUpdates getAsyncUpdates() {
return asyncUpdates;
AsyncUpdates asyncUpdates = this.asyncUpdates;
if (asyncUpdates != null) {
return asyncUpdates;
}
return L.getDefaultAsyncUpdates();
}

/**
Expand All @@ -421,7 +427,7 @@ public AsyncUpdates getAsyncUpdates() {
* whether automatic is defaulting to enabled or not.
*/
public boolean getAsyncUpdatesEnabled() {
return asyncUpdates == AsyncUpdates.ENABLED;
return getAsyncUpdates() == AsyncUpdates.ENABLED;
}

/**
Expand Down