Add GlobalImageLoadListener

Reviewed By: AaaChiuuu

Differential Revision: D5932865

fbshipit-source-id: 700983d7fc44c4ee5f77b58ef54cec62869009e0
This commit is contained in:
Felix Oghina 2017-09-29 08:37:22 -07:00 committed by Facebook Github Bot
parent 15130d5fa2
commit 7320ca5c7a
5 changed files with 62 additions and 28 deletions

View File

@ -9,18 +9,12 @@
package com.facebook.react.flat; package com.facebook.react.flat;
import javax.annotation.Nullable;
import java.util.LinkedList;
import java.util.List;
import android.content.Context; import android.content.Context;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter; import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Animatable; import android.graphics.drawable.Animatable;
import android.net.Uri; import android.net.Uri;
import com.facebook.drawee.controller.ControllerListener; import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.drawable.ScalingUtils.ScaleType; import com.facebook.drawee.drawable.ScalingUtils.ScaleType;
import com.facebook.drawee.generic.GenericDraweeHierarchy; import com.facebook.drawee.generic.GenericDraweeHierarchy;
@ -31,12 +25,16 @@ import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.facebook.infer.annotation.Assertions; import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.views.image.GlobalImageLoadListener;
import com.facebook.react.views.image.ImageLoadEvent; import com.facebook.react.views.image.ImageLoadEvent;
import com.facebook.react.views.image.ImageResizeMode; import com.facebook.react.views.image.ImageResizeMode;
import com.facebook.react.views.image.ReactImageView; import com.facebook.react.views.image.ReactImageView;
import com.facebook.react.views.imagehelper.ImageSource; import com.facebook.react.views.imagehelper.ImageSource;
import com.facebook.react.views.imagehelper.MultiSourceHelper; import com.facebook.react.views.imagehelper.MultiSourceHelper;
import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult; import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.Nullable;
/** /**
* DrawImageWithDrawee is a DrawCommand that can draw a local or remote image. * DrawImageWithDrawee is a DrawCommand that can draw a local or remote image.
@ -48,6 +46,7 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult;
private static final String LOCAL_CONTENT_SCHEME = "content"; private static final String LOCAL_CONTENT_SCHEME = "content";
private final List<ImageSource> mSources = new LinkedList<>(); private final List<ImageSource> mSources = new LinkedList<>();
private final @Nullable GlobalImageLoadListener mGlobalImageLoadListener;
private @Nullable DraweeRequestHelper mRequestHelper; private @Nullable DraweeRequestHelper mRequestHelper;
private @Nullable PorterDuffColorFilter mColorFilter; private @Nullable PorterDuffColorFilter mColorFilter;
private ScaleType mScaleType = ImageResizeMode.defaultValue(); private ScaleType mScaleType = ImageResizeMode.defaultValue();
@ -59,6 +58,10 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult;
private int mFadeDuration = ReactImageView.REMOTE_IMAGE_FADE_DURATION_MS; private int mFadeDuration = ReactImageView.REMOTE_IMAGE_FADE_DURATION_MS;
private @Nullable FlatViewGroup.InvalidateCallback mCallback; private @Nullable FlatViewGroup.InvalidateCallback mCallback;
public DrawImageWithDrawee(@Nullable GlobalImageLoadListener globalImageLoadListener) {
mGlobalImageLoadListener = globalImageLoadListener;
}
@Override @Override
public boolean hasImageRequest() { public boolean hasImageRequest() {
return !mSources.isEmpty(); return !mSources.isEmpty();
@ -274,6 +277,9 @@ import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult;
.setResizeOptions(resizeOptions) .setResizeOptions(resizeOptions)
.setProgressiveRenderingEnabled(mProgressiveRenderingEnabled) .setProgressiveRenderingEnabled(mProgressiveRenderingEnabled)
.build(); .build();
if (mGlobalImageLoadListener != null) {
mGlobalImageLoadListener.onLoadAttempt(source.getUri());
}
ImageRequest cachedImageRequest = null; ImageRequest cachedImageRequest = null;
if (cachedSource != null) { if (cachedSource != null) {

View File

@ -11,7 +11,7 @@ package com.facebook.react.flat;
import com.facebook.drawee.backends.pipeline.Fresco; import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.AbstractDraweeControllerBuilder; import com.facebook.drawee.controller.AbstractDraweeControllerBuilder;
import com.facebook.react.views.image.GlobalImageLoadListener;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public final class RCTImageViewManager extends FlatViewManager { public final class RCTImageViewManager extends FlatViewManager {
@ -19,6 +19,7 @@ public final class RCTImageViewManager extends FlatViewManager {
/* package */ static final String REACT_CLASS = "RCTImageView"; /* package */ static final String REACT_CLASS = "RCTImageView";
private @Nullable AbstractDraweeControllerBuilder mDraweeControllerBuilder; private @Nullable AbstractDraweeControllerBuilder mDraweeControllerBuilder;
private @Nullable GlobalImageLoadListener mGlobalImageLoadListener;
private final @Nullable Object mCallerContext; private final @Nullable Object mCallerContext;
public RCTImageViewManager() { public RCTImageViewManager() {
@ -28,7 +29,15 @@ public final class RCTImageViewManager extends FlatViewManager {
public RCTImageViewManager( public RCTImageViewManager(
AbstractDraweeControllerBuilder draweeControllerBuilder, AbstractDraweeControllerBuilder draweeControllerBuilder,
Object callerContext) { Object callerContext) {
this(draweeControllerBuilder, null, callerContext);
}
public RCTImageViewManager(
AbstractDraweeControllerBuilder draweeControllerBuilder,
@Nullable GlobalImageLoadListener globalImageLoadListener,
Object callerContext) {
mDraweeControllerBuilder = draweeControllerBuilder; mDraweeControllerBuilder = draweeControllerBuilder;
mGlobalImageLoadListener = globalImageLoadListener;
mCallerContext = callerContext; mCallerContext = callerContext;
} }
@ -39,7 +48,7 @@ public final class RCTImageViewManager extends FlatViewManager {
@Override @Override
public RCTImageView createShadowNodeInstance() { public RCTImageView createShadowNodeInstance() {
return new RCTImageView(new DrawImageWithDrawee()); return new RCTImageView(new DrawImageWithDrawee(mGlobalImageLoadListener));
} }
@Override @Override

View File

@ -0,0 +1,12 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.react.views.image;
import android.net.Uri;
/** Listener interface for global image loading events. */
public interface GlobalImageLoadListener {
/** Called when a source has been set on an ImageView, but before it is actually loaded. */
void onLoadAttempt(Uri uri);
}

View File

@ -9,14 +9,8 @@
package com.facebook.react.views.image; package com.facebook.react.views.image;
import javax.annotation.Nullable;
import java.util.Map;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuff.Mode;
import com.facebook.yoga.YogaConstants;
import com.facebook.drawee.backends.pipeline.Fresco; import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.AbstractDraweeControllerBuilder; import com.facebook.drawee.controller.AbstractDraweeControllerBuilder;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException; import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
@ -30,6 +24,9 @@ import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewProps; import com.facebook.react.uimanager.ViewProps;
import com.facebook.react.uimanager.annotations.ReactProp; import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.annotations.ReactPropGroup; import com.facebook.react.uimanager.annotations.ReactPropGroup;
import com.facebook.yoga.YogaConstants;
import java.util.Map;
import javax.annotation.Nullable;
@ReactModule(name = ReactImageManager.REACT_CLASS) @ReactModule(name = ReactImageManager.REACT_CLASS)
public class ReactImageManager extends SimpleViewManager<ReactImageView> { public class ReactImageManager extends SimpleViewManager<ReactImageView> {
@ -42,12 +39,20 @@ public class ReactImageManager extends SimpleViewManager<ReactImageView> {
} }
private @Nullable AbstractDraweeControllerBuilder mDraweeControllerBuilder; private @Nullable AbstractDraweeControllerBuilder mDraweeControllerBuilder;
private @Nullable GlobalImageLoadListener mGlobalImageLoadListener;
private final @Nullable Object mCallerContext; private final @Nullable Object mCallerContext;
public ReactImageManager(
AbstractDraweeControllerBuilder draweeControllerBuilder, Object callerContext) {
this(draweeControllerBuilder, null, callerContext);
}
public ReactImageManager( public ReactImageManager(
AbstractDraweeControllerBuilder draweeControllerBuilder, AbstractDraweeControllerBuilder draweeControllerBuilder,
@Nullable GlobalImageLoadListener globalImageLoadListener,
Object callerContext) { Object callerContext) {
mDraweeControllerBuilder = draweeControllerBuilder; mDraweeControllerBuilder = draweeControllerBuilder;
mGlobalImageLoadListener = globalImageLoadListener;
mCallerContext = callerContext; mCallerContext = callerContext;
} }
@ -71,9 +76,7 @@ public class ReactImageManager extends SimpleViewManager<ReactImageView> {
@Override @Override
public ReactImageView createViewInstance(ThemedReactContext context) { public ReactImageView createViewInstance(ThemedReactContext context) {
return new ReactImageView( return new ReactImageView(
context, context, getDraweeControllerBuilder(), mGlobalImageLoadListener, getCallerContext());
getDraweeControllerBuilder(),
getCallerContext());
} }
// In JS this is Image.props.source // In JS this is Image.props.source

View File

@ -9,12 +9,6 @@
package com.facebook.react.views.image; package com.facebook.react.views.image;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapShader; import android.graphics.BitmapShader;
@ -30,10 +24,7 @@ import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.widget.Toast; import android.widget.Toast;
import com.facebook.common.util.UriUtil; import com.facebook.common.util.UriUtil;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.yoga.YogaConstants;
import com.facebook.drawee.controller.AbstractDraweeControllerBuilder; import com.facebook.drawee.controller.AbstractDraweeControllerBuilder;
import com.facebook.drawee.controller.BaseControllerListener; import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener; import com.facebook.drawee.controller.ControllerListener;
@ -54,15 +45,21 @@ import com.facebook.imagepipeline.request.Postprocessor;
import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.FloatUtil; import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.modules.fresco.ReactNetworkImageRequest; import com.facebook.react.modules.fresco.ReactNetworkImageRequest;
import com.facebook.react.uimanager.FloatUtil;
import com.facebook.react.uimanager.PixelUtil; import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.UIManagerModule; import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.EventDispatcher; import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.views.imagehelper.ImageSource; import com.facebook.react.views.imagehelper.ImageSource;
import com.facebook.react.views.imagehelper.MultiSourceHelper; import com.facebook.react.views.imagehelper.MultiSourceHelper;
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult; import com.facebook.react.views.imagehelper.MultiSourceHelper.MultiSourceResult;
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
import com.facebook.yoga.YogaConstants;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.Nullable;
/** /**
* Wrapper class around Fresco's GenericDraweeView, enabling persisting props across multiple view * Wrapper class around Fresco's GenericDraweeView, enabling persisting props across multiple view
@ -163,6 +160,7 @@ public class ReactImageView extends GenericDraweeView {
private @Nullable IterativeBoxBlurPostProcessor mIterativeBoxBlurPostProcessor; private @Nullable IterativeBoxBlurPostProcessor mIterativeBoxBlurPostProcessor;
private @Nullable ControllerListener mControllerListener; private @Nullable ControllerListener mControllerListener;
private @Nullable ControllerListener mControllerForTesting; private @Nullable ControllerListener mControllerForTesting;
private @Nullable GlobalImageLoadListener mGlobalImageLoadListener;
private final @Nullable Object mCallerContext; private final @Nullable Object mCallerContext;
private int mFadeDurationMs = -1; private int mFadeDurationMs = -1;
private boolean mProgressiveRenderingEnabled; private boolean mProgressiveRenderingEnabled;
@ -178,11 +176,13 @@ public class ReactImageView extends GenericDraweeView {
public ReactImageView( public ReactImageView(
Context context, Context context,
AbstractDraweeControllerBuilder draweeControllerBuilder, AbstractDraweeControllerBuilder draweeControllerBuilder,
@Nullable GlobalImageLoadListener globalImageLoadListener,
@Nullable Object callerContext) { @Nullable Object callerContext) {
super(context, buildHierarchy(context)); super(context, buildHierarchy(context));
mScaleType = ImageResizeMode.defaultValue(); mScaleType = ImageResizeMode.defaultValue();
mDraweeControllerBuilder = draweeControllerBuilder; mDraweeControllerBuilder = draweeControllerBuilder;
mRoundedCornerPostprocessor = new RoundedCornerPostprocessor(); mRoundedCornerPostprocessor = new RoundedCornerPostprocessor();
mGlobalImageLoadListener = globalImageLoadListener;
mCallerContext = callerContext; mCallerContext = callerContext;
mSources = new LinkedList<>(); mSources = new LinkedList<>();
} }
@ -416,6 +416,10 @@ public class ReactImageView extends GenericDraweeView {
ImageRequest imageRequest = ReactNetworkImageRequest.fromBuilderWithHeaders(imageRequestBuilder, mHeaders); ImageRequest imageRequest = ReactNetworkImageRequest.fromBuilderWithHeaders(imageRequestBuilder, mHeaders);
if (mGlobalImageLoadListener != null) {
mGlobalImageLoadListener.onLoadAttempt(mImageSource.getUri());
}
// This builder is reused // This builder is reused
mDraweeControllerBuilder.reset(); mDraweeControllerBuilder.reset();