Skip to content

Commit

Permalink
reduce scope of flag API components
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeta51 committed Jul 1, 2024
1 parent b80ca84 commit 6e2a181
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import androidx.compose.ui.unit.IntSize
import com.airbnb.lottie.AsyncUpdates
import com.airbnb.lottie.LottieComposition
import com.airbnb.lottie.LottieDrawable
import com.airbnb.lottie.LottieFeatureFlags
import com.airbnb.lottie.LottieFeatureFlag
import com.airbnb.lottie.RenderMode
import kotlin.math.roundToInt

Expand Down Expand Up @@ -115,7 +115,7 @@ fun LottieAnimation(
matrix.preTranslate(translation.x.toFloat(), translation.y.toFloat())
matrix.preScale(scale.scaleX, scale.scaleY)

drawable.enableFeatureFlag(LottieFeatureFlags.FeatureFlag.MergePathsApi19, enableMergePaths)
drawable.enableFeatureFlag(LottieFeatureFlag.MergePathsApi19, enableMergePaths)
drawable.setSafeMode(safeMode)
drawable.renderMode = renderMode
drawable.asyncUpdates = asyncUpdates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,21 +403,21 @@ public void setUseCompositionFrameRate(boolean useCompositionFrameRate) {
* instead of using merge paths.
*/
public void enableMergePathsForKitKatAndAbove(boolean enable) {
lottieDrawable.enableFeatureFlag(LottieFeatureFlags.FeatureFlag.MergePathsApi19, enable);
lottieDrawable.enableFeatureFlag(LottieFeatureFlag.MergePathsApi19, enable);
}

/**
* Returns whether merge paths are enabled for KitKat and above.
*/
public boolean isMergePathsEnabledForKitKatAndAbove() {
return lottieDrawable.isFeatureFlagEnabled(LottieFeatureFlags.FeatureFlag.MergePathsApi19);
return lottieDrawable.isFeatureFlagEnabled(LottieFeatureFlag.MergePathsApi19);
}

public void enableFeatureFlag(LottieFeatureFlags.FeatureFlag flag, boolean enable) {
public void enableFeatureFlag(LottieFeatureFlag flag, boolean enable) {
lottieDrawable.enableFeatureFlag(flag, enable);
}

public boolean isFeatureFlagEnabled(LottieFeatureFlags.FeatureFlag flag) {
public boolean isFeatureFlagEnabled(LottieFeatureFlag flag) {
return lottieDrawable.isFeatureFlagEnabled(flag);
}

Expand Down
12 changes: 6 additions & 6 deletions lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public boolean hasMatte() {

@Deprecated
public boolean enableMergePathsForKitKatAndAbove() {
return lottieFeatureFlags.isFlagEnabled(LottieFeatureFlags.FeatureFlag.MergePathsApi19);
return lottieFeatureFlags.isFlagEnabled(LottieFeatureFlag.MergePathsApi19);
}

/**
Expand All @@ -300,18 +300,18 @@ public boolean enableMergePathsForKitKatAndAbove() {
*/
@Deprecated
public void enableMergePathsForKitKatAndAbove(boolean enable) {
boolean changed = lottieFeatureFlags.enableFlag(LottieFeatureFlags.FeatureFlag.MergePathsApi19, enable);
boolean changed = lottieFeatureFlags.enableFlag(LottieFeatureFlag.MergePathsApi19, enable);
if (composition != null && changed) {
buildCompositionLayer();
}
}

/**
* @deprecated Replaced by {@link #enableFeatureFlag(LottieFeatureFlags.FeatureFlag, boolean)}
* @deprecated Replaced by {@link #enableFeatureFlag(LottieFeatureFlag, boolean)}
*/
@Deprecated
public boolean isMergePathsEnabledForKitKatAndAbove() {
return lottieFeatureFlags.isFlagEnabled(LottieFeatureFlags.FeatureFlag.MergePathsApi19);
return lottieFeatureFlags.isFlagEnabled(LottieFeatureFlag.MergePathsApi19);
}

/**
Expand All @@ -321,14 +321,14 @@ public boolean isMergePathsEnabledForKitKatAndAbove() {
* Please ensure that the animation supported by the enabled feature looks acceptable across all
* targeted API levels.
*/
public void enableFeatureFlag(LottieFeatureFlags.FeatureFlag flag, boolean enable) {
public void enableFeatureFlag(LottieFeatureFlag flag, boolean enable) {
boolean changed = lottieFeatureFlags.enableFlag(flag, enable);
if (composition != null && changed) {
buildCompositionLayer();
}
}

public boolean isFeatureFlagEnabled(LottieFeatureFlags.FeatureFlag flag) {
public boolean isFeatureFlagEnabled(LottieFeatureFlag flag) {
return lottieFeatureFlags.isFlagEnabled(flag);
}

Expand Down
18 changes: 18 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/LottieFeatureFlag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.airbnb.lottie;

import android.os.Build;

public enum LottieFeatureFlag {
/**
* Merge paths currently don't work if the the operand shape is entirely contained within the
* first shape. If you need to cut out one shape from another shape, use an even-odd fill type
* instead of using merge paths.
*/
MergePathsApi19(Build.VERSION_CODES.KITKAT);

public final int minRequiredSdkVersion;

LottieFeatureFlag(int minRequiredSdkVersion) {
this.minRequiredSdkVersion = minRequiredSdkVersion;
}
}
22 changes: 4 additions & 18 deletions lottie/src/main/java/com/airbnb/lottie/LottieFeatureFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,15 @@

import java.util.HashSet;

public class LottieFeatureFlags {
public enum FeatureFlag {
/**
* Merge paths currently don't work if the the operand shape is entirely contained within the
* first shape. If you need to cut out one shape from another shape, use an even-odd fill type
* instead of using merge paths.
*/
MergePathsApi19(Build.VERSION_CODES.KITKAT);

public final int minRequiredSdkVersion;

FeatureFlag(int minRequiredSdkVersion) {
this.minRequiredSdkVersion = minRequiredSdkVersion;
}
}
class LottieFeatureFlags {

private final HashSet<FeatureFlag> enabledFlags = new HashSet<>();
private final HashSet<LottieFeatureFlag> enabledFlags = new HashSet<>();

/**
* Returns true if the flag was changed.
*/
@SuppressLint("DefaultLocale")
public boolean enableFlag(FeatureFlag flag, boolean enable) {
public boolean enableFlag(LottieFeatureFlag flag, boolean enable) {
if (enable) {
if (Build.VERSION.SDK_INT < flag.minRequiredSdkVersion) {
Logger.warning(String.format("%s is not supported pre SDK %d", flag.name(), flag.minRequiredSdkVersion));
Expand All @@ -41,7 +27,7 @@ public boolean enableFlag(FeatureFlag flag, boolean enable) {
}
}

public boolean isFlagEnabled(FeatureFlag flag) {
public boolean isFlagEnabled(LottieFeatureFlag flag) {
return enabledFlags.contains(flag);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import com.airbnb.lottie.LottieComposition;
import com.airbnb.lottie.LottieDrawable;
import com.airbnb.lottie.LottieFeatureFlags;
import com.airbnb.lottie.LottieFeatureFlag;
import com.airbnb.lottie.animation.content.Content;
import com.airbnb.lottie.animation.content.MergePathsContent;
import com.airbnb.lottie.model.layer.BaseLayer;
Expand Down Expand Up @@ -61,7 +61,7 @@ public boolean isHidden() {
}

@Override @Nullable public Content toContent(LottieDrawable drawable, LottieComposition composition, BaseLayer layer) {
if (!drawable.isFeatureFlagEnabled(LottieFeatureFlags.FeatureFlag.MergePathsApi19)) {
if (!drawable.isFeatureFlagEnabled(LottieFeatureFlag.MergePathsApi19)) {
Logger.warning("Animation contains merge paths but they are disabled.");
return null;
}
Expand Down

0 comments on commit 6e2a181

Please sign in to comment.