Skip to content

Commit

Permalink
add onError event and wire image load failure to it (#1687)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Rousal authored and reseul committed Mar 5, 2018
1 parent 3973386 commit 63322df
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
28 changes: 28 additions & 0 deletions ReactWindows/ReactNative.Shared/Views/Image/ReactImageLoadEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ public class ReactImageLoadEvent : Event
/// </summary>
public const int OnLoadEnd = 3;

/// <summary>
/// The event identifier for image load error.
/// </summary>
public const int OnError = 4;

private readonly int _eventType;
private readonly string _imageUri;
private readonly int _width;
private readonly int _height;
private readonly string _error;

/// <summary>
/// Instantiates a <see cref="ReactImageLoadEvent"/>.
Expand Down Expand Up @@ -57,6 +63,18 @@ public ReactImageLoadEvent(int viewId, int eventType, string imageUri, int width
_height = height;
}

/// <summary>
/// Instantiates a <see cref="ReactImageLoadEvent"/>.
/// </summary>
/// <param name="viewId">The view identifier.</param>
/// <param name="error">The error string.</param>
public ReactImageLoadEvent(int viewId, string error)
: base(viewId)
{
_eventType = OnError;
_error = error;
}

/// <summary>
/// The name of the event.
/// </summary>
Expand All @@ -72,6 +90,8 @@ public override string EventName
return "topLoad";
case OnLoadEnd:
return "topLoadEnd";
case OnError:
return "topError";
default:
throw new InvalidOperationException(
Invariant($"Invalid image event '{_eventType}'."));
Expand Down Expand Up @@ -135,6 +155,14 @@ public override void Dispatch(RCTEventEmitter eventEmitter)
}
}

if (_eventType == OnError)
{
eventData = new JObject()
{
{ "error", _error }
};
}

eventEmitter.receiveEvent(ViewTag, EventName, eventData);
}
}
Expand Down
32 changes: 23 additions & 9 deletions ReactWindows/ReactNative/Views/Image/ReactImageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ public override IReadOnlyDictionary<string, object> ExportedCustomDirectEventTyp
{ "registrationName", "onLoadEnd" }
}
},
{
"topError",
new Dictionary<string, object>
{
{ "registrationName", "onError" }
}
},
};
}
}
Expand Down Expand Up @@ -231,21 +238,28 @@ public override void SetDimensions(Border view, Dimensions dimensions)
SetUriFromMultipleSources(view);
}

private void OnImageFailed(Border view)
private void OnImageFailed(Border view, Exception e)
{
if (!view.HasTag())
{
// View may have been unmounted, ignore.
return;
}

view.GetReactContext()
var eventDispatcher = view.GetReactContext()
.GetNativeModule<UIManagerModule>()
.EventDispatcher
.DispatchEvent(
new ReactImageLoadEvent(
view.GetTag(),
ReactImageLoadEvent.OnLoadEnd));
.EventDispatcher;

eventDispatcher.DispatchEvent(
new ReactImageLoadEvent(
view.GetTag(),
e.Message));

eventDispatcher.DispatchEvent(
new ReactImageLoadEvent(
view.GetTag(),
ReactImageLoadEvent.OnLoadEnd));

}

private void OnImageStatusUpdate(Border view, ImageLoadStatus status, ImageMetadata metadata)
Expand Down Expand Up @@ -287,9 +301,9 @@ private async void SetUriFromSingleSource(Border view, string source)
imageBrush.ImageSource = image;
OnImageStatusUpdate(view, ImageLoadStatus.OnLoadEnd, metadata);
}
catch
catch (Exception e)
{
OnImageFailed(view);
OnImageFailed(view, e);
}
}

Expand Down

0 comments on commit 63322df

Please sign in to comment.