Skip to content
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

[feat] Added download progress for NetworkImageNode #1489

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions Source/ASNetworkImageNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (readonly) CGFloat renderedImageQuality;

/**
* Loading progress of the current image.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call it "download progress" to be a bit clearer and consistent with PINRemoteImage.

Suggested change
* Loading progress of the current image.
* Download progress of the current image.

* When downloading a network image, this value would be updated to track downloading progress. ( number between 0.0 and 1.0 )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* When downloading a network image, this value would be updated to track downloading progress. ( number between 0.0 and 1.0 )
* When downloading a network image, this value would be updated to track download progress (value between 0 and 1)

* If the URL is unset, this is 1.0 if defaultImage or image is set to non-nil.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* If the URL is unset, this is 1.0 if defaultImage or image is set to non-nil.
* If the URL is unset, this is 1 if defaultImage or image is set to non-nil.

*/
@property (readonly) CGFloat loadingProgress;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@property (readonly) CGFloat loadingProgress;
@property (readonly) CGFloat downloadProgress;


@end


Expand Down
48 changes: 46 additions & 2 deletions Source/ASNetworkImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ @interface ASNetworkImageNode ()
BOOL _imageWasSetExternally;
CGFloat _currentImageQuality;
CGFloat _renderedImageQuality;
CGFloat _loadingProgress;
BOOL _shouldRenderProgressImages;

struct {
Expand Down Expand Up @@ -155,6 +156,7 @@ - (void)_locked_setImage:(UIImage *)image
// If our image is being set externally, the image quality is 100%
if (imageWasSetExternally) {
[self _setCurrentImageQuality:1.0];
[self _locked_setLoadingProgress:1.0];
}

[self _locked__setImage:image];
Expand Down Expand Up @@ -206,7 +208,9 @@ - (void)setURL:(NSURL *)URL resetToDefault:(BOOL)reset
_imageWasSetExternally = NO;

[self _locked_cancelImageDownloadWithResumePossibility:NO];


[self _locked_setLoadingProgress:0.0];
nguyenhuy marked this conversation as resolved.
Show resolved Hide resolved

_imageLoaded = NO;

_URL = URL;
Expand Down Expand Up @@ -277,6 +281,24 @@ - (void)_setCurrentImageQuality:(CGFloat)imageQuality
});
}

- (void)setLoadingProgress:(CGFloat)loadingProgress
{
ASLockScopeSelf();
_loadingProgress = loadingProgress;
}

- (CGFloat)loadingProgress
{
return ASLockedSelf(_loadingProgress);
}

- (void)_locked_setLoadingProgress:(CGFloat)loadingProgress
Copy link
Member

@nguyenhuy nguyenhuy Jun 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add an explanation why we're dispatching to the main queue here. Something similar to what _setCurrentImageQuality has.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- (void)_locked_setLoadingProgress:(CGFloat)loadingProgress
- (void)_setDownloadProgress:(CGFloat)downloadProgress

{
dispatch_async(dispatch_get_main_queue(), ^{
self.loadingProgress = loadingProgress;
});
}

- (void)setRenderedImageQuality:(CGFloat)renderedImageQuality
{
ASLockScopeSelf();
Expand Down Expand Up @@ -355,6 +377,7 @@ - (void)displayWillStartAsynchronously:(BOOL)asynchronously
UIImage *result = [[_cache synchronouslyFetchedCachedImageWithURL:url] asdk_image];
if (result) {
[self _setCurrentImageQuality:1.0];
[self _locked_setLoadingProgress:1.0];
[self _locked__setImage:result];
_imageLoaded = YES;

Expand Down Expand Up @@ -432,6 +455,17 @@ + (BOOL)useMainThreadDelegateCallbacks

#pragma mark - Progress

- (void)_updateDownloadedProgress:(CGFloat)progress
downloadIdentifier:(nullable id)downloadIdentifier
{
ASLockScopeSelf();
// Getting a result back for a different download identifier, download must not have been successfully canceled
if (ASObjectIsEqual(_downloadIdentifier, downloadIdentifier) == NO && downloadIdentifier != nil) {
return;
}
[self _locked_setLoadingProgress:progress];
}

- (void)handleProgressImage:(UIImage *)progressImage progress:(CGFloat)progress downloadIdentifier:(nullable id)downloadIdentifier
{
ASLockScopeSelf();
Expand Down Expand Up @@ -534,6 +568,7 @@ - (void)_locked_cancelDownloadAndClearImageWithResumePossibility:(BOOL)storeResu

[self _locked_setAnimatedImage:nil];
[self _setCurrentImageQuality:0.0];
[self _locked_setLoadingProgress:0.0];
[self _locked__setImage:_defaultImage];

_imageLoaded = NO;
Expand Down Expand Up @@ -591,7 +626,14 @@ - (void)_downloadImageWithCompletion:(void (^)(id <ASImageContainerProtocol> ima
}

dispatch_queue_t callbackQueue = [self callbackQueue];
ASImageDownloaderProgress downloadProgress = NULL;
__weak __typeof__(self) weakSelf = self;
ASImageDownloaderProgress downloadProgress = ^(CGFloat progress){
__typeof__(self) strongSelf = weakSelf;
if (strongSelf == nil) {
return;
}
[strongSelf _updateDownloadedProgress:progress downloadIdentifier:downloadIdentifier];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bit more concise.

if (strongSelf != nil) {
  [strongSelf _updateDownloadedProgress:progress downloadIdentifier:downloadIdentifier];
}

};
ASImageDownloaderCompletion completion = ^(id <ASImageContainerProtocol> _Nullable imageContainer, NSError * _Nullable error, id _Nullable downloadIdentifier, id _Nullable userInfo) {
if (finished != NULL) {
finished(imageContainer, error, downloadIdentifier, userInfo);
Expand Down Expand Up @@ -722,6 +764,7 @@ - (void)_lazilyLoadImageIfNecessary
self->_imageLoaded = YES;

[self _setCurrentImageQuality:1.0];
[self _locked_setLoadingProgress:1.0];

if (self->_delegateFlags.delegateDidLoadImageWithInfo) {
ASUnlockScope(self);
Expand Down Expand Up @@ -761,6 +804,7 @@ - (void)_lazilyLoadImageIfNecessary
UIImage *newImage;
if (imageContainer != nil) {
[strongSelf _setCurrentImageQuality:1.0];
[self _locked_setLoadingProgress:1.0];
NSData *animatedImageData = [imageContainer asdk_animatedImageData];
if (animatedImageData && strongSelf->_downloaderFlags.downloaderImplementsAnimatedImage) {
id animatedImage = [strongSelf->_downloader animatedImageWithData:animatedImageData];
Expand Down