Skip to content

Commit

Permalink
Extract more factory classes (#139)
Browse files Browse the repository at this point in the history
Another change for #3

Pretty mechanical and straightforward change. Extracted a bunch of constructors into more static inner factory classes with a newInstance method by convention.
Later on we can pull out all these factories into a separate module that provides the default json parsing implementation.
  • Loading branch information
felipecsl authored and gpeal committed Feb 21, 2017
1 parent c5a6daf commit 127ee14
Show file tree
Hide file tree
Showing 18 changed files with 469 additions and 328 deletions.
57 changes: 29 additions & 28 deletions LottieSample/src/androidTest/java/com/airbnb/lottie/LottieTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,35 @@ public class LottieTest {
MainActivity.class);

@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,
MainActivity activity = activityRule.getActivity();
TestRobot.testAnimation(activity, "9squares-AlBoardman.json");
TestRobot.testAnimation(activity, "EmptyState.json");
TestRobot.testAnimation(activity, "HamburgerArrow.json");
TestRobot.testAnimation(activity, "LottieLogo1.json");
TestRobot.testAnimation(activity, "LottieLogo2.json");
TestRobot.testAnimation(activity, "MotionCorpse-Jrcanest.json");
TestRobot.testAnimation(activity, "PinJump.json");
TestRobot.testAnimation(activity, "TwitterHeart.json");
TestRobot.testAnimation(activity, "Tests/Hosts.json");
TestRobot.testAnimation(activity, "Tests/LightBulb.json", null,
new float[]{0f, 0.05f, 0.10f, 0.2f, 0.3f, 0.4f, 0.5f, 1f});
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");
TestRobot.testAnimation(activity, "Tests/LoopPlayOnce.json");
TestRobot.testAnimation(activity, "Tests/Alarm.json");
TestRobot.testAnimation(activity, "Tests/CheckSwitch.json");
TestRobot.testAnimation(activity, "Tests/EllipseTrimPath.json");
TestRobot.testAnimation(activity, "Tests/SplitDimensions.json");
TestRobot.testAnimation(activity, "Tests/TrimPathsFull.json");
TestRobot.testAnimation(activity, "Tests/Laugh4.json");
TestRobot.testAnimation(activity, "Tests/Star.json");
TestRobot.testAnimation(activity, "Tests/Polygon.json");
TestRobot.testAnimation(activity, "Tests/AllSet.json");
TestRobot.testAnimation(activity, "Tests/City.json");
TestRobot.testAnimation(activity, "Tests/PreCompMadness.json");
TestRobot.testAnimation(activity, "Tests/MatteParentPrecomp.json");
TestRobot.testAnimation(activity, "Tests/Image.json", "Tests/weaccept");
TestRobot.testStatic(activity, "Tests/TrimPathFill.json");
TestRobot.testStatic(activity, "Tests/Mask_26.json");
TestRobot.testStatic(activity, "Tests/MatteInv.json");
TestRobot.testStatic(activity, "Tests/MaskInv.json");
}
}
114 changes: 66 additions & 48 deletions lottie/src/main/java/com/airbnb/lottie/AnimatableTransform.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,81 @@
import org.json.JSONObject;

