Skip to content

Feature/success loaded event #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions WebImage/WebImage/WebImageView.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@interface WebImageView : UIImageView

@property (nonatomic, copy) RCTDirectEventBlock onWebImageError;
@property (nonatomic, copy) RCTDirectEventBlock onWebImageSuccess;
@property (nonatomic) WebImageSource* source;

@end
22 changes: 21 additions & 1 deletion WebImage/WebImage/WebImageView.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
#import "WebImageView.h"

@implementation WebImageView
@implementation WebImageView{
BOOL isCallback;
}

- (void)setSource:(WebImageSource *)source {
_source = source;
isCallback= NO;

SDWebImageManager* manager =[[SDWebImageManager alloc] init];
[manager cachedImageExistsForURL:source.uri completion:^(BOOL inCache) {
if(!inCache || isCallback) return ;
if(_onWebImageSuccess){
_onWebImageSuccess(@{ @"uri":self->_source.uri.absoluteString, @"type" : @"cache"});
}

isCallback = YES;
}];

[self sd_setImageWithURL:source.uri completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (error) {
if (_onWebImageError) {
_onWebImageError(@{@"error":error.description, @"uri":imageURL.absoluteString});
}
return;
}
else{
if(_onWebImageSuccess && !self->isCallback){
self->isCallback = YES;
_onWebImageSuccess(@{ @"uri":imageURL.absoluteString});
}
}
}];
}

Expand Down
1 change: 1 addition & 0 deletions WebImage/WebImage/WebImageViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ - (UIView*)view {
}

RCT_EXPORT_VIEW_PROPERTY(onWebImageError, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onWebImageSuccess, RCTDirectEventBlock);

@end
9 changes: 9 additions & 0 deletions android/src/main/java/org/vovkasm/WebImage/WebImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ private static class WebImageViewTarget extends ViewTarget<WebImageView, Bitmap>
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
view.setBitmap(bitmap);
ThemedReactContext context = view.getThemedReactContext();
if (context != null) {
WritableMap event = Arguments.createMap();
final Uri uri = view.getImageUri();
if (uri != null) {
event.putString("uri", uri.toString());
}
context.getJSModule(RCTEventEmitter.class).receiveEvent(view.getId(), "onWebImageSuccess", event);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
Map<String, String> onErrorEventExport = new HashMap<>();
onErrorEventExport.put("registrationName", "onWebImageError");
exportedEvents.put("onWebImageError", onErrorEventExport);

// declare success event
Map<String, String> onSuccessEventExport = new HashMap<>();
onSuccessEventExport.put("registrationName", "onWebImageSuccess");
exportedEvents.put("onWebImageSuccess", onSuccessEventExport);

return exportedEvents;
}

Expand Down
15 changes: 12 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,23 @@ class WebImage extends React.Component {
* error - error message
* uri - URL that result this error
*/
onError: PropTypes.func
onError: PropTypes.func,
/**
* Callback called on success. With event.nativeEvent object with props:
* uri - URL that result this success
*/
onSuccess : PropTypes.func
}

render () {
const { source, ...props } = this.props
const resolvedSource = resolveAssetSource(source)

return <WebImageView {...props} source={resolvedSource} onWebImageError={this._onError} />
return <WebImageView {...props} source={resolvedSource} onWebImageError={this._onError} onWebImageSuccess={this._onSuccess} />
}

_onSuccess = (e) => {
this.props.onSuccess && this.props.onSuccess(e.nativeEvent)
}

_onError = (e) => {
Expand All @@ -62,7 +71,7 @@ WebImage.defaultProps = {
}

var WebImageView = requireNativeComponent('WebImageView', WebImage, {
nativeOnly: {onWebImageError: true}
nativeOnly: {onWebImageError: true, onWebImageSuccess : true}
})

export default WebImage