-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from 1 commit
4482e8a
c0409fc
908e8e1
720cc9f
353be9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -133,6 +133,13 @@ NS_ASSUME_NONNULL_BEGIN | |||||
*/ | ||||||
@property (readonly) CGFloat renderedImageQuality; | ||||||
|
||||||
/** | ||||||
* Loading 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 ) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
@property (readonly) CGFloat loadingProgress; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
@end | ||||||
|
||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -46,6 +46,7 @@ @interface ASNetworkImageNode () | |||||
BOOL _imageWasSetExternally; | ||||||
CGFloat _currentImageQuality; | ||||||
CGFloat _renderedImageQuality; | ||||||
CGFloat _loadingProgress; | ||||||
BOOL _shouldRenderProgressImages; | ||||||
|
||||||
struct { | ||||||
|
@@ -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]; | ||||||
|
@@ -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; | ||||||
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
dispatch_async(dispatch_get_main_queue(), ^{ | ||||||
self.loadingProgress = loadingProgress; | ||||||
}); | ||||||
} | ||||||
|
||||||
- (void)setRenderedImageQuality:(CGFloat)renderedImageQuality | ||||||
{ | ||||||
ASLockScopeSelf(); | ||||||
|
@@ -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; | ||||||
|
||||||
|
@@ -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(); | ||||||
|
@@ -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; | ||||||
|
@@ -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]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a bit more concise.
|
||||||
}; | ||||||
ASImageDownloaderCompletion completion = ^(id <ASImageContainerProtocol> _Nullable imageContainer, NSError * _Nullable error, id _Nullable downloadIdentifier, id _Nullable userInfo) { | ||||||
if (finished != NULL) { | ||||||
finished(imageContainer, error, downloadIdentifier, userInfo); | ||||||
|
@@ -722,6 +764,7 @@ - (void)_lazilyLoadImageIfNecessary | |||||
self->_imageLoaded = YES; | ||||||
|
||||||
[self _setCurrentImageQuality:1.0]; | ||||||
[self _locked_setLoadingProgress:1.0]; | ||||||
|
||||||
if (self->_delegateFlags.delegateDidLoadImageWithInfo) { | ||||||
ASUnlockScope(self); | ||||||
|
@@ -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]; | ||||||
|
There was a problem hiding this comment.
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.