Skip to content

Commit

Permalink
Allow configuring a default global value for async updates (#2356)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpeal authored Sep 2, 2023
1 parent 5ca7e90 commit c76b9f0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
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

0 comments on commit c76b9f0

Please sign in to comment.