Include image dimensions & url in Image's onLoad callback

Summary:
This is similar to the iOS feature that was implemented by 84f68c338a.

**Test plan (required)**

Verified that the image dimensions are included in the `onLoad` callback in a test app. Also, this change is used in my team's app.

Adam Comella
Microsoft Corp.
Closes https://github.com/facebook/react-native/pull/9608

Differential Revision: D3797659

Pulled By: foghina

fbshipit-source-id: ea83a907bf6b0b61d9bc1e90fc7c64b7132db81f
This commit is contained in:
Adam Comella 2016-09-01 02:38:37 -07:00 committed by Facebook Github Bot 9
parent 1e5d52bf0b
commit bcf48e74ae
2 changed files with 31 additions and 5 deletions

View File

@ -36,22 +36,32 @@ public class ImageLoadEvent extends Event<ImageLoadEvent> {
private final int mEventType; private final int mEventType;
private final @Nullable String mImageUri; private final @Nullable String mImageUri;
private final int mWidth;
private final int mHeight;
public ImageLoadEvent(int viewId, @ImageEventType int eventType) { public ImageLoadEvent(int viewId, @ImageEventType int eventType) {
this(viewId, eventType, null); this(viewId, eventType, null);
} }
public ImageLoadEvent(int viewId, @ImageEventType int eventType, String imageUri) {
this(viewId, eventType, imageUri, 0, 0);
}
public ImageLoadEvent( public ImageLoadEvent(
int viewId, int viewId,
@ImageEventType int eventType, @ImageEventType int eventType,
@Nullable String imageUri) { @Nullable String imageUri,
int width,
int height) {
super(viewId); super(viewId);
mEventType = eventType; mEventType = eventType;
mImageUri = imageUri; mImageUri = imageUri;
mWidth = width;
mHeight = height;
} }
public static String eventNameForType(@ImageEventType int eventType) { public static String eventNameForType(@ImageEventType int eventType) {
switch(eventType) { switch (eventType) {
case ON_ERROR: case ON_ERROR:
return "topError"; return "topError";
case ON_LOAD: case ON_LOAD:
@ -82,10 +92,25 @@ public class ImageLoadEvent extends Event<ImageLoadEvent> {
@Override @Override
public void dispatch(RCTEventEmitter rctEventEmitter) { public void dispatch(RCTEventEmitter rctEventEmitter) {
WritableMap eventData = null; WritableMap eventData = null;
if (mImageUri != null) {
if (mImageUri != null || mEventType == ON_LOAD) {
eventData = Arguments.createMap(); eventData = Arguments.createMap();
eventData.putString("uri", mImageUri);
if (mImageUri != null) {
eventData.putString("uri", mImageUri);
}
if (mEventType == ON_LOAD) {
WritableMap source = Arguments.createMap();
source.putDouble("width", mWidth);
source.putDouble("height", mHeight);
if (mImageUri != null) {
source.putString("url", mImageUri);
}
eventData.putMap("source", source);
}
} }
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), eventData); rctEventEmitter.receiveEvent(getViewTag(), getEventName(), eventData);
} }
} }

View File

@ -201,7 +201,8 @@ public class ReactImageView extends GenericDraweeView {
@Nullable Animatable animatable) { @Nullable Animatable animatable) {
if (imageInfo != null) { if (imageInfo != null) {
mEventDispatcher.dispatchEvent( mEventDispatcher.dispatchEvent(
new ImageLoadEvent(getId(), ImageLoadEvent.ON_LOAD)); new ImageLoadEvent(getId(), ImageLoadEvent.ON_LOAD,
mImageSource.getSource(), imageInfo.getWidth(), imageInfo.getHeight()));
mEventDispatcher.dispatchEvent( mEventDispatcher.dispatchEvent(
new ImageLoadEvent(getId(), ImageLoadEvent.ON_LOAD_END)); new ImageLoadEvent(getId(), ImageLoadEvent.ON_LOAD_END));
} }