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

Pull out LottieComposition factory methods into inner class #137

Merged
merged 6 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions LottieSample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ android {
targetSdkVersion 25
versionCode 3
versionName "1.0.3"
testInstrumentationRunner "com.airbnb.lottie.TestRunner"
}
buildTypes {
release {
Expand Down
81 changes: 42 additions & 39 deletions LottieSample/src/androidTest/java/com/airbnb/lottie/LottieTest.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
package com.airbnb.lottie;


import android.test.ActivityInstrumentationTestCase2;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.airbnb.lottie.samples.MainActivity;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* Run these with: ./gradlew --daemon recordMode screenshotTests
* If you run that command, it completes successfully, and nothing shows up in git, then you haven't broken anything!
* Run these with: ./gradlew recordMode screenshotTests
* If you run that command, it completes successfully, and nothing shows up in git, then you
* haven't broken anything!
*/
public class LottieTest extends ActivityInstrumentationTestCase2<MainActivity> {

public LottieTest() {
super(MainActivity.class);
}
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LottieTest {
@Rule public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(
MainActivity.class);

@Test
public void testAll() {
TestRobot.testAnimation(getActivity(), "9squares-AlBoardman.json");
TestRobot.testAnimation(getActivity(), "EmptyState.json");
TestRobot.testAnimation(getActivity(), "HamburgerArrow.json");
TestRobot.testAnimation(getActivity(), "LottieLogo1.json");
TestRobot.testAnimation(getActivity(), "LottieLogo2.json");
TestRobot.testAnimation(getActivity(), "MotionCorpse-Jrcanest.json");
TestRobot.testAnimation(getActivity(), "PinJump.json");
TestRobot.testAnimation(getActivity(), "TwitterHeart.json");
TestRobot.testAnimation(getActivity(), "Tests/Hosts.json");
TestRobot.testAnimation(getActivity(), "Tests/LightBulb.json", null,
@Test public void testAll() {
TestRobot.testAnimation(activityRule.getActivity(), "9squares-AlBoardman.json");
TestRobot.testAnimation(activityRule.getActivity(), "EmptyState.json");
TestRobot.testAnimation(activityRule.getActivity(), "HamburgerArrow.json");
TestRobot.testAnimation(activityRule.getActivity(), "LottieLogo1.json");
TestRobot.testAnimation(activityRule.getActivity(), "LottieLogo2.json");
TestRobot.testAnimation(activityRule.getActivity(), "MotionCorpse-Jrcanest.json");
TestRobot.testAnimation(activityRule.getActivity(), "PinJump.json");
TestRobot.testAnimation(activityRule.getActivity(), "TwitterHeart.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/Hosts.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/LightBulb.json", null,
new float[]{0f, 0.05f, 0.10f, 0.2f, 0.3f, 0.4f, 0.5f, 1f});
TestRobot.testAnimation(getActivity(), "Tests/LoopPlayOnce.json");
TestRobot.testAnimation(getActivity(), "Tests/Alarm.json");
TestRobot.testAnimation(getActivity(), "Tests/CheckSwitch.json");
TestRobot.testAnimation(getActivity(), "Tests/EllipseTrimPath.json");
TestRobot.testAnimation(getActivity(), "Tests/SplitDimensions.json");
TestRobot.testAnimation(getActivity(), "Tests/TrimPathsFull.json");
TestRobot.testAnimation(getActivity(), "Tests/Laugh4.json");
TestRobot.testAnimation(getActivity(), "Tests/Star.json");
TestRobot.testAnimation(getActivity(), "Tests/Polygon.json");
TestRobot.testAnimation(getActivity(), "Tests/AllSet.json");
TestRobot.testAnimation(getActivity(), "Tests/City.json");
TestRobot.testAnimation(getActivity(), "Tests/PreCompMadness.json");
TestRobot.testAnimation(getActivity(), "Tests/MatteParentPrecomp.json");
TestRobot.testAnimation(getActivity(), "Tests/Image.json", "Tests/weaccept");
TestRobot.testStatic(getActivity(), "Tests/TrimPathFill.json");
TestRobot.testStatic(getActivity(), "Tests/Mask_26.json");
TestRobot.testStatic(getActivity(), "Tests/MatteInv.json");
TestRobot.testStatic(getActivity(), "Tests/MaskInv.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/LoopPlayOnce.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/Alarm.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/CheckSwitch.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/EllipseTrimPath.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/SplitDimensions.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/TrimPathsFull.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/Laugh4.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/Star.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/Polygon.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/AllSet.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/City.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/PreCompMadness.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/MatteParentPrecomp.json");
TestRobot.testAnimation(activityRule.getActivity(), "Tests/Image.json", "Tests/weaccept");
TestRobot.testStatic(activityRule.getActivity(), "Tests/TrimPathFill.json");
TestRobot.testStatic(activityRule.getActivity(), "Tests/Mask_26.json");
TestRobot.testStatic(activityRule.getActivity(), "Tests/MatteInv.json");
TestRobot.testStatic(activityRule.getActivity(), "Tests/MaskInv.json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static void testAnimation(MainActivity activity, String fileName, String imageAs
float[] progress) {
final LottieAnimationView view = new LottieAnimationView(activity);
view.setImageAssetsFolder(imageAssetsFolder);
view.setComposition(LottieComposition.fromFileSync(activity, fileName));
view.setComposition(LottieComposition.Factory.fromFileSync(activity, fileName));
ViewHelpers.setupView(view)
.layout();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package com.airbnb.lottie;

import android.os.Bundle;
import android.support.test.runner.AndroidJUnitRunner;

import com.facebook.testing.screenshot.ScreenshotRunner;

public class TestRunner extends AndroidJUnitRunner {
@Override public void onCreate(Bundle arguments) {
ScreenshotRunner.onCreate(this, arguments);
super.onCreate(arguments);
}

@Override public void finish(int resultCode, Bundle results) {
ScreenshotRunner.onDestroy();
super.finish(resultCode, results);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieComposition;
import com.airbnb.lottie.OnCompositionLoadedListener;

import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -150,8 +151,8 @@ public void onAnimationUpdate(ValueAnimator animation) {
case RC_ASSET:
final String assetName = data.getStringExtra(EXTRA_ANIMATION_NAME);
animationView.setImageAssetsFolder(assetFolders.get(assetName));
LottieComposition.fromAssetFileName(getContext(), assetName,
new LottieComposition.OnCompositionLoadedListener() {
LottieComposition.Factory.fromAssetFileName(getContext(), assetName,
new OnCompositionLoadedListener() {
@Override
public void onCompositionLoaded(LottieComposition composition) {
setComposition(composition, assetName);
Expand Down Expand Up @@ -288,8 +289,8 @@ private void onFileLoaded(final Uri uri) {
return;
}

LottieComposition
.fromInputStream(getContext(), fis, new LottieComposition.OnCompositionLoadedListener() {
LottieComposition.Factory
.fromInputStream(getContext(), fis, new OnCompositionLoadedListener() {
@Override
public void onCompositionLoaded(LottieComposition composition) {
setComposition(composition, uri.getPath());
Expand Down Expand Up @@ -324,8 +325,8 @@ private void loadUrl(String url) {

try {
JSONObject json = new JSONObject(response.body().string());
LottieComposition
.fromJson(getResources(), json, new LottieComposition.OnCompositionLoadedListener() {
LottieComposition.Factory
.fromJson(getResources(), json, new OnCompositionLoadedListener() {
@Override
public void onCompositionLoaded(LottieComposition composition) {
setComposition(composition, "Network Animation");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieComposition;
import com.airbnb.lottie.OnCompositionLoadedListener;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -43,8 +44,8 @@ public LottieFontViewGroup(Context context, AttributeSet attrs, int defStyleAttr

private void init() {
setFocusableInTouchMode(true);
LottieComposition.fromAssetFileName(getContext(), "Mobilo/BlinkingCursor.json",
new LottieComposition.OnCompositionLoadedListener() {
LottieComposition.Factory.fromAssetFileName(getContext(), "Mobilo/BlinkingCursor.json",
new OnCompositionLoadedListener() {
@Override
public void onCompositionLoaded(LottieComposition composition) {
cursorView = new LottieAnimationView(getContext());
Expand Down Expand Up @@ -180,8 +181,8 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
if (compositionMap.containsKey(fileName)) {
addComposition(compositionMap.get(fileName));
} else {
LottieComposition.fromAssetFileName(getContext(), fileName,
new LottieComposition.OnCompositionLoadedListener() {
LottieComposition.Factory.fromAssetFileName(getContext(), fileName,
new OnCompositionLoadedListener() {
@Override
public void onCompositionLoaded(LottieComposition composition) {
compositionMap.put(fileName, composition);
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ animationView.cancelAnimation();
Under the hood, `LottieAnimationView` uses `LottieDrawable` to render its animations. If you need to, you can use the drawable form directly:
```java
LottieDrawable drawable = new LottieDrawable();
LottieComposition.fromAssetFileName(getContext(), "hello-world.json", (composition) -> {
LottieComposition.Factory.fromAssetFileName(getContext(), "hello-world.json", (composition) -> {
drawable.setComposition(composition);
});
```
Expand Down
5 changes: 5 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/Cancellable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.airbnb.lottie;

interface Cancellable {
void cancel();
}
10 changes: 10 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/CompositionLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.airbnb.lottie;

import android.os.AsyncTask;

abstract class CompositionLoader<Params> extends AsyncTask<Params, Void, LottieComposition>
implements Cancellable {
@Override public void cancel() {
cancel(true);
}
}
23 changes: 23 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/FileCompositionLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.airbnb.lottie;

import android.content.res.Resources;

import java.io.InputStream;

final class FileCompositionLoader extends CompositionLoader<InputStream> {
private final Resources res;
private final OnCompositionLoadedListener loadedListener;

FileCompositionLoader(Resources res, OnCompositionLoadedListener loadedListener) {
this.res = res;
this.loadedListener = loadedListener;
}

@Override protected LottieComposition doInBackground(InputStream... params) {
return LottieComposition.Factory.fromInputStream(res, params[0]);
}

@Override protected void onPostExecute(LottieComposition composition) {
loadedListener.onCompositionLoaded(composition);
}
}
23 changes: 23 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/JsonCompositionLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.airbnb.lottie;

import android.content.res.Resources;

import org.json.JSONObject;

final class JsonCompositionLoader extends CompositionLoader<JSONObject> {
private final Resources res;
private final OnCompositionLoadedListener loadedListener;

JsonCompositionLoader(Resources res, OnCompositionLoadedListener loadedListener) {
this.res = res;
this.loadedListener = loadedListener;
}

@Override protected LottieComposition doInBackground(JSONObject... params) {
return LottieComposition.Factory.fromJsonSync(res, params[0]);
}

@Override protected void onPostExecute(LottieComposition composition) {
loadedListener.onCompositionLoaded(composition);
}
}
12 changes: 6 additions & 6 deletions lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public enum CacheStrategy {
private static final Map<String, WeakReference<LottieComposition>> weakRefCache =
new HashMap<>();

private final LottieComposition.OnCompositionLoadedListener loadedListener =
new LottieComposition.OnCompositionLoadedListener() {
private final OnCompositionLoadedListener loadedListener =
new OnCompositionLoadedListener() {
@Override
public void onCompositionLoaded(LottieComposition composition) {
setComposition(composition);
Expand All @@ -67,7 +67,7 @@ public void onCompositionLoaded(LottieComposition composition) {
private final LottieDrawable lottieDrawable = new LottieDrawable();
private String animationName;

@Nullable private LottieComposition.Cancellable compositionLoader;
@Nullable private Cancellable compositionLoader;
/**
* Can be null because it is created async
*/
Expand Down Expand Up @@ -205,8 +205,8 @@ public void setAnimation(final String animationName, final CacheStrategy cacheSt
this.animationName = animationName;
lottieDrawable.cancelAnimation();
cancelLoaderTask();
compositionLoader = LottieComposition.fromAssetFileName(getContext(), animationName,
new LottieComposition.OnCompositionLoadedListener() {
compositionLoader = LottieComposition.Factory.fromAssetFileName(getContext(), animationName,
new OnCompositionLoadedListener() {
@Override
public void onCompositionLoaded(LottieComposition composition) {
if (cacheStrategy == CacheStrategy.Strong) {
Expand All @@ -229,7 +229,7 @@ public void onCompositionLoaded(LottieComposition composition) {
*/
public void setAnimation(final JSONObject json) {
cancelLoaderTask();
compositionLoader = LottieComposition.fromJson(getResources(), json, loadedListener);
compositionLoader = LottieComposition.Factory.fromJson(getResources(), json, loadedListener);
}

private void cancelLoaderTask() {
Expand Down
Loading