[ASNetworkImageNode] Attach progress image handler even if download starts while already visible

This commit is contained in:
Adlai Holler 2016-04-11 12:51:16 -07:00
parent 37c7f6f849
commit 362c41ae80

View File

@ -14,6 +14,7 @@
#import "ASEqualityHelpers.h"
#import "ASThread.h"
#import "ASInternalHelpers.h"
#import "ASDisplayNodeExtras.h"
#if PIN_REMOTE_IMAGE
#import "ASPINRemoteImageDownloader.h"
@ -211,9 +212,8 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};
- (void)visibilityDidChange:(BOOL)isVisible
{
[super visibilityDidChange:isVisible];
ASDN::MutexLocker l(_lock);
if (_downloaderImplementsSetPriority) {
ASDN::MutexLocker l(_lock);
if (_downloadIdentifier != nil) {
if (isVisible) {
[_downloader setPriority:ASImageDownloaderPriorityVisible withDownloadIdentifier:_downloadIdentifier];
@ -222,32 +222,8 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};
}
}
}
if (_downloaderImplementsSetProgress) {
ASDN::MutexLocker l(_lock);
if (_downloadIdentifier != nil) {
__weak __typeof__(self) weakSelf = self;
ASImageDownloaderProgressImage progress = nil;
if (isVisible) {
progress = ^(UIImage * _Nonnull progressImage, id _Nullable downloadIdentifier) {
__typeof__(self) strongSelf = weakSelf;
if (strongSelf == nil) {
return;
}
ASDN::MutexLocker l(strongSelf->_lock);
//Getting a result back for a different download identifier, download must not have been successfully canceled
if (!ASObjectIsEqual(strongSelf->_downloadIdentifier, downloadIdentifier) && downloadIdentifier != nil) {
return;
}
strongSelf.image = progressImage;
};
}
[_downloader setProgressImageBlock:progress callbackQueue:dispatch_get_main_queue() withDownloadIdentifier:_downloadIdentifier];
}
}
[self _updateProgressImageBlockOnDownloaderIfNeeded];
}
- (void)clearFetchedData
@ -277,6 +253,32 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};
#pragma mark - Private methods -- only call with lock.
- (void)_updateProgressImageBlockOnDownloaderIfNeeded
{
if (!_downloaderImplementsSetProgress || _downloadIdentifier == nil) {
return;
}
ASImageDownloaderProgressImage progress = nil;
if (ASInterfaceStateIncludesVisible(self.interfaceState)) {
__weak __typeof__(self) weakSelf = self;
progress = ^(UIImage * _Nonnull progressImage, id _Nullable downloadIdentifier) {
__typeof__(self) strongSelf = weakSelf;
if (strongSelf == nil) {
return;
}
ASDN::MutexLocker l(strongSelf->_lock);
//Getting a result back for a different download identifier, download must not have been successfully canceled
if (ASObjectIsEqual(strongSelf->_downloadIdentifier, downloadIdentifier) == NO && downloadIdentifier != nil) {
return;
}
strongSelf.image = progressImage;
};
}
[_downloader setProgressImageBlock:progress callbackQueue:dispatch_get_main_queue() withDownloadIdentifier:_downloadIdentifier];
}
- (void)_clearImage
{
// Destruction of bigger images on the main thread can be expensive
@ -335,6 +337,7 @@ static const CGSize kMinReleaseImageOnBackgroundSize = {20.0, 20.0};
}];
#pragma clang diagnostic pop
}
[self _updateProgressImageBlockOnDownloaderIfNeeded];
});
}