Merge pull request #2578 from dreampiggy/refactory_manager_logic
Refactory manager logic, move code for readability and future maintain
This commit is contained in:
commit
4a52cce18a
|
@ -176,6 +176,7 @@ static id<SDImageLoader> _defaultImageLoader;
|
|||
|
||||
#pragma mark - Private
|
||||
|
||||
// Query cache process
|
||||
- (void)callCacheProcessForOperation:(nonnull SDWebImageCombinedOperation *)operation
|
||||
url:(nullable NSURL *)url
|
||||
options:(SDWebImageOptions)options
|
||||
|
@ -203,6 +204,7 @@ static id<SDImageLoader> _defaultImageLoader;
|
|||
}
|
||||
}
|
||||
|
||||
// Download process
|
||||
- (void)callDownloadProcessForOperation:(nonnull SDWebImageCombinedOperation *)operation
|
||||
url:(nullable NSURL *)url
|
||||
options:(SDWebImageOptions)options
|
||||
|
@ -245,20 +247,7 @@ static id<SDImageLoader> _defaultImageLoader;
|
|||
// Image refresh hit the NSURLCache cache, do not call the completion block
|
||||
} else if (error) {
|
||||
[self callCompletionBlockForOperation:strongOperation completion:completedBlock error:error url:url];
|
||||
BOOL shouldBlockFailedURL;
|
||||
// Check whether we should block failed url
|
||||
if ([self.delegate respondsToSelector:@selector(imageManager:shouldBlockFailedURL:withError:)]) {
|
||||
shouldBlockFailedURL = [self.delegate imageManager:self shouldBlockFailedURL:url withError:error];
|
||||
} else {
|
||||
shouldBlockFailedURL = ( error.code != NSURLErrorNotConnectedToInternet
|
||||
&& error.code != NSURLErrorCancelled
|
||||
&& error.code != NSURLErrorTimedOut
|
||||
&& error.code != NSURLErrorInternationalRoamingOff
|
||||
&& error.code != NSURLErrorDataNotAllowed
|
||||
&& error.code != NSURLErrorCannotFindHost
|
||||
&& error.code != NSURLErrorCannotConnectToHost
|
||||
&& error.code != NSURLErrorNetworkConnectionLost);
|
||||
}
|
||||
BOOL shouldBlockFailedURL = [self shouldBlockFailedURLWithURL:url error:error];
|
||||
|
||||
if (shouldBlockFailedURL) {
|
||||
SD_LOCK(self.failedURLsLock);
|
||||
|
@ -272,50 +261,7 @@ static id<SDImageLoader> _defaultImageLoader;
|
|||
SD_UNLOCK(self.failedURLsLock);
|
||||
}
|
||||
|
||||
SDImageCacheType storeCacheType = SDImageCacheTypeAll;
|
||||
if (context[SDWebImageContextStoreCacheType]) {
|
||||
storeCacheType = [context[SDWebImageContextStoreCacheType] integerValue];
|
||||
}
|
||||
id<SDWebImageCacheKeyFilter> cacheKeyFilter = context[SDWebImageContextCacheKeyFilter];
|
||||
NSString *key = [self cacheKeyForURL:url cacheKeyFilter:cacheKeyFilter];
|
||||
id<SDImageTransformer> transformer = context[SDWebImageContextImageTransformer];
|
||||
id<SDWebImageCacheSerializer> cacheSerializer = context[SDWebImageContextCacheSerializer];
|
||||
if (downloadedImage && (!downloadedImage.sd_isAnimated || (options & SDWebImageTransformAnimatedImage)) && transformer) {
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
|
||||
@autoreleasepool {
|
||||
UIImage *transformedImage = [transformer transformedImageWithImage:downloadedImage forKey:key];
|
||||
if (transformedImage && finished) {
|
||||
NSString *transformerKey = [transformer transformerKey];
|
||||
NSString *cacheKey = SDTransformedKeyForKey(key, transformerKey);
|
||||
BOOL imageWasTransformed = ![transformedImage isEqual:downloadedImage];
|
||||
NSData *cacheData;
|
||||
// pass nil if the image was transformed, so we can recalculate the data from the image
|
||||
if (cacheSerializer) {
|
||||
cacheData = [cacheSerializer cacheDataWithImage:transformedImage originalData:(imageWasTransformed ? nil : downloadedData) imageURL:url];
|
||||
} else {
|
||||
cacheData = (imageWasTransformed ? nil : downloadedData);
|
||||
}
|
||||
[self.imageCache storeImage:transformedImage imageData:cacheData forKey:cacheKey cacheType:storeCacheType completion:nil];
|
||||
}
|
||||
|
||||
[self callCompletionBlockForOperation:strongOperation completion:completedBlock image:transformedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url];
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (downloadedImage && finished) {
|
||||
if (cacheSerializer) {
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
|
||||
@autoreleasepool {
|
||||
NSData *cacheData = [cacheSerializer cacheDataWithImage:downloadedImage originalData:downloadedData imageURL:url];
|
||||
[self.imageCache storeImage:downloadedImage imageData:cacheData forKey:key cacheType:storeCacheType completion:nil];
|
||||
}
|
||||
});
|
||||
} else {
|
||||
[self.imageCache storeImage:downloadedImage imageData:downloadedData forKey:key cacheType:storeCacheType completion:nil];
|
||||
}
|
||||
}
|
||||
[self callCompletionBlockForOperation:strongOperation completion:completedBlock image:downloadedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url];
|
||||
}
|
||||
[self callStoreCacheProcessForOperation:strongOperation url:url options:options context:context downloadedImage:downloadedImage downloadedData:downloadedData finished:finished progress:progressBlock completed:completedBlock];
|
||||
}
|
||||
|
||||
if (finished) {
|
||||
|
@ -332,6 +278,62 @@ static id<SDImageLoader> _defaultImageLoader;
|
|||
}
|
||||
}
|
||||
|
||||
// Store cache process
|
||||
- (void)callStoreCacheProcessForOperation:(nonnull SDWebImageCombinedOperation *)operation
|
||||
url:(nullable NSURL *)url
|
||||
options:(SDWebImageOptions)options
|
||||
context:(SDWebImageContext *)context
|
||||
downloadedImage:(nullable UIImage *)downloadedImage
|
||||
downloadedData:(nullable NSData *)downloadedData
|
||||
finished:(BOOL)finished
|
||||
progress:(nullable SDImageLoaderProgressBlock)progressBlock
|
||||
completed:(nullable SDInternalCompletionBlock)completedBlock {
|
||||
SDImageCacheType storeCacheType = SDImageCacheTypeAll;
|
||||
if (context[SDWebImageContextStoreCacheType]) {
|
||||
storeCacheType = [context[SDWebImageContextStoreCacheType] integerValue];
|
||||
}
|
||||
id<SDWebImageCacheKeyFilter> cacheKeyFilter = context[SDWebImageContextCacheKeyFilter];
|
||||
NSString *key = [self cacheKeyForURL:url cacheKeyFilter:cacheKeyFilter];
|
||||
id<SDImageTransformer> transformer = context[SDWebImageContextImageTransformer];
|
||||
id<SDWebImageCacheSerializer> cacheSerializer = context[SDWebImageContextCacheSerializer];
|
||||
if (downloadedImage && (!downloadedImage.sd_isAnimated || (options & SDWebImageTransformAnimatedImage)) && transformer) {
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
|
||||
@autoreleasepool {
|
||||
UIImage *transformedImage = [transformer transformedImageWithImage:downloadedImage forKey:key];
|
||||
if (transformedImage && finished) {
|
||||
NSString *transformerKey = [transformer transformerKey];
|
||||
NSString *cacheKey = SDTransformedKeyForKey(key, transformerKey);
|
||||
BOOL imageWasTransformed = ![transformedImage isEqual:downloadedImage];
|
||||
NSData *cacheData;
|
||||
// pass nil if the image was transformed, so we can recalculate the data from the image
|
||||
if (cacheSerializer && (storeCacheType == SDImageCacheTypeDisk || storeCacheType == SDImageCacheTypeAll)) {
|
||||
cacheData = [cacheSerializer cacheDataWithImage:transformedImage originalData:(imageWasTransformed ? nil : downloadedData) imageURL:url];
|
||||
} else {
|
||||
cacheData = (imageWasTransformed ? nil : downloadedData);
|
||||
}
|
||||
[self.imageCache storeImage:transformedImage imageData:cacheData forKey:cacheKey cacheType:storeCacheType completion:nil];
|
||||
}
|
||||
|
||||
[self callCompletionBlockForOperation:operation completion:completedBlock image:transformedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url];
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (downloadedImage && finished) {
|
||||
if (cacheSerializer && (storeCacheType == SDImageCacheTypeDisk || storeCacheType == SDImageCacheTypeAll)) {
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
|
||||
@autoreleasepool {
|
||||
NSData *cacheData = [cacheSerializer cacheDataWithImage:downloadedImage originalData:downloadedData imageURL:url];
|
||||
[self.imageCache storeImage:downloadedImage imageData:cacheData forKey:key cacheType:storeCacheType completion:nil];
|
||||
}
|
||||
});
|
||||
} else {
|
||||
[self.imageCache storeImage:downloadedImage imageData:downloadedData forKey:key cacheType:storeCacheType completion:nil];
|
||||
}
|
||||
}
|
||||
[self callCompletionBlockForOperation:operation completion:completedBlock image:downloadedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Helper
|
||||
|
||||
- (void)safelyRemoveOperationFromRunning:(nullable SDWebImageCombinedOperation*)operation {
|
||||
|
@ -365,6 +367,33 @@ static id<SDImageLoader> _defaultImageLoader;
|
|||
});
|
||||
}
|
||||
|
||||
- (BOOL)shouldBlockFailedURLWithURL:(nonnull NSURL *)url
|
||||
error:(nonnull NSError *)error {
|
||||
// Check whether we should block failed url
|
||||
BOOL shouldBlockFailedURL;
|
||||
if ([self.delegate respondsToSelector:@selector(imageManager:shouldBlockFailedURL:withError:)]) {
|
||||
shouldBlockFailedURL = [self.delegate imageManager:self shouldBlockFailedURL:url withError:error];
|
||||
} else {
|
||||
NSString *domain = error.domain;
|
||||
NSInteger code = error.code;
|
||||
if ([domain isEqualToString:NSURLErrorDomain]) {
|
||||
shouldBlockFailedURL = ( code != NSURLErrorNotConnectedToInternet
|
||||
&& code != NSURLErrorCancelled
|
||||
&& code != NSURLErrorTimedOut
|
||||
&& code != NSURLErrorInternationalRoamingOff
|
||||
&& code != NSURLErrorDataNotAllowed
|
||||
&& code != NSURLErrorCannotFindHost
|
||||
&& code != NSURLErrorCannotConnectToHost
|
||||
&& code != NSURLErrorNetworkConnectionLost);
|
||||
} else {
|
||||
// Custom loader, don't do extra error code check
|
||||
shouldBlockFailedURL = NO;
|
||||
}
|
||||
}
|
||||
|
||||
return shouldBlockFailedURL;
|
||||
}
|
||||
|
||||
- (SDWebImageContext *)processedContextWithContext:(SDWebImageContext *)context {
|
||||
SDWebImageMutableContext *mutableContext = [SDWebImageMutableContext dictionary];
|
||||
|
||||
|
|
Loading…
Reference in New Issue