Skip to content

Commit

Permalink
Add the ability to set custom cache keys or set non cache key (#1450)
Browse files Browse the repository at this point in the history
Fixes #1289
  • Loading branch information
gpeal committed Nov 30, 2019
1 parent 8d676e1 commit ee94224
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 26 deletions.
Expand Up @@ -40,9 +40,9 @@ class PlayerViewModel(
val url = args.url ?: args.animationData?.lottieLink

when {
url != null -> LottieCompositionFactory.fromUrl(application, url)
url != null -> LottieCompositionFactory.fromUrl(application, url, null)
args.fileUri != null -> taskForUri(args.fileUri)
args.asset != null -> LottieCompositionFactory.fromAsset(application, args.asset)
args.asset != null -> LottieCompositionFactory.fromAsset(application, args.asset, null)
else -> throw IllegalArgumentException("Don't know how to fetch animation for $args")
}
.addListener {
Expand All @@ -64,7 +64,7 @@ class PlayerViewModel(
return LottieTask { throw e }
}

return LottieCompositionFactory.fromJsonInputStream(fis, uri.toString())
return LottieCompositionFactory.fromJsonInputStream(fis, null)
}

fun toggleRenderGraphVisible() = setState { copy(renderGraphVisible = !renderGraphVisible) }
Expand Down
26 changes: 23 additions & 3 deletions lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
Expand Up @@ -101,6 +101,7 @@ public void onResult(Throwable result) {
private boolean wasAnimatingWhenNotShown = false;
private boolean wasAnimatingWhenDetached = false;
private boolean autoPlay = false;
private boolean cacheComposition = true;
private RenderMode renderMode = RenderMode.AUTOMATIC;
private Set<LottieOnCompositionLoadedListener> lottieOnCompositionLoadedListeners = new HashSet<>();
/**
Expand Down Expand Up @@ -134,6 +135,7 @@ public LottieAnimationView(Context context, AttributeSet attrs, int defStyleAttr
private void init(@Nullable AttributeSet attrs) {
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.LottieAnimationView);
if (!isInEditMode()) {
cacheComposition = ta.getBoolean(R.styleable.LottieAnimationView_lottie_cacheComposition, true);
boolean hasRawRes = ta.hasValue(R.styleable.LottieAnimationView_lottie_rawRes);
boolean hasFileName = ta.hasValue(R.styleable.LottieAnimationView_lottie_fileName);
boolean hasUrl = ta.hasValue(R.styleable.LottieAnimationView_lottie_url);
Expand Down Expand Up @@ -348,20 +350,36 @@ public boolean isMergePathsEnabledForKitKatAndAbove() {
return lottieDrawable.isMergePathsEnabledForKitKatAndAbove();
}

/**
* If set to true, all future compositions that are set will be cached so that they don't need to be parsed
* next time they are loaded. This won't apply to compositions that have already been loaded.
*
* Defaults to true.
*
* {@link R.attr#lottie_cacheComposition}
*/
public void setCacheComposition(boolean cacheComposition) {
this.cacheComposition = cacheComposition;
}

/**
* Sets the animation from a file in the raw directory.
* This will load and deserialize the file asynchronously.
*/
public void setAnimation(@RawRes final int rawRes) {
this.animationResId = rawRes;
animationName = null;
setCompositionTask(LottieCompositionFactory.fromRawRes(getContext(), rawRes));
LottieTask<LottieComposition> task = cacheComposition ?
LottieCompositionFactory.fromRawRes(getContext(), rawRes) : LottieCompositionFactory.fromRawRes(getContext(), rawRes, null);
setCompositionTask(task);
}

public void setAnimation(final String assetName) {
this.animationName = assetName;
animationResId = 0;
setCompositionTask(LottieCompositionFactory.fromAsset(getContext(), assetName));
LottieTask<LottieComposition> task = cacheComposition ?
LottieCompositionFactory.fromAsset(getContext(), assetName) : LottieCompositionFactory.fromAsset(getContext(), assetName, null);
setCompositionTask(task);
}

/**
Expand Down Expand Up @@ -401,7 +419,9 @@ public void setAnimation(InputStream stream, @Nullable String cacheKey) {
* can be accessed immediately for subsequent requests. If the file does not parse to a composition, the temporary file will be deleted.
*/
public void setAnimationFromUrl(String url) {
setCompositionTask(LottieCompositionFactory.fromUrl(getContext(), url));
LottieTask<LottieComposition> task = cacheComposition ?
LottieCompositionFactory.fromUrl(getContext(), url) : LottieCompositionFactory.fromUrl(getContext(), url, null);
setCompositionTask(task);
}

/**
Expand Down
115 changes: 95 additions & 20 deletions lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java
Expand Up @@ -65,10 +65,20 @@ public static void setMaxCacheSize(int size) {
* Fetch an animation from an http url. Once it is downloaded once, Lottie will cache the file to disk for
* future use. Because of this, you may call `fromUrl` ahead of time to warm the cache if you think you
* might need an animation in the future.
*
* To skip the cache, add null as a third parameter.
*/
public static LottieTask<LottieComposition> fromUrl(final Context context, final String url) {
String urlCacheKey = "url_" + url;
return cache(urlCacheKey, new Callable<LottieResult<LottieComposition>>() {
return fromUrl(context, url, "url_" + url);
}

/**
* Fetch an animation from an http url. Once it is downloaded once, Lottie will cache the file to disk for
* future use. Because of this, you may call `fromUrl` ahead of time to warm the cache if you think you
* might need an animation in the future.
*/
public static LottieTask<LottieComposition> fromUrl(final Context context, final String url, @Nullable String cacheKey) {
return cache(cacheKey, new Callable<LottieResult<LottieComposition>>() {
@Override
public LottieResult<LottieComposition> call() {
return NetworkFetcher.fetchSync(context, url);
Expand All @@ -91,15 +101,31 @@ public static LottieResult<LottieComposition> fromUrlSync(Context context, Strin
* The asset file name will be used as a cache key so future usages won't have to parse the json again.
* However, if your animation has images, you may package the json and images as a single flattened zip file in assets.
*
* To skip the cache, add null as a third parameter.
*
* @see #fromZipStream(ZipInputStream, String)
*/
public static LottieTask<LottieComposition> fromAsset(Context context, final String fileName) {
String cacheKey = "asset_" + fileName;
return fromAsset(context, fileName, cacheKey);
}

/**
* Parse an animation from src/main/assets. It is recommended to use {@link #fromRawRes(Context, int)} instead.
* The asset file name will be used as a cache key so future usages won't have to parse the json again.
* However, if your animation has images, you may package the json and images as a single flattened zip file in assets.
*
* Pass null as the cache key to skip the cache.
*
* @see #fromZipStream(ZipInputStream, String)
*/
public static LottieTask<LottieComposition> fromAsset(Context context, final String fileName, @Nullable final String cacheKey) {
// Prevent accidentally leaking an Activity.
final Context appContext = context.getApplicationContext();
return cache(fileName, new Callable<LottieResult<LottieComposition>>() {
return cache(cacheKey, new Callable<LottieResult<LottieComposition>>() {
@Override
public LottieResult<LottieComposition> call() {
return fromAssetSync(appContext, fileName);
return fromAssetSync(appContext, fileName, cacheKey);
}
});
}
Expand All @@ -109,12 +135,28 @@ public LottieResult<LottieComposition> call() {
* The asset file name will be used as a cache key so future usages won't have to parse the json again.
* However, if your animation has images, you may package the json and images as a single flattened zip file in assets.
*
* To skip the cache, add null as a third parameter.
*
* @see #fromZipStreamSync(ZipInputStream, String)
*/
@WorkerThread
public static LottieResult<LottieComposition> fromAssetSync(Context context, String fileName) {
try {
String cacheKey = "asset_" + fileName;
return fromAssetSync(context, fileName, cacheKey);
}

/**
* Parse an animation from src/main/assets. It is recommended to use {@link #fromRawRes(Context, int)} instead.
* The asset file name will be used as a cache key so future usages won't have to parse the json again.
* However, if your animation has images, you may package the json and images as a single flattened zip file in assets.
*
* Pass null as the cache key to skip the cache.
*
* @see #fromZipStreamSync(ZipInputStream, String)
*/
@WorkerThread
public static LottieResult<LottieComposition> fromAssetSync(Context context, String fileName, @Nullable String cacheKey) {
try {
if (fileName.endsWith(".zip")) {
return fromZipStreamSync(new ZipInputStream(context.getAssets().open(fileName)), cacheKey);
}
Expand All @@ -131,12 +173,27 @@ public static LottieResult<LottieComposition> fromAssetSync(Context context, Str
* The resource id will be used as a cache key so future usages won't parse the json again.
* Note: to correctly load dark mode (-night) resources, make sure you pass Activity as a context (instead of e.g. the application context).
* The Activity won't be leaked.
*
* To skip the cache, add null as a third parameter.
*/
public static LottieTask<LottieComposition> fromRawRes(Context context, @RawRes final int rawRes) {
return fromRawRes(context, rawRes, rawResCacheKey(context, rawRes));
}

/**
* Parse an animation from raw/res. This is recommended over putting your animation in assets because
* it uses a hard reference to R.
* The resource id will be used as a cache key so future usages won't parse the json again.
* Note: to correctly load dark mode (-night) resources, make sure you pass Activity as a context (instead of e.g. the application context).
* The Activity won't be leaked.
*
* Pass null as the cache key to skip caching.
*/
public static LottieTask<LottieComposition> fromRawRes(Context context, @RawRes final int rawRes, @Nullable String cacheKey) {
// Prevent accidentally leaking an Activity.
final WeakReference<Context> contextRef = new WeakReference<>(context);
final Context appContext = context.getApplicationContext();
return cache(rawResCacheKey(context, rawRes), new Callable<LottieResult<LottieComposition>>() {
return cache(cacheKey, new Callable<LottieResult<LottieComposition>>() {
@Override
public LottieResult<LottieComposition> call() {
@Nullable Context originalContext = contextRef.get();
Expand All @@ -152,11 +209,27 @@ public LottieResult<LottieComposition> call() {
* The resource id will be used as a cache key so future usages won't parse the json again.
* Note: to correctly load dark mode (-night) resources, make sure you pass Activity as a context (instead of e.g. the application context).
* The Activity won't be leaked.
*
* To skip the cache, add null as a third parameter.
*/
@WorkerThread
public static LottieResult<LottieComposition> fromRawResSync(Context context, @RawRes int rawRes) {
return fromRawResSync(context, rawRes, rawResCacheKey(context, rawRes));
}

/**
* Parse an animation from raw/res. This is recommended over putting your animation in assets because
* it uses a hard reference to R.
* The resource id will be used as a cache key so future usages won't parse the json again.
* Note: to correctly load dark mode (-night) resources, make sure you pass Activity as a context (instead of e.g. the application context).
* The Activity won't be leaked.
*
* Pass null as the cache key to skip caching.
*/
@WorkerThread
public static LottieResult<LottieComposition> fromRawResSync(Context context, @RawRes int rawRes, @Nullable String cacheKey) {
try {
return fromJsonInputStreamSync(context.getResources().openRawResource(rawRes), rawResCacheKey(context, rawRes));
return fromJsonInputStreamSync(context.getResources().openRawResource(rawRes), cacheKey);
} catch (Resources.NotFoundException e) {
return new LottieResult<>(e);
}
Expand Down Expand Up @@ -399,19 +472,21 @@ public LottieResult<LottieComposition> call() {
}

LottieTask<LottieComposition> task = new LottieTask<>(callable);
task.addListener(new LottieListener<LottieComposition>() {
@Override
public void onResult(LottieComposition result) {
taskCache.remove(cacheKey);
}
});
task.addFailureListener(new LottieListener<Throwable>() {
@Override
public void onResult(Throwable result) {
taskCache.remove(cacheKey);
}
});
taskCache.put(cacheKey, task);
if (cacheKey != null) {
task.addListener(new LottieListener<LottieComposition>() {
@Override
public void onResult(LottieComposition result) {
taskCache.remove(cacheKey);
}
});
task.addFailureListener(new LottieListener<Throwable>() {
@Override
public void onResult(Throwable result) {
taskCache.remove(cacheKey);
}
});
taskCache.put(cacheKey, task);
}
return task;
}
}
1 change: 1 addition & 0 deletions lottie/src/main/res/values/attrs.xml
Expand Up @@ -18,6 +18,7 @@
<attr name="lottie_colorFilter" format="color" />
<attr name="lottie_scale" format="float" />
<attr name="lottie_speed" format="float" />
<attr name="lottie_cacheComposition" format="boolean" />
<!-- These values must be kept in sync with the RenderMode enum -->
<attr name="lottie_renderMode" format="enum">
<enum name="automatic" value="0" />
Expand Down

0 comments on commit ee94224

Please sign in to comment.