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

LottieCompositionFactory: Add factory methods that take an okio Source. #2527

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

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

/**
* Helpers to create or cache a LottieComposition.
Expand Down Expand Up @@ -363,7 +364,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 fromSourceSync(source(stream), cacheKey, close);
}

/**
Expand All @@ -384,7 +385,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 +403,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 fromSourceSync(source(stream), cacheKey);
}

public static LottieTask<LottieComposition> fromSource(final Source source, @Nullable final String cacheKey) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about naming this fromJsonSource to make it clear that this should be a json file, not zip, gzip, etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it. I've tried to see whether I could do the magic bytes check just like you do in other methods but with that Context parameter it gets awkward which is needed for fonts in case they are embedded.

return cache(cacheKey, () -> fromSourceSync(source, cacheKey), () -> Utils.closeQuietly(source));
}

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

@WorkerThread
public static LottieResult<LottieComposition> fromSourceSync(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 +572,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 @@ -21,6 +21,8 @@
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame;

import okio.Source;

@SuppressWarnings("ReferenceEquality")
public class LottieCompositionFactoryTest extends BaseTest {
private static final String JSON = "{\"v\":\"4.11.1\",\"fr\":60,\"ip\":0,\"op\":180,\"w\":300,\"h\":300,\"nm\":\"Comp 1\",\"ddd\":0,\"assets\":[]," +
Expand Down Expand Up @@ -67,6 +69,14 @@ public void testLoadInvalidJsonString() {
assertNull(result.getValue());
}

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

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