Skip to content

Commit

Permalink
Check cache for sync
Browse files Browse the repository at this point in the history
  • Loading branch information
gpeal committed Sep 2, 2023
1 parent 0014ef6 commit f609986
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ public static LottieResult<LottieComposition> fromUrlSync(Context context, Strin
*/
@WorkerThread
public static LottieResult<LottieComposition> fromUrlSync(Context context, String url, @Nullable String cacheKey) {
final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey);
if (cachedComposition != null) {
return new LottieResult<>(cachedComposition);
}
LottieResult<LottieComposition> result = L.networkFetcher(context).fetchSync(context, url, cacheKey);
if (cacheKey != null && result.getValue() != null) {
LottieCompositionCache.getInstance().put(cacheKey, result.getValue());
Expand Down Expand Up @@ -213,6 +217,10 @@ public static LottieResult<LottieComposition> fromAssetSync(Context context, Str
*/
@WorkerThread
public static LottieResult<LottieComposition> fromAssetSync(Context context, String fileName, @Nullable String cacheKey) {
final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey);
if (cachedComposition != null) {
return new LottieResult<>(cachedComposition);
}
try {
if (fileName.endsWith(".zip") || fileName.endsWith(".lottie")) {
return fromZipStreamSync(context, new ZipInputStream(context.getAssets().open(fileName)), cacheKey);
Expand Down Expand Up @@ -282,6 +290,10 @@ public static LottieResult<LottieComposition> fromRawResSync(Context context, @R
*/
@WorkerThread
public static LottieResult<LottieComposition> fromRawResSync(Context context, @RawRes int rawRes, @Nullable String cacheKey) {
final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey);
if (cachedComposition != null) {
return new LottieResult<>(cachedComposition);
}
try {
BufferedSource source = Okio.buffer(source(context.getResources().openRawResource(rawRes)));
if (isZipCompressed(source)) {
Expand Down Expand Up @@ -376,8 +388,6 @@ public static LottieTask<LottieComposition> fromJsonString(final String json, @N
*/
@WorkerThread
public static LottieResult<LottieComposition> fromJsonStringSync(String json, @Nullable String cacheKey) {


ByteArrayInputStream stream = new ByteArrayInputStream(json.getBytes());
return fromJsonReaderSync(JsonReader.of(buffer(source(stream))), cacheKey);
}
Expand All @@ -399,6 +409,10 @@ public static LottieResult<LottieComposition> fromJsonReaderSync(com.airbnb.lott
private static LottieResult<LottieComposition> fromJsonReaderSyncInternal(
com.airbnb.lottie.parser.moshi.JsonReader reader, @Nullable String cacheKey, boolean close) {
try {
final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey);
if (cachedComposition != null) {
return new LottieResult<>(cachedComposition);
}
LottieComposition composition = LottieCompositionMoshiParser.parse(reader);
if (cacheKey != null) {
LottieCompositionCache.getInstance().put(cacheKey, composition);
Expand Down Expand Up @@ -516,6 +530,10 @@ private static LottieResult<LottieComposition> fromZipStreamSyncInternal(Context
Map<String, Typeface> fonts = new HashMap<>();

try {
final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey);
if (cachedComposition != null) {
return new LottieResult<>(cachedComposition);
}
ZipEntry entry = inputStream.getNextEntry();
while (entry != null) {
final String entryName = entry.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static junit.framework.Assert.assertNull;
import static okio.Okio.buffer;
import static okio.Okio.source;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame;

@SuppressWarnings("ReferenceEquality")
Expand Down Expand Up @@ -45,6 +46,20 @@ public void testLoadJsonString() {
assertNotNull(result.getValue());
}

@Test
public void testLoadJsonStringHitsCache() {
LottieResult<LottieComposition> result1 = LottieCompositionFactory.fromJsonStringSync(JSON, "json");
LottieResult<LottieComposition> result2 = LottieCompositionFactory.fromJsonStringSync(JSON, "json");
assertEquals(result1, result2);
}

@Test
public void testLoadDifferentJsonStringsDoesntHitsCache() {
LottieResult<LottieComposition> result1 = LottieCompositionFactory.fromJsonStringSync(JSON, "jso1");
LottieResult<LottieComposition> result2 = LottieCompositionFactory.fromJsonStringSync(JSON, "json2");
assertNotEquals(result1, result2);
}

@Test
public void testLoadInvalidJsonString() {
LottieResult<LottieComposition> result = LottieCompositionFactory.fromJsonStringSync(NOT_JSON, "not_json");
Expand Down

0 comments on commit f609986

Please sign in to comment.