Skip to content

Commit

Permalink
Remove Reference API
Browse files Browse the repository at this point in the history
Summary: Reference has been deprecated for a while. This diff completely removes it in favor of using ComparableDrawable everywhere.

Reviewed By: astreet

Differential Revision: D14241428

fbshipit-source-id: a986f34fda3a05c85d3d57b1e806c5e48c3f485e
  • Loading branch information
pasqualeanatriello authored and facebook-github-bot committed Mar 8, 2019
1 parent 6d25c57 commit b1aa39a
Show file tree
Hide file tree
Showing 36 changed files with 98 additions and 841 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
int DOUBLE = 1;
int ARRAY = 2;
int PRIMITIVE = 3;
int REFERENCE = 4;
int COMPARABLE_DRAWABLE = 4;
int COLLECTION_COMPLEVEL_0 = 5;
int COLLECTION_COMPLEVEL_1 = 6;
int COLLECTION_COMPLEVEL_2 = 7;
Expand All @@ -50,7 +50,7 @@
DOUBLE,
ARRAY,
PRIMITIVE,
REFERENCE,
COMPARABLE_DRAWABLE,
COLLECTION_COMPLEVEL_0,
COLLECTION_COMPLEVEL_1,
COLLECTION_COMPLEVEL_2,
Expand Down
5 changes: 2 additions & 3 deletions litho-core/src/main/java/com/facebook/litho/CommonProps.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import androidx.annotation.StyleRes;
import com.facebook.infer.annotation.ThreadConfined;
import com.facebook.litho.drawable.ComparableDrawable;
import com.facebook.litho.reference.Reference;
import com.facebook.yoga.YogaAlign;
import com.facebook.yoga.YogaDirection;
import com.facebook.yoga.YogaEdge;
Expand Down Expand Up @@ -66,7 +65,7 @@ public interface CommonProps extends CommonPropsCopyable {

void heightPx(@Px int height);

void background(@Nullable Reference<? extends Drawable> background);
void background(@Nullable ComparableDrawable background);

void testKey(String testKey);

Expand Down Expand Up @@ -139,7 +138,7 @@ public interface CommonProps extends CommonPropsCopyable {
void clickHandler(EventHandler<ClickEvent> clickHandler);

@Nullable
Reference<? extends Drawable> getBackground();
ComparableDrawable getBackground();

void longClickHandler(EventHandler<LongClickEvent> longClickHandler);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.facebook.litho;

import android.animation.StateListAnimator;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.ViewOutlineProvider;
import androidx.annotation.AttrRes;
Expand All @@ -28,7 +27,6 @@
import com.facebook.infer.annotation.ThreadConfined;
import com.facebook.litho.config.ComponentsConfiguration;
import com.facebook.litho.drawable.ComparableDrawable;
import com.facebook.litho.reference.Reference;
import com.facebook.yoga.YogaAlign;
import com.facebook.yoga.YogaConstants;
import com.facebook.yoga.YogaDirection;
Expand All @@ -49,7 +47,7 @@ class CommonPropsHolder implements CommonProps {
@Nullable private OtherProps mOtherProps;
@Nullable private NodeInfo mNodeInfo;
@Nullable private LayoutProps mLayoutProps;
@Nullable private Reference<? extends Drawable> mBackground;
@Nullable private ComparableDrawable mBackground;
@Nullable private String mTestKey;
private boolean mWrapInView;
@AttrRes private int mDefStyleAttr;
Expand Down Expand Up @@ -98,7 +96,7 @@ public void heightPx(@Px int height) {
}

@Override
public void background(@Nullable Reference<? extends Drawable> background) {
public void background(@Nullable ComparableDrawable background) {
mPrivateFlags |= PFLAG_BACKGROUND_IS_SET;
mBackground = background;
}
Expand Down Expand Up @@ -286,7 +284,7 @@ public void clickHandler(EventHandler<ClickEvent> clickHandler) {

@Override
@Nullable
public Reference<? extends Drawable> getBackground() {
public ComparableDrawable getBackground() {
return mBackground;
}

Expand Down
42 changes: 7 additions & 35 deletions litho-core/src/main/java/com/facebook/litho/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
import com.facebook.litho.drawable.ComparableDrawable;
import com.facebook.litho.drawable.ComparableResDrawable;
import com.facebook.litho.drawable.DefaultComparableDrawable;
import com.facebook.litho.reference.DrawableReference;
import com.facebook.litho.reference.Reference;
import com.facebook.yoga.YogaAlign;
import com.facebook.yoga.YogaDirection;
import com.facebook.yoga.YogaEdge;
Expand Down Expand Up @@ -1326,27 +1324,12 @@ public T touchExpansionDip(
* @see ComparableDrawable
*/
@Deprecated
public T background(@Nullable Reference<? extends Drawable> background) {
/**
* We want to test the effect of not using References so if we come into this method with an
* actual reference (that is not a DrawableReference) we turn it into a DrawableReference to
* force it to behave as a proper Drawable. This is actually going to be slightly worse for
* memory than just having a reference to the Drawable directly.
*/
if (ComponentsConfiguration.dontUseReferences
&& background != null
&& !(background instanceof DrawableReference)) {
final Drawable backgroundDrawable =
Reference.acquire(getContext().getAndroidContext(), background);

if (backgroundDrawable != null) {
background =
DrawableReference.create(DefaultComparableDrawable.create(backgroundDrawable));
}
public T background(@Nullable Drawable background) {
if (background instanceof ComparableDrawable || background == null) {
return background((ComparableDrawable) background);
}

mComponent.getOrCreateCommonProps().background(background);
return getThis();
return background(DefaultComparableDrawable.create(background));
}

/**
Expand All @@ -1359,19 +1342,8 @@ public T background(@Nullable Reference<? extends Drawable> background) {
* @see ComparableDrawable
*/
public T background(@Nullable ComparableDrawable background) {
return background(background != null ? DrawableReference.create(background) : null);
}

/**
* @deprecated use {@link #background(ComparableDrawable)} more efficient diffing of drawables.
* @see ComparableDrawable
*/
@Deprecated
public T background(@Nullable Drawable background) {
if (background instanceof ComparableDrawable) {
return background((ComparableDrawable) background);
}
return background(background != null ? DefaultComparableDrawable.create(background) : null);
mComponent.getOrCreateCommonProps().background(background);
return getThis();
}

public T backgroundAttr(@AttrRes int resId, @DrawableRes int defaultResId) {
Expand Down Expand Up @@ -1413,7 +1385,7 @@ public T foreground(@Nullable ComparableDrawable foreground) {
*/
@Deprecated
public T foreground(@Nullable Drawable foreground) {
if (foreground instanceof ComparableDrawable) {
if (foreground instanceof ComparableDrawable || foreground == null) {
return foreground((ComparableDrawable) foreground);
}
return foreground(foreground != null ? DefaultComparableDrawable.create(foreground) : null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import android.graphics.drawable.Drawable;
import androidx.annotation.Px;
import com.facebook.infer.annotation.ThreadConfined;
import com.facebook.litho.reference.Reference;
import com.facebook.litho.drawable.ComparableDrawable;
import com.facebook.yoga.YogaDirection;

/**
Expand Down Expand Up @@ -57,7 +57,7 @@ public interface ComponentLayout {

boolean isPaddingSet();

Reference<? extends Drawable> getBackground();
ComparableDrawable getBackground();

YogaDirection getResolvedLayoutDirection();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.facebook.litho;

import com.facebook.litho.annotations.Comparable;
import com.facebook.litho.reference.Reference;
import com.facebook.litho.drawable.ComparableDrawable;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -86,8 +86,8 @@ public static boolean hasEquivalentFields(Object obj1, Object obj2) {
}
break;

case Comparable.REFERENCE:
if (Reference.shouldUpdate((Reference) val1, (Reference) val2)) {
case Comparable.COMPARABLE_DRAWABLE:
if (!((ComparableDrawable) val1).isEquivalentTo((ComparableDrawable) val2)) {
return false;
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.facebook.litho;

import android.graphics.drawable.Drawable;
import com.facebook.litho.reference.Reference;
import com.facebook.litho.drawable.ComparableDrawable;
import com.facebook.yoga.YogaAlign;
import com.facebook.yoga.YogaConstants;
import com.facebook.yoga.YogaDirection;
Expand Down Expand Up @@ -50,7 +50,7 @@ public void setForegroundColor(int color) {
}

@Nullable
public Reference<? extends Drawable> getBackground() {
public ComparableDrawable getBackground() {
return mNode.getBackground();
}

Expand Down
21 changes: 9 additions & 12 deletions litho-core/src/main/java/com/facebook/litho/DrawableComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

import android.content.Context;
import android.graphics.drawable.Drawable;
import com.facebook.litho.reference.Reference;
import com.facebook.litho.drawable.ComparableDrawable;

class DrawableComponent<T extends Drawable> extends Component {

Reference<T> mDrawable;
ComparableDrawable mDrawable;
int mDrawableWidth;
int mDrawableHeight;

private DrawableComponent(Reference drawable) {
private DrawableComponent(ComparableDrawable drawable) {
super("DrawableComponent");
mDrawable = drawable;
}
Expand All @@ -48,7 +48,7 @@ protected void onMount(
Object content) {
MatrixDrawable drawable = (MatrixDrawable) content;

drawable.mount(Reference.acquire(context.getAndroidContext(), getDrawable()));
drawable.mount(getDrawable());
}

@Override
Expand All @@ -65,10 +65,7 @@ protected void onUnmount(
ComponentContext context,
Object mountedContent) {
final MatrixDrawable<T> matrixDrawable = (MatrixDrawable<T>) mountedContent;
final T innerMountedDrawable = matrixDrawable.getMountedDrawable();

matrixDrawable.unmount();
Reference.release(context.getAndroidContext(), innerMountedDrawable, getDrawable());
}

@Override
Expand All @@ -81,19 +78,19 @@ public MountType getMountType() {
return MountType.DRAWABLE;
}

public static DrawableComponent create(Reference<? extends Drawable> drawable) {
public static DrawableComponent create(ComparableDrawable drawable) {
return new DrawableComponent<>(drawable);
}

@Override
protected boolean shouldUpdate(Component previous, Component next) {
final Reference previousReference = ((DrawableComponent) previous).getDrawable();
final Reference nextReference = ((DrawableComponent) next).getDrawable();
final ComparableDrawable previousDrawable = ((DrawableComponent) previous).getDrawable();
final ComparableDrawable nextDrawable = ((DrawableComponent) next).getDrawable();

return Reference.shouldUpdate(previousReference, nextReference);
return !previousDrawable.isEquivalentTo(nextDrawable);
}

private Reference<T> getDrawable() {
private ComparableDrawable getDrawable() {
return mDrawable;
}

Expand Down
32 changes: 11 additions & 21 deletions litho-core/src/main/java/com/facebook/litho/InternalNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
import com.facebook.litho.drawable.ComparableDrawable;
import com.facebook.litho.drawable.ComparableResDrawable;
import com.facebook.litho.drawable.DefaultComparableDrawable;
import com.facebook.litho.reference.DrawableReference;
import com.facebook.litho.reference.Reference;
import com.facebook.yoga.YogaAlign;
import com.facebook.yoga.YogaBaselineFunction;
import com.facebook.yoga.YogaConstants;
Expand Down Expand Up @@ -123,7 +121,7 @@ class InternalNode implements ComponentLayout {
private boolean mDuplicateParentState;
private long mPrivateFlags;

private @Nullable Reference<? extends Drawable> mBackground;
private @Nullable ComparableDrawable mBackground;
private @Nullable ComparableDrawable mForeground;
private final int[] mBorderColors = new int[Border.EDGE_COUNT];
private final float[] mBorderRadius = new float[Border.RADIUS_COUNT];
Expand Down Expand Up @@ -290,7 +288,7 @@ public boolean isPaddingSet() {
}

@Override
public @Nullable Reference<? extends Drawable> getBackground() {
public @Nullable ComparableDrawable getBackground() {
return mBackground;
}

Expand Down Expand Up @@ -796,21 +794,13 @@ InternalNode child(InternalNode child) {
return this;
}

/**
* @deprecated use {@link #background(ComparableDrawable)} more efficient diffing of drawables.
*/
@Deprecated
InternalNode background(@Nullable Reference<? extends Drawable> background) {
InternalNode background(@Nullable ComparableDrawable background) {
mPrivateFlags |= PFLAG_BACKGROUND_IS_SET;
mBackground = background;
setPaddingFromDrawableReference(background);
setPaddingFromBackground(background);
return this;
}

InternalNode background(@Nullable ComparableDrawable background) {
return background(background != null ? DrawableReference.create(background) : null);
}

/**
* @deprecated use {@link #background(ComparableDrawable)} more efficient diffing of drawables.
*/
Expand All @@ -819,6 +809,7 @@ InternalNode background(@Nullable Drawable background) {
if (background instanceof ComparableDrawable) {
return background((ComparableDrawable) background);
}

return background(background != null ? DefaultComparableDrawable.create(background) : null);
}

Expand All @@ -839,6 +830,10 @@ InternalNode backgroundColor(@ColorInt int backgroundColor) {
*/
@Deprecated
InternalNode foreground(@Nullable Drawable foreground) {
if (foreground instanceof ComparableDrawable) {
return foreground((ComparableDrawable) foreground);
}

return foreground(foreground != null ? DefaultComparableDrawable.create(foreground) : null);
}

Expand Down Expand Up @@ -1616,11 +1611,8 @@ static void assertContextSpecificStyleNotSet(InternalNode node) {
return mNestedTreeProps != null ? mNestedTreeProps.mPendingTreeProps : null;
}

private <T extends Drawable> void setPaddingFromDrawableReference(@Nullable Reference<T> ref) {
if (ref == null) {
return;
}
final T drawable = Reference.acquire(mComponentContext.getAndroidContext(), ref);
private <T extends Drawable> void setPaddingFromBackground(Drawable drawable) {

if (drawable != null) {
final Rect backgroundPadding = new Rect();
if (getDrawablePadding(drawable, backgroundPadding)) {
Expand All @@ -1629,8 +1621,6 @@ private <T extends Drawable> void setPaddingFromDrawableReference(@Nullable Refe
paddingPx(RIGHT, backgroundPadding.right);
paddingPx(BOTTOM, backgroundPadding.bottom);
}

Reference.release(mComponentContext.getAndroidContext(), drawable, ref);
}
}

Expand Down
Loading

0 comments on commit b1aa39a

Please sign in to comment.