Skip to content

Commit

Permalink
LottieCompositionFactory: Add factory methods that take an okio Sourc…
Browse files Browse the repository at this point in the history
…e. (#2527)
  • Loading branch information
vanniktech authored Aug 5, 2024
1 parent 6b70fcd commit a52e6d0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.util.Base64;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.RawRes;
Expand Down Expand Up @@ -48,6 +47,7 @@

import okio.BufferedSource;
import okio.Okio;
import okio.Source;

/**
* Helpers to create or cache a LottieComposition.
Expand Down Expand Up @@ -363,7 +363,7 @@ public static LottieResult<LottieComposition> fromJsonInputStreamSync(InputStrea
*/
@WorkerThread
public static LottieResult<LottieComposition> fromJsonInputStreamSync(InputStream stream, @Nullable String cacheKey, boolean close) {
return fromJsonReaderSync(JsonReader.of(buffer(source(stream))), cacheKey, close);
return fromJsonSourceSync(source(stream), cacheKey, close);
}

/**
Expand All @@ -384,7 +384,7 @@ public static LottieTask<LottieComposition> fromJson(final JSONObject json, @Nul
*/
@Deprecated
@WorkerThread
public static LottieResult<LottieComposition> fromJsonSync(JSONObject json, @Nullable String cacheKey) {
public static LottieResult<LottieComposition> fromJsonSync(final JSONObject json, @Nullable String cacheKey) {
return fromJsonStringSync(json.toString(), cacheKey);
}

Expand All @@ -402,26 +402,41 @@ 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);
return fromJsonSourceSync(source(stream), cacheKey);
}

public static LottieTask<LottieComposition> fromJsonSource(final Source source, @Nullable final String cacheKey) {
return cache(cacheKey, () -> fromJsonSourceSync(source, cacheKey), () -> Utils.closeQuietly(source));
}

@WorkerThread
public static LottieResult<LottieComposition> fromJsonSourceSync(final Source source, @Nullable String cacheKey) {
return fromJsonSourceSync(source, cacheKey, true);
}

@WorkerThread
public static LottieResult<LottieComposition> fromJsonSourceSync(final Source source, @Nullable String cacheKey,
boolean close) {
return fromJsonReaderSyncInternal(JsonReader.of(buffer(source)), cacheKey, close);
}

public static LottieTask<LottieComposition> fromJsonReader(final JsonReader reader, @Nullable final String cacheKey) {
return cache(cacheKey, () -> fromJsonReaderSync(reader, cacheKey), () -> Utils.closeQuietly(reader));
}

@WorkerThread
public static LottieResult<LottieComposition> fromJsonReaderSync(com.airbnb.lottie.parser.moshi.JsonReader reader, @Nullable String cacheKey) {
public static LottieResult<LottieComposition> fromJsonReaderSync(final JsonReader reader, @Nullable String cacheKey) {
return fromJsonReaderSync(reader, cacheKey, true);
}

@WorkerThread
public static LottieResult<LottieComposition> fromJsonReaderSync(com.airbnb.lottie.parser.moshi.JsonReader reader, @Nullable String cacheKey,
public static LottieResult<LottieComposition> fromJsonReaderSync(final JsonReader reader, @Nullable String cacheKey,
boolean close) {
return fromJsonReaderSyncInternal(reader, cacheKey, close);
}

private static LottieResult<LottieComposition> fromJsonReaderSyncInternal(
com.airbnb.lottie.parser.moshi.JsonReader reader, @Nullable String cacheKey, boolean close) {
JsonReader reader, @Nullable String cacheKey, boolean close) {
try {
final LottieComposition cachedComposition = cacheKey == null ? null : LottieCompositionCache.getInstance().get(cacheKey);
if (cachedComposition != null) {
Expand Down Expand Up @@ -556,7 +571,7 @@ private static LottieResult<LottieComposition> fromZipStreamSyncInternal(Context
} else if (entry.getName().equalsIgnoreCase("manifest.json")) { //ignore .lottie manifest
inputStream.closeEntry();
} else if (entry.getName().contains(".json")) {
com.airbnb.lottie.parser.moshi.JsonReader reader = JsonReader.of(buffer(source(inputStream)));
JsonReader reader = JsonReader.of(buffer(source(inputStream)));
composition = LottieCompositionFactory.fromJsonReaderSyncInternal(reader, null, false).getValue();
} else if (entryName.contains(".png") || entryName.contains(".webp") || entryName.contains(".jpg") || entryName.contains(".jpeg")) {
String[] splitName = entryName.split("/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
import static okio.Okio.buffer;
import static okio.Okio.source;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame;

import okio.Source;

@SuppressWarnings("ReferenceEquality")
public class LottieCompositionFactoryTest extends BaseTest {
Expand Down Expand Up @@ -67,6 +68,14 @@ public void testLoadInvalidJsonString() {
assertNull(result.getValue());
}

@Test
public void testLoadJsonSource() {
Source source = source(new ByteArrayInputStream(JSON.getBytes()));
LottieResult<LottieComposition> result = LottieCompositionFactory.fromJsonSourceSync(source, "json");
assertNull(result.getException());
assertNotNull(result.getValue());
}

@Test
public void testLoadJsonReader() {
JsonReader reader = JsonReader.of(buffer(source(new ByteArrayInputStream(JSON.getBytes()))));
Expand Down

0 comments on commit a52e6d0

Please sign in to comment.