mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
RN: Cleanup ImageLoadEvent Logic (Android)
Summary: Cleans up `ImageLoadEvent` to minimize constructor confusion and to make the dispatching logic more predictable. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D22023141 fbshipit-source-id: 17e66de867f51121a3f9a6b782dbad700a54231a
This commit is contained in:
committed by
Facebook GitHub Bot
parent
76fe94e8b0
commit
90997c26e3
@@ -30,41 +30,45 @@ public class ImageLoadEvent extends Event<ImageLoadEvent> {
|
||||
public static final int ON_PROGRESS = 5;
|
||||
|
||||
private final int mEventType;
|
||||
private final @Nullable String mImageUri;
|
||||
private final @Nullable String mErrorMessage;
|
||||
private final @Nullable String mSourceUri;
|
||||
private final int mWidth;
|
||||
private final int mHeight;
|
||||
private final @Nullable String mImageError;
|
||||
|
||||
public ImageLoadEvent(int viewId, @ImageEventType int eventType) {
|
||||
this(viewId, eventType, null);
|
||||
public static final ImageLoadEvent createLoadStartEvent(int viewId) {
|
||||
return new ImageLoadEvent(viewId, ON_LOAD_START);
|
||||
}
|
||||
|
||||
public ImageLoadEvent(int viewId, @ImageEventType int eventType, boolean error, String message) {
|
||||
this(viewId, eventType, null, 0, 0, message);
|
||||
public static final ImageLoadEvent createLoadEvent(
|
||||
int viewId, @Nullable String imageUri, int width, int height) {
|
||||
return new ImageLoadEvent(viewId, ON_LOAD, null, imageUri, width, height);
|
||||
}
|
||||
|
||||
public ImageLoadEvent(int viewId, @ImageEventType int eventType, String imageUri) {
|
||||
this(viewId, eventType, imageUri, 0, 0, null);
|
||||
public static final ImageLoadEvent createErrorEvent(int viewId, Throwable throwable) {
|
||||
return new ImageLoadEvent(viewId, ON_ERROR, throwable.getMessage(), null, 0, 0);
|
||||
}
|
||||
|
||||
public ImageLoadEvent(
|
||||
int viewId, @ImageEventType int eventType, @Nullable String imageUri, int width, int height) {
|
||||
this(viewId, eventType, imageUri, width, height, null);
|
||||
public static final ImageLoadEvent createLoadEndEvent(int viewId) {
|
||||
return new ImageLoadEvent(viewId, ON_LOAD_END);
|
||||
}
|
||||
|
||||
public ImageLoadEvent(
|
||||
private ImageLoadEvent(int viewId, @ImageEventType int eventType) {
|
||||
this(viewId, eventType, null, null, 0, 0);
|
||||
}
|
||||
|
||||
private ImageLoadEvent(
|
||||
int viewId,
|
||||
@ImageEventType int eventType,
|
||||
@Nullable String imageUri,
|
||||
@Nullable String errorMessage,
|
||||
@Nullable String sourceUri,
|
||||
int width,
|
||||
int height,
|
||||
@Nullable String message) {
|
||||
int height) {
|
||||
super(viewId);
|
||||
mEventType = eventType;
|
||||
mImageUri = imageUri;
|
||||
mErrorMessage = errorMessage;
|
||||
mSourceUri = sourceUri;
|
||||
mWidth = width;
|
||||
mHeight = height;
|
||||
mImageError = message;
|
||||
}
|
||||
|
||||
public static String eventNameForType(@ImageEventType int eventType) {
|
||||
@@ -100,26 +104,29 @@ public class ImageLoadEvent extends Event<ImageLoadEvent> {
|
||||
public void dispatch(RCTEventEmitter rctEventEmitter) {
|
||||
WritableMap eventData = null;
|
||||
|
||||
if (mImageUri != null || (mEventType == ON_LOAD || mEventType == ON_ERROR)) {
|
||||
eventData = Arguments.createMap();
|
||||
|
||||
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);
|
||||
} else if (mEventType == ON_ERROR) {
|
||||
eventData.putString("error", mImageError);
|
||||
}
|
||||
switch (mEventType) {
|
||||
case ON_LOAD:
|
||||
eventData = Arguments.createMap();
|
||||
// TODO: Remove this (to be less redundant and to be consistent with iOS).
|
||||
eventData.putString("uri", mSourceUri);
|
||||
eventData.putMap("source", createEventDataSource());
|
||||
break;
|
||||
case ON_ERROR:
|
||||
eventData = Arguments.createMap();
|
||||
// TODO: Remove this (to be less redundant and to be consistent with iOS).
|
||||
eventData.putString("uri", mSourceUri);
|
||||
eventData.putString("error", mErrorMessage);
|
||||
break;
|
||||
}
|
||||
|
||||
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), eventData);
|
||||
}
|
||||
|
||||
private WritableMap createEventDataSource() {
|
||||
WritableMap source = Arguments.createMap();
|
||||
source.putDouble("width", mWidth);
|
||||
source.putDouble("height", mHeight);
|
||||
source.putString("url", mSourceUri);
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,8 +240,7 @@ public class ReactImageView extends GenericDraweeView {
|
||||
new BaseControllerListener<ImageInfo>() {
|
||||
@Override
|
||||
public void onSubmit(String id, Object callerContext) {
|
||||
mEventDispatcher.dispatchEvent(
|
||||
new ImageLoadEvent(getId(), ImageLoadEvent.ON_LOAD_START));
|
||||
mEventDispatcher.dispatchEvent(ImageLoadEvent.createLoadStartEvent(getId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -249,22 +248,18 @@ public class ReactImageView extends GenericDraweeView {
|
||||
String id, @Nullable final ImageInfo imageInfo, @Nullable Animatable animatable) {
|
||||
if (imageInfo != null) {
|
||||
mEventDispatcher.dispatchEvent(
|
||||
new ImageLoadEvent(
|
||||
ImageLoadEvent.createLoadEvent(
|
||||
getId(),
|
||||
ImageLoadEvent.ON_LOAD,
|
||||
mImageSource.getSource(),
|
||||
imageInfo.getWidth(),
|
||||
imageInfo.getHeight()));
|
||||
mEventDispatcher.dispatchEvent(
|
||||
new ImageLoadEvent(getId(), ImageLoadEvent.ON_LOAD_END));
|
||||
mEventDispatcher.dispatchEvent(ImageLoadEvent.createLoadEndEvent(getId()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(String id, Throwable throwable) {
|
||||
mEventDispatcher.dispatchEvent(
|
||||
new ImageLoadEvent(
|
||||
getId(), ImageLoadEvent.ON_ERROR, true, throwable.getMessage()));
|
||||
mEventDispatcher.dispatchEvent(ImageLoadEvent.createErrorEvent(getId(), throwable));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user