diff --git a/android/src/main/java/com/dylanvann/fastimage/FastImageRequestListener.java b/android/src/main/java/com/dylanvann/fastimage/FastImageRequestListener.java new file mode 100644 index 0000000..62cd757 --- /dev/null +++ b/android/src/main/java/com/dylanvann/fastimage/FastImageRequestListener.java @@ -0,0 +1,52 @@ +package com.dylanvann.fastimage; + +import android.graphics.drawable.Drawable; + +import com.bumptech.glide.load.DataSource; +import com.bumptech.glide.load.engine.GlideException; +import com.bumptech.glide.request.RequestListener; +import com.bumptech.glide.request.target.ImageViewTarget; +import com.bumptech.glide.request.target.Target; +import com.facebook.react.bridge.WritableNativeMap; +import com.facebook.react.uimanager.ThemedReactContext; +import com.facebook.react.uimanager.events.RCTEventEmitter; + +public class FastImageRequestListener implements RequestListener { + public static final String REACT_ON_ERROR_EVENT = "onFastImageError"; + public static final String REACT_ON_LOAD_EVENT = "onFastImageLoad"; + public static final String REACT_ON_LOAD_END_EVENT = "onFastImageLoadEnd"; + private static String key = null; + + public FastImageRequestListener(String key) { + this.key = key; + } + + @Override + public boolean onLoadFailed(@android.support.annotation.Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + OkHttpProgressGlideModule.forget(key); + if (!(target instanceof ImageViewTarget)) { + return false; + } + ImageViewWithUrl view = (ImageViewWithUrl) ((ImageViewTarget) target).getView(); + ThemedReactContext context = (ThemedReactContext) view.getContext(); + RCTEventEmitter eventEmitter = context.getJSModule(RCTEventEmitter.class); + int viewId = view.getId(); + eventEmitter.receiveEvent(viewId, REACT_ON_ERROR_EVENT, new WritableNativeMap()); + eventEmitter.receiveEvent(viewId, REACT_ON_LOAD_END_EVENT, new WritableNativeMap()); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + if (!(target instanceof ImageViewTarget)) { + return false; + } + ImageViewWithUrl view = (ImageViewWithUrl) ((ImageViewTarget) target).getView(); + ThemedReactContext context = (ThemedReactContext) view.getContext(); + RCTEventEmitter eventEmitter = context.getJSModule(RCTEventEmitter.class); + int viewId = view.getId(); + eventEmitter.receiveEvent(viewId, REACT_ON_LOAD_EVENT, new WritableNativeMap()); + eventEmitter.receiveEvent(viewId, REACT_ON_LOAD_END_EVENT, new WritableNativeMap()); + return false; + } +}