class AnimatableTransform {
private AnimatablePathValue anchorPoint;
private IAnimatablePathValue position;
private AnimatableScaleValue scale;
private AnimatableFloatValue rotation;
private AnimatableIntegerValue opacity;

AnimatableTransform(LottieComposition composition) {
this.anchorPoint = new AnimatablePathValue();
this.position = new AnimatablePathValue();
this.scale = new AnimatableScaleValue(composition);
this.rotation = new AnimatableFloatValue(composition, 0f);
this.opacity = new AnimatableIntegerValue(composition, 255);
private final AnimatablePathValue anchorPoint;
private final IAnimatablePathValue position;
private final AnimatableScaleValue scale;
private final AnimatableFloatValue rotation;
private final AnimatableIntegerValue opacity;

AnimatableTransform(AnimatablePathValue anchorPoint, IAnimatablePathValue position,
AnimatableScaleValue scale, AnimatableFloatValue rotation, AnimatableIntegerValue opacity) {
this.anchorPoint = anchorPoint;
this.position = position;
this.scale = scale;
this.rotation = rotation;
this.opacity = opacity;
}

AnimatableTransform(JSONObject json, LottieComposition composition) {
JSONObject anchorJson = json.optJSONObject("a");
if (anchorJson != null) {
anchorPoint = new AnimatablePathValue(anchorJson.opt("k"), composition);
} else {
throwMissingTransform("anchor");
static class Factory {
static AnimatableTransform newInstance(LottieComposition composition) {
AnimatablePathValue anchorPoint = new AnimatablePathValue();
IAnimatablePathValue position = new AnimatablePathValue();
AnimatableScaleValue scale = new AnimatableScaleValue(composition);
AnimatableFloatValue rotation = new AnimatableFloatValue(composition, 0f);
AnimatableIntegerValue opacity = new AnimatableIntegerValue(composition, 255);
return new AnimatableTransform(anchorPoint, position, scale, rotation, opacity);
}

JSONObject positionJson = json.optJSONObject("p");
if (positionJson != null) {
position =
AnimatablePathValue.createAnimatablePathOrSplitDimensionPath(positionJson, composition);
} else {
throwMissingTransform("position");
}
static AnimatableTransform newInstance(JSONObject json, LottieComposition composition) {
AnimatablePathValue anchorPoint = null;
IAnimatablePathValue position = null;
AnimatableScaleValue scale = null;
AnimatableFloatValue rotation = null;
AnimatableIntegerValue opacity = null;
JSONObject anchorJson = json.optJSONObject("a");
if (anchorJson != null) {
anchorPoint = new AnimatablePathValue(anchorJson.opt("k"), composition);
} else {
throwMissingTransform("anchor");
}

JSONObject scaleJson = json.optJSONObject("s");
if (scaleJson != null) {
scale = new AnimatableScaleValue(scaleJson, composition, false);
} else {
throwMissingTransform("scale");
}
JSONObject positionJson = json.optJSONObject("p");
if (positionJson != null) {
position =
AnimatablePathValue.createAnimatablePathOrSplitDimensionPath(positionJson, composition);
} else {
throwMissingTransform("position");
}

JSONObject rotationJson = json.optJSONObject("r");
if (rotationJson == null) {
rotationJson = json.optJSONObject("rz");
}
if (rotationJson != null) {
rotation = new AnimatableFloatValue(rotationJson, composition, false);
} else {
throwMissingTransform("rotation");
}
JSONObject scaleJson = json.optJSONObject("s");
if (scaleJson != null) {
scale = new AnimatableScaleValue(scaleJson, composition, false);
} else {
throwMissingTransform("scale");
}

JSONObject rotationJson = json.optJSONObject("r");
if (rotationJson == null) {
rotationJson = json.optJSONObject("rz");
}
if (rotationJson != null) {
rotation = new AnimatableFloatValue(rotationJson, composition, false);
} else {
throwMissingTransform("rotation");
}

JSONObject opacityJson = json.optJSONObject("o");
if (opacityJson != null) {
opacity = new AnimatableIntegerValue(opacityJson, composition, false, true);
} else {
throwMissingTransform("opacity");
JSONObject opacityJson = json.optJSONObject("o");
if (opacityJson != null) {
opacity = new AnimatableIntegerValue(opacityJson, composition, false, true);
} else {
throwMissingTransform("opacity");
}
return new AnimatableTransform(anchorPoint, position, scale, rotation, opacity);
}
}

private void throwMissingTransform(String missingProperty) {
throw new IllegalArgumentException("Missing transform for " + missingProperty);
private static void throwMissingTransform(String missingProperty) {
throw new IllegalArgumentException("Missing transform for " + missingProperty);
}
}

AnimatablePathValue getAnchorPoint() {
Expand Down
16 changes: 12 additions & 4 deletions lottie/src/main/java/com/airbnb/lottie/CircleShape.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ class CircleShape {
private final IAnimatablePathValue position;
private final AnimatablePointValue size;

CircleShape(JSONObject json, LottieComposition composition) {
position = AnimatablePathValue.createAnimatablePathOrSplitDimensionPath(
json.optJSONObject("p"), composition);
size = new AnimatablePointValue(json.optJSONObject("s"), composition);
private CircleShape(IAnimatablePathValue position, AnimatablePointValue size) {
this.position = position;
this.size = size;
}

static class Factory {
static CircleShape newInstance(JSONObject json, LottieComposition composition) {
return new CircleShape(
AnimatablePathValue
.createAnimatablePathOrSplitDimensionPath(json.optJSONObject("p"), composition),
new AnimatablePointValue(json.optJSONObject("s"), composition));
}
}

public IAnimatablePathValue getPosition() {
Expand Down
9 changes: 4 additions & 5 deletions lottie/src/main/java/com/airbnb/lottie/ImageAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import org.json.JSONObject;

class ImageAsset {

private int width;
private int height;
private String id;
private String fileName;
private final int width;
private final int height;
private final String id;
private final String fileName;

ImageAsset(JSONObject imageJson) {
width = imageJson.optInt("w");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import static junit.framework.Assert.assertNotNull;

class ImageAssetBitmapManager {

private final Context context;
private String imagesFolder;
private final Map<String, ImageAsset> imageAssets;
Expand Down
2 changes: 1 addition & 1 deletion lottie/src/main/java/com/airbnb/lottie/Keyframe.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.List;

public class Keyframe<T> {
private static Interpolator LINEAR_INTERPOLATOR = new LinearInterpolator();
private static final Interpolator LINEAR_INTERPOLATOR = new LinearInterpolator();

static <T> List<Keyframe<T>> parseKeyframes(JSONArray json, LottieComposition composition,
float scale, AnimatableValue<T, ?> animatableValue) {
Expand Down
Loading

0 comments on commit 127ee14

Please sign in to comment.