From b88a552fbaa5cbdb907de6f75959ea5df0e035e0 Mon Sep 17 00:00:00 2001 From: Bogdan Poplauschi Date: Wed, 16 Jul 2014 15:17:08 +0300 Subject: [PATCH] Updated all block params and properties, using defined blocks that have var names (for proper autocomplete). Created a SDWebImageNoParamsBlock to distinguish easier from other no params blocks. Fixes #810 --- SDWebImage/SDImageCache.h | 12 +++++++----- SDWebImage/SDImageCache.m | 12 ++++++------ SDWebImage/SDWebImageCompat.h | 2 ++ SDWebImage/SDWebImageDownloader.h | 4 +++- SDWebImage/SDWebImageDownloader.m | 4 ++-- SDWebImage/SDWebImageDownloaderOperation.h | 2 +- SDWebImage/SDWebImageDownloaderOperation.m | 8 ++++++-- SDWebImage/SDWebImageManager.h | 4 +++- SDWebImage/SDWebImageManager.m | 4 ++-- SDWebImage/SDWebImagePrefetcher.h | 6 +++++- SDWebImage/SDWebImagePrefetcher.m | 6 +++--- 11 files changed, 40 insertions(+), 24 deletions(-) diff --git a/SDWebImage/SDImageCache.h b/SDWebImage/SDImageCache.h index e823b65..bde9d5d 100644 --- a/SDWebImage/SDImageCache.h +++ b/SDWebImage/SDImageCache.h @@ -28,6 +28,8 @@ typedef void(^SDWebImageQueryCompletedBlock)(UIImage *image, SDImageCacheType ca typedef void(^SDWebImageCheckCacheCompletionBlock)(BOOL isInCache); +typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger totalSize); + /** * SDImageCache maintains a memory cache and an optional disk cache. Disk cache write operations are performed * asynchronous so it doesn’t add unnecessary latency to the UI. @@ -136,7 +138,7 @@ typedef void(^SDWebImageCheckCacheCompletionBlock)(BOOL isInCache); * @param key The unique image cache key * @param completionBlock An block that should be executed after the image has been removed (optional) */ -- (void)removeImageForKey:(NSString *)key withCompletion:(void (^)())completion; +- (void)removeImageForKey:(NSString *)key withCompletion:(SDWebImageNoParamsBlock)completion; /** * Remove the image from memory and optionally disk cache synchronously @@ -153,7 +155,7 @@ typedef void(^SDWebImageCheckCacheCompletionBlock)(BOOL isInCache); * @param fromDisk Also remove cache entry from disk if YES * @param completionBlock An block that should be executed after the image has been removed (optional) */ -- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(void (^)())completion; +- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(SDWebImageNoParamsBlock)completion; /** * Clear all memory cached images @@ -164,7 +166,7 @@ typedef void(^SDWebImageCheckCacheCompletionBlock)(BOOL isInCache); * Clear all disk cached images. Non-blocking method - returns immediately. * @param completionBlock An block that should be executed after cache expiration completes (optional) */ -- (void)clearDiskOnCompletion:(void (^)())completion; +- (void)clearDiskOnCompletion:(SDWebImageNoParamsBlock)completion; /** * Clear all disk cached images @@ -176,7 +178,7 @@ typedef void(^SDWebImageCheckCacheCompletionBlock)(BOOL isInCache); * Remove all expired cached image from disk. Non-blocking method - returns immediately. * @param completionBlock An block that should be executed after cache expiration completes (optional) */ -- (void)cleanDiskWithCompletionBlock:(void (^)())completionBlock; +- (void)cleanDiskWithCompletionBlock:(SDWebImageNoParamsBlock)completionBlock; /** * Remove all expired cached image from disk @@ -197,7 +199,7 @@ typedef void(^SDWebImageCheckCacheCompletionBlock)(BOOL isInCache); /** * Asynchronously calculate the disk cache's size. */ -- (void)calculateSizeWithCompletionBlock:(void (^)(NSUInteger fileCount, NSUInteger totalSize))completionBlock; +- (void)calculateSizeWithCompletionBlock:(SDWebImageCalculateSizeBlock)completionBlock; /** * Async check if image exists in disk cache already (does not load the image) diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 6ed20d5..59c3471 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -276,7 +276,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { return SDScaledImageForKey(key, image); } -- (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image, SDImageCacheType cacheType))doneBlock { +- (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompletedBlock)doneBlock { if (!doneBlock) { return nil; } @@ -319,7 +319,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { [self removeImageForKey:key withCompletion:nil]; } -- (void)removeImageForKey:(NSString *)key withCompletion:(void (^)())completion { +- (void)removeImageForKey:(NSString *)key withCompletion:(SDWebImageNoParamsBlock)completion { [self removeImageForKey:key fromDisk:YES withCompletion:completion]; } @@ -327,7 +327,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { [self removeImageForKey:key fromDisk:fromDisk withCompletion:nil]; } -- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(void (^)())completion { +- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(SDWebImageNoParamsBlock)completion { if (key == nil) { return; @@ -367,7 +367,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { [self clearDiskOnCompletion:nil]; } -- (void)clearDiskOnCompletion:(void (^)())completion +- (void)clearDiskOnCompletion:(SDWebImageNoParamsBlock)completion { dispatch_async(self.ioQueue, ^{ [_fileManager removeItemAtPath:self.diskCachePath error:nil]; @@ -388,7 +388,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { [self cleanDiskWithCompletionBlock:nil]; } -- (void)cleanDiskWithCompletionBlock:(void (^)())completionBlock { +- (void)cleanDiskWithCompletionBlock:(SDWebImageNoParamsBlock)completionBlock { dispatch_async(self.ioQueue, ^{ NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES]; NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey]; @@ -504,7 +504,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) { return count; } -- (void)calculateSizeWithCompletionBlock:(void (^)(NSUInteger fileCount, NSUInteger totalSize))completionBlock { +- (void)calculateSizeWithCompletionBlock:(SDWebImageCalculateSizeBlock)completionBlock { NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES]; dispatch_async(self.ioQueue, ^{ diff --git a/SDWebImage/SDWebImageCompat.h b/SDWebImage/SDWebImageCompat.h index 3d8d636..a0555fd 100644 --- a/SDWebImage/SDWebImageCompat.h +++ b/SDWebImage/SDWebImageCompat.h @@ -53,6 +53,8 @@ extern UIImage *SDScaledImageForKey(NSString *key, UIImage *image); +typedef void(^SDWebImageNoParamsBlock)(); + #define dispatch_main_sync_safe(block)\ if ([NSThread isMainThread]) {\ block();\ diff --git a/SDWebImage/SDWebImageDownloader.h b/SDWebImage/SDWebImageDownloader.h index a972759..008231a 100644 --- a/SDWebImage/SDWebImageDownloader.h +++ b/SDWebImage/SDWebImageDownloader.h @@ -72,6 +72,8 @@ typedef void(^SDWebImageDownloaderProgressBlock)(NSInteger receivedSize, NSInteg typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage *image, NSData *data, NSError *error, BOOL finished); +typedef NSDictionary *(^SDWebImageDownloaderHeadersFilterBlock)(NSURL *url, NSDictionary *headers); + /** * Asynchronous downloader dedicated and optimized for image loading. */ @@ -120,7 +122,7 @@ typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage *image, NSData *data, * This block will be invoked for each downloading image request, returned * NSDictionary will be used as headers in corresponding HTTP request. */ -@property (nonatomic, strong) NSDictionary *(^headersFilter)(NSURL *url, NSDictionary *headers); +@property (nonatomic, copy) SDWebImageDownloaderHeadersFilterBlock headersFilter; /** * Set a value for a HTTP header to be appended to each download HTTP request. diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index 54c8a6f..60914db 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -104,7 +104,7 @@ static NSString *const kCompletedCallbackKey = @"completed"; return _downloadQueue.maxConcurrentOperationCount; } -- (id )downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSInteger, NSInteger))progressBlock completed:(void (^)(UIImage *, NSData *, NSError *, BOOL))completedBlock { +- (id )downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock { __block SDWebImageDownloaderOperation *operation; __weak SDWebImageDownloader *wself = self; @@ -174,7 +174,7 @@ static NSString *const kCompletedCallbackKey = @"completed"; return operation; } -- (void)addProgressCallback:(void (^)(NSInteger, NSInteger))progressBlock andCompletedBlock:(void (^)(UIImage *, NSData *data, NSError *, BOOL))completedBlock forURL:(NSURL *)url createCallback:(void (^)())createCallback { +- (void)addProgressCallback:(SDWebImageDownloaderProgressBlock)progressBlock andCompletedBlock:(SDWebImageDownloaderCompletedBlock)completedBlock forURL:(NSURL *)url createCallback:(SDWebImageNoParamsBlock)createCallback { // The URL will be used as the key to the callbacks dictionary so it cannot be nil. If it is nil immediately call the completed block with no image or data. if (url == nil) { if (completedBlock != nil) { diff --git a/SDWebImage/SDWebImageDownloaderOperation.h b/SDWebImage/SDWebImageDownloaderOperation.h index a674e89..21a3106 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.h +++ b/SDWebImage/SDWebImageDownloaderOperation.h @@ -55,6 +55,6 @@ options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock - cancelled:(void (^)())cancelBlock; + cancelled:(SDWebImageNoParamsBlock)cancelBlock; @end diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index b9ad2fa..333e316 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -16,7 +16,7 @@ @property (copy, nonatomic) SDWebImageDownloaderProgressBlock progressBlock; @property (copy, nonatomic) SDWebImageDownloaderCompletedBlock completedBlock; -@property (copy, nonatomic) void (^cancelBlock)(); +@property (copy, nonatomic) SDWebImageNoParamsBlock cancelBlock; @property (assign, nonatomic, getter = isExecuting) BOOL executing; @property (assign, nonatomic, getter = isFinished) BOOL finished; @@ -40,7 +40,11 @@ @synthesize executing = _executing; @synthesize finished = _finished; -- (id)initWithRequest:(NSURLRequest *)request options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSInteger, NSInteger))progressBlock completed:(void (^)(UIImage *, NSData *, NSError *, BOOL))completedBlock cancelled:(void (^)())cancelBlock { +- (id)initWithRequest:(NSURLRequest *)request + options:(SDWebImageDownloaderOptions)options + progress:(SDWebImageDownloaderProgressBlock)progressBlock + completed:(SDWebImageDownloaderCompletedBlock)completedBlock + cancelled:(SDWebImageNoParamsBlock)cancelBlock { if ((self = [super init])) { _request = request; _shouldUseCredentialStorage = YES; diff --git a/SDWebImage/SDWebImageManager.h b/SDWebImage/SDWebImageManager.h index 301787c..6bb0dcf 100644 --- a/SDWebImage/SDWebImageManager.h +++ b/SDWebImage/SDWebImageManager.h @@ -81,6 +81,8 @@ typedef void(^SDWebImageCompletionBlock)(UIImage *image, NSError *error, SDImage typedef void(^SDWebImageCompletionWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL); +typedef NSString *(^SDWebImageCacheKeyFilterBlock)(NSURL *url); + @class SDWebImageManager; @@ -157,7 +159,7 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager]; * @endcode */ -@property (strong) NSString *(^cacheKeyFilter)(NSURL *url); +@property (copy) SDWebImageCacheKeyFilterBlock cacheKeyFilter; /** * Returns global SDWebImageManager instance. diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 6c75159..745d112 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -12,7 +12,7 @@ @interface SDWebImageCombinedOperation : NSObject @property (assign, nonatomic, getter = isCancelled) BOOL cancelled; -@property (copy, nonatomic) void (^cancelBlock)(); +@property (copy, nonatomic) SDWebImageNoParamsBlock cancelBlock; @property (strong, nonatomic) NSOperation *cacheOperation; @end @@ -297,7 +297,7 @@ @implementation SDWebImageCombinedOperation -- (void)setCancelBlock:(void (^)())cancelBlock { +- (void)setCancelBlock:(SDWebImageNoParamsBlock)cancelBlock { if (self.isCancelled) { if (cancelBlock) cancelBlock(); } diff --git a/SDWebImage/SDWebImagePrefetcher.h b/SDWebImage/SDWebImagePrefetcher.h index 3829033..4f14fa2 100644 --- a/SDWebImage/SDWebImagePrefetcher.h +++ b/SDWebImage/SDWebImagePrefetcher.h @@ -35,6 +35,8 @@ @end +typedef void(^SDWebImagePrefetcherProgressBlock)(NSUInteger noOfFinishedUrls, NSUInteger noOfTotalUrls); +typedef void(^SDWebImagePrefetcherCompletionBlock)(NSUInteger noOfFinishedUrls, NSUInteger noOfSkippedUrls); /** * Prefetch some URLs in the cache for future use. Images are downloaded in low priority. @@ -82,8 +84,10 @@ * first parameter is the number of completed (successful or not) requests, * second parameter is the total number of images originally requested to be prefetched * @param completionBlock block to be called when prefetching is completed + * first param is the number of completed (successful or not) requests, + * second parameter is the number of skipped requests */ -- (void)prefetchURLs:(NSArray *)urls progress:(void (^)(NSUInteger, NSUInteger))progressBlock completed:(void (^)(NSUInteger, NSUInteger))completionBlock; +- (void)prefetchURLs:(NSArray *)urls progress:(SDWebImagePrefetcherProgressBlock)progressBlock completed:(SDWebImagePrefetcherCompletionBlock)completionBlock; /** * Remove and cancel queued list diff --git a/SDWebImage/SDWebImagePrefetcher.m b/SDWebImage/SDWebImagePrefetcher.m index 8f959f6..4087e4a 100644 --- a/SDWebImage/SDWebImagePrefetcher.m +++ b/SDWebImage/SDWebImagePrefetcher.m @@ -20,8 +20,8 @@ @property (assign, nonatomic) NSUInteger skippedCount; @property (assign, nonatomic) NSUInteger finishedCount; @property (assign, nonatomic) NSTimeInterval startedTime; -@property (copy, nonatomic) void (^completionBlock)(NSUInteger, NSUInteger); -@property (copy, nonatomic) void (^progressBlock)(NSUInteger, NSUInteger); +@property (copy, nonatomic) SDWebImagePrefetcherCompletionBlock completionBlock; +@property (copy, nonatomic) SDWebImagePrefetcherProgressBlock progressBlock; @end @@ -113,7 +113,7 @@ [self prefetchURLs:urls progress:nil completed:nil]; } -- (void)prefetchURLs:(NSArray *)urls progress:(void (^)(NSUInteger, NSUInteger))progressBlock completed:(void (^)(NSUInteger, NSUInteger))completionBlock { +- (void)prefetchURLs:(NSArray *)urls progress:(SDWebImagePrefetcherProgressBlock)progressBlock completed:(SDWebImagePrefetcherCompletionBlock)completionBlock { [self cancelPrefetching]; // Prevent duplicate prefetch request self.startedTime = CFAbsoluteTimeGetCurrent(); self.prefetchURLs = urls;