Skip to content

Commit

Permalink
Add more delegate methods for monitoring network image node progress (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestmama authored and nguyenhuy committed Nov 27, 2018
1 parent a255953 commit 4d1ffcc
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
37 changes: 37 additions & 0 deletions Source/ASNetworkImageNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,42 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)imageNodeDidStartFetchingData:(ASNetworkImageNode *)imageNode;

/**
* Notification that the image node will load image from cache
*
* @param imageNode The sender.
*
* @discussion Called on the main thread.
*/
- (void)imageNodeWillLoadImageFromCache:(ASNetworkImageNode *)imageNode;

/**
* Notification that the image node finished loading image from cache
*
* @param imageNode The sender.
*
* @discussion Called on the main thread.
*/
- (void)imageNodeDidLoadImageFromCache:(ASNetworkImageNode *)imageNode;

/**
* Notification that the image node will load image from network
*
* @param imageNode The sender.
*
* @discussion Called on the main thread.
*/
- (void)imageNodeWillLoadImageFromNetwork:(ASNetworkImageNode *)imageNode;

/**
* Notification that the image node will start display
*
* @param imageNode The sender.
*
* @discussion Called on the main thread.
*/
- (void)imageNodeWillStartDisplayAsynchronously:(ASNetworkImageNode *)imageNode;

/**
* Notification that the image node finished downloading an image, with additional info.
* If implemented, this method will be called instead of `imageNode:didLoadImage:`.
Expand Down Expand Up @@ -195,6 +231,7 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)imageNodeDidFinishDecoding:(ASNetworkImageNode *)imageNode;


@end

NS_ASSUME_NONNULL_END
39 changes: 37 additions & 2 deletions Source/ASNetworkImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ @interface ASNetworkImageNode ()
BOOL _shouldRenderProgressImages;

struct {
unsigned int delegateWillStartDisplayAsynchronously:1;
unsigned int delegateWillLoadImageFromCache:1;
unsigned int delegateWillLoadImageFromNetwork:1;
unsigned int delegateDidStartFetchingData:1;
unsigned int delegateDidFailWithError:1;
unsigned int delegateDidFinishDecoding:1;
unsigned int delegateDidLoadImage:1;
unsigned int delegateDidLoadImageFromCache:1;
unsigned int delegateDidLoadImageWithInfo:1;
} _delegateFlags;

Expand Down Expand Up @@ -297,10 +301,14 @@ - (void)setDelegate:(id<ASNetworkImageNodeDelegate>)delegate
ASLockScopeSelf();
_delegate = delegate;

_delegateFlags.delegateWillStartDisplayAsynchronously = [delegate respondsToSelector:@selector(imageNodeWillStartDisplayAsynchronously:)];
_delegateFlags.delegateWillLoadImageFromCache = [delegate respondsToSelector:@selector(imageNodeWillLoadImageFromCache:)];
_delegateFlags.delegateWillLoadImageFromNetwork = [delegate respondsToSelector:@selector(imageNodeWillLoadImageFromNetwork:)];
_delegateFlags.delegateDidStartFetchingData = [delegate respondsToSelector:@selector(imageNodeDidStartFetchingData:)];
_delegateFlags.delegateDidFailWithError = [delegate respondsToSelector:@selector(imageNode:didFailWithError:)];
_delegateFlags.delegateDidFinishDecoding = [delegate respondsToSelector:@selector(imageNodeDidFinishDecoding:)];
_delegateFlags.delegateDidLoadImage = [delegate respondsToSelector:@selector(imageNode:didLoadImage:)];
_delegateFlags.delegateDidLoadImageFromCache = [delegate respondsToSelector:@selector(imageNodeDidLoadImageFromCache:)];
_delegateFlags.delegateDidLoadImageWithInfo = [delegate respondsToSelector:@selector(imageNode:didLoadImage:info:)];
}

Expand Down Expand Up @@ -335,6 +343,17 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously
{
[super displayWillStartAsynchronously:asynchronously];

id<ASNetworkImageNodeDelegate> delegate;
BOOL notifyDelegate;
{
ASLockScopeSelf();
notifyDelegate = _delegateFlags.delegateWillStartDisplayAsynchronously;
delegate = _delegate;
}
if (notifyDelegate) {
[delegate imageNodeWillStartDisplayAsynchronously:self];
}

if (asynchronously == NO && _cacheFlags.cacheSupportsSynchronousFetch) {
ASLockScopeSelf();

Expand All @@ -350,10 +369,10 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously
if (_delegateFlags.delegateDidLoadImageWithInfo) {
ASUnlockScope(self);
let info = [[ASNetworkImageLoadInfo alloc] initWithURL:url sourceType:ASNetworkImageSourceSynchronousCache downloadIdentifier:nil userInfo:nil];
[_delegate imageNode:self didLoadImage:result info:info];
[delegate imageNode:self didLoadImage:result info:info];
} else if (_delegateFlags.delegateDidLoadImage) {
ASUnlockScope(self);
[_delegate imageNode:self didLoadImage:result];
[delegate imageNode:self didLoadImage:result];
}
}
}
Expand Down Expand Up @@ -616,6 +635,9 @@ - (void)_lazilyLoadImageIfNecessary
[self lock];
__weak id<ASNetworkImageNodeDelegate> delegate = _delegate;
BOOL delegateDidStartFetchingData = _delegateFlags.delegateDidStartFetchingData;
BOOL delegateWillLoadImageFromCache = _delegateFlags.delegateWillLoadImageFromCache;
BOOL delegateWillLoadImageFromNetwork = _delegateFlags.delegateWillLoadImageFromNetwork;
BOOL delegateDidLoadImageFromCache = _delegateFlags.delegateDidLoadImageFromCache;
BOOL isImageLoaded = _imageLoaded;
NSURL *URL = _URL;
id currentDownloadIdentifier = _downloadIdentifier;
Expand Down Expand Up @@ -773,18 +795,31 @@ - (void)_lazilyLoadImageIfNecessary
}

if ([imageContainer asdk_image] == nil && self->_downloader != nil) {
if (delegateWillLoadImageFromNetwork) {
[delegate imageNodeWillLoadImageFromNetwork:self];
}
[self _downloadImageWithCompletion:^(id<ASImageContainerProtocol> imageContainer, NSError *error, id downloadIdentifier, id userInfo) {
finished(imageContainer, error, downloadIdentifier, ASNetworkImageSourceDownload, userInfo);
}];
} else {
if (delegateDidLoadImageFromCache) {
[delegate imageNodeDidLoadImageFromCache:self];
}
as_log_verbose(ASImageLoadingLog(), "Decached image for %@ img: %@ url: %@", self, [imageContainer asdk_image], URL);
finished(imageContainer, nil, nil, ASNetworkImageSourceAsynchronousCache, nil);
}
};

if (delegateWillLoadImageFromCache) {
[delegate imageNodeWillLoadImageFromCache:self];
}
[_cache cachedImageWithURL:URL
callbackQueue:[self callbackQueue]
completion:completion];
} else {
if (delegateWillLoadImageFromNetwork) {
[delegate imageNodeWillLoadImageFromNetwork:self];
}
[self _downloadImageWithCompletion:^(id<ASImageContainerProtocol> imageContainer, NSError *error, id downloadIdentifier, id userInfo) {
finished(imageContainer, error, downloadIdentifier, ASNetworkImageSourceDownload, userInfo);
}];
Expand Down

0 comments on commit 4d1ffcc

Please sign in to comment.