Skip to content

Commit

Permalink
Include image dimensions & url in Image's onLoad callback
Browse files Browse the repository at this point in the history
Summary:
This is similar to the iOS feature that was implemented by 84f68c3.

**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 #9608

Differential Revision: D3797659

Pulled By: foghina

fbshipit-source-id: ea83a907bf6b0b61d9bc1e90fc7c64b7132db81f
  • Loading branch information
Adam Comella authored and Facebook Github Bot 9 committed Sep 1, 2016
1 parent 1e5d52b commit bcf48e7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
Expand Up @@ -36,22 +36,32 @@ public class ImageLoadEvent extends Event<ImageLoadEvent> {

private final int mEventType;
private final @Nullable String mImageUri;
private final int mWidth;
private final int mHeight;

public ImageLoadEvent(int viewId, @ImageEventType int eventType) {
this(viewId, eventType, null);
}

public ImageLoadEvent(int viewId, @ImageEventType int eventType, String imageUri) {
this(viewId, eventType, imageUri, 0, 0);
}

public ImageLoadEvent(
int viewId,
@ImageEventType int eventType,
@Nullable String imageUri) {
@Nullable String imageUri,
int width,
int height) {
super(viewId);
mEventType = eventType;
mImageUri = imageUri;
mWidth = width;
mHeight = height;
}

public static String eventNameForType(@ImageEventType int eventType) {
switch(eventType) {
switch (eventType) {
case ON_ERROR:
return "topError";
case ON_LOAD:
Expand Down Expand Up @@ -82,10 +92,25 @@ public short getCoalescingKey() {
@Override
public void dispatch(RCTEventEmitter rctEventEmitter) {
WritableMap eventData = null;
if (mImageUri != null) {

if (mImageUri != null || mEventType == ON_LOAD) {
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);
}
}
Expand Up @@ -201,7 +201,8 @@ public void onFinalImageSet(
@Nullable Animatable animatable) {
if (imageInfo != null) {
mEventDispatcher.dispatchEvent(
new ImageLoadEvent(getId(), ImageLoadEvent.ON_LOAD));
new ImageLoadEvent(getId(), ImageLoadEvent.ON_LOAD,
mImageSource.getSource(), imageInfo.getWidth(), imageInfo.getHeight()));
mEventDispatcher.dispatchEvent(
new ImageLoadEvent(getId(), ImageLoadEvent.ON_LOAD_END));
}
Expand Down

0 comments on commit bcf48e7

Please sign in to comment.