Merge pull request #2250 from dreampiggy/refactor_api_style

Refactor api style
This commit is contained in:
DreamPiggy 2018-03-11 18:44:02 +08:00 committed by GitHub
commit 4aa48267a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 50 additions and 55 deletions

View File

@ -26,7 +26,7 @@
[super viewDidLoad]; [super viewDidLoad];
//Add GIF coder for better animated image rendering //Add GIF coder for better animated image rendering
[[SDWebImageCodersManager sharedInstance] addCoder:[SDWebImageGIFCoder sharedCoder]]; [[SDWebImageCodersManager sharedManager] addCoder:[SDWebImageGIFCoder sharedCoder]];
// NOTE: https links or authentication ones do not work (there is a crash) // NOTE: https links or authentication ones do not work (there is a crash)

View File

@ -62,10 +62,8 @@ typedef void(^SDWebImageCompletionWithPossibleErrorBlock)(NSError * _Nullable er
/** /**
* Returns global shared cache instance * Returns global shared cache instance
*
* @return SDImageCache global instance
*/ */
+ (nonnull instancetype)sharedImageCache; @property (nonatomic, class, readonly, nonnull) SDImageCache *sharedImageCache;
/** /**
* Init a new cache store with a specific namespace * Init a new cache store with a specific namespace

View File

@ -298,7 +298,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
} else { } else {
format = SDImageFormatJPEG; format = SDImageFormatJPEG;
} }
data = [[SDWebImageCodersManager sharedInstance] encodedDataWithImage:image format:format]; data = [[SDWebImageCodersManager sharedManager] encodedDataWithImage:image format:format];
} }
[self _storeImageDataToDisk:data forKey:key error:&writeError]; [self _storeImageDataToDisk:data forKey:key error:&writeError];
} }
@ -475,10 +475,10 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
- (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NSData *)data { - (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NSData *)data {
if (data) { if (data) {
UIImage *image = [[SDWebImageCodersManager sharedInstance] decodedImageWithData:data]; UIImage *image = [[SDWebImageCodersManager sharedManager] decodedImageWithData:data];
image = [self scaledImageForKey:key image:image]; image = [self scaledImageForKey:key image:image];
if (self.config.shouldDecompressImages) { if (self.config.shouldDecompressImages) {
image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&data options:@{SDWebImageCoderScaleDownLargeImagesKey: @(NO)}]; image = [[SDWebImageCodersManager sharedManager] decompressedImageWithImage:image data:&data options:@{SDWebImageCoderScaleDownLargeImagesKey: @(NO)}];
} }
return image; return image;
} else { } else {

View File

@ -32,9 +32,9 @@
@interface SDWebImageCodersManager : NSObject<SDWebImageCoder> @interface SDWebImageCodersManager : NSObject<SDWebImageCoder>
/** /**
Shared reusable instance Returns the global shared coders manager instance.
*/ */
+ (nonnull instancetype)sharedInstance; @property (nonatomic, class, readonly, nonnull) SDWebImageCodersManager *sharedManager;
/** /**
All coders in coders manager. The coders array is a priority queue, which means the later added coder will have the highest priority All coders in coders manager. The coders array is a priority queue, which means the later added coder will have the highest priority

View File

@ -22,7 +22,7 @@
@implementation SDWebImageCodersManager @implementation SDWebImageCodersManager
+ (nonnull instancetype)sharedInstance { + (nonnull instancetype)sharedManager {
static dispatch_once_t once; static dispatch_once_t once;
static id instance; static id instance;
dispatch_once(&once, ^{ dispatch_once(&once, ^{

View File

@ -139,19 +139,26 @@ typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterB
*/ */
@property (readonly, nonatomic, nonnull) NSURLSessionConfiguration *sessionConfiguration; @property (readonly, nonatomic, nonnull) NSURLSessionConfiguration *sessionConfiguration;
/**
* Gets/Sets a subclass of `SDWebImageDownloaderOperation` as the default
* `NSOperation` to be used each time SDWebImage constructs a request
* operation to download an image.
*
* @param operationClass The subclass of `SDWebImageDownloaderOperation` to set
* as default. Passing `nil` will revert to `SDWebImageDownloaderOperation`.
*/
@property (assign, nonatomic, nullable) Class operationClass;
/**
* Gets/Sets the download queue suspension state.
*/
@property (assign, nonatomic, getter=isSuspended) BOOL suspended;
/** /**
* Changes download operations execution order. Default value is `SDWebImageDownloaderFIFOExecutionOrder`. * Changes download operations execution order. Default value is `SDWebImageDownloaderFIFOExecutionOrder`.
*/ */
@property (assign, nonatomic) SDWebImageDownloaderExecutionOrder executionOrder; @property (assign, nonatomic) SDWebImageDownloaderExecutionOrder executionOrder;
/**
* Singleton method, returns the shared instance
*
* @return global shared instance of downloader class
*/
+ (nonnull instancetype)sharedDownloader;
/** /**
* Set the default URL credential to be set for request operations. * Set the default URL credential to be set for request operations.
*/ */
@ -175,6 +182,11 @@ typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterB
*/ */
@property (nonatomic, copy, nullable) SDWebImageDownloaderHeadersFilterBlock headersFilter; @property (nonatomic, copy, nullable) SDWebImageDownloaderHeadersFilterBlock headersFilter;
/**
* Returns the global shared downloader instance
*/
@property (nonatomic, class, readonly, nonnull) SDWebImageDownloader *sharedDownloader;
/** /**
* Creates an instance of a downloader with specified session configuration. * Creates an instance of a downloader with specified session configuration.
* @note `timeoutIntervalForRequest` is going to be overwritten. * @note `timeoutIntervalForRequest` is going to be overwritten.
@ -197,16 +209,6 @@ typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterB
*/ */
- (nullable NSString *)valueForHTTPHeaderField:(nullable NSString *)field; - (nullable NSString *)valueForHTTPHeaderField:(nullable NSString *)field;
/**
* Sets a subclass of `SDWebImageDownloaderOperation` as the default
* `NSOperation` to be used each time SDWebImage constructs a request
* operation to download an image.
*
* @param operationClass The subclass of `SDWebImageDownloaderOperation` to set
* as default. Passing `nil` will revert to `SDWebImageDownloaderOperation`.
*/
- (void)setOperationClass:(nullable Class)operationClass;
/** /**
* Creates a SDWebImageDownloader async downloader instance with a given URL * Creates a SDWebImageDownloader async downloader instance with a given URL
* *
@ -263,11 +265,6 @@ typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterB
*/ */
- (void)cancel:(nullable SDWebImageDownloadToken *)token; - (void)cancel:(nullable SDWebImageDownloadToken *)token;
/**
* Sets the download queue suspension state
*/
- (void)setSuspended:(BOOL)suspended;
/** /**
* Cancels all download operations in the queue * Cancels all download operations in the queue
*/ */

View File

@ -36,7 +36,6 @@
@property (strong, nonatomic, nonnull) NSOperationQueue *downloadQueue; @property (strong, nonatomic, nonnull) NSOperationQueue *downloadQueue;
@property (weak, nonatomic, nullable) NSOperation *lastAddedOperation; @property (weak, nonatomic, nullable) NSOperation *lastAddedOperation;
@property (assign, nonatomic, nullable) Class operationClass;
@property (strong, nonatomic, nonnull) NSMutableDictionary<NSURL *, SDWebImageDownloaderOperation *> *URLOperations; @property (strong, nonatomic, nonnull) NSMutableDictionary<NSURL *, SDWebImageDownloaderOperation *> *URLOperations;
@property (strong, nonatomic, nullable) SDHTTPHeadersMutableDictionary *HTTPHeaders; @property (strong, nonatomic, nullable) SDHTTPHeadersMutableDictionary *HTTPHeaders;
@property (strong, nonatomic, nonnull) dispatch_semaphore_t operationsLock; // a lock to keep the access to `URLOperations` thread-safe @property (strong, nonatomic, nonnull) dispatch_semaphore_t operationsLock; // a lock to keep the access to `URLOperations` thread-safe
@ -309,6 +308,10 @@
return token; return token;
} }
- (BOOL)isSuspended {
return self.downloadQueue.isSuspended;
}
- (void)setSuspended:(BOOL)suspended { - (void)setSuspended:(BOOL)suspended {
self.downloadQueue.suspended = suspended; self.downloadQueue.suspended = suspended;
} }

View File

@ -345,7 +345,7 @@ didReceiveResponse:(NSURLResponse *)response
if (!self.progressiveCoder) { if (!self.progressiveCoder) {
// We need to create a new instance for progressive decoding to avoid conflicts // We need to create a new instance for progressive decoding to avoid conflicts
for (id<SDWebImageCoder>coder in [SDWebImageCodersManager sharedInstance].coders) { for (id<SDWebImageCoder>coder in [SDWebImageCodersManager sharedManager].coders) {
if ([coder conformsToProtocol:@protocol(SDWebImageProgressiveCoder)] && if ([coder conformsToProtocol:@protocol(SDWebImageProgressiveCoder)] &&
[((id<SDWebImageProgressiveCoder>)coder) canIncrementallyDecodeFromData:imageData]) { [((id<SDWebImageProgressiveCoder>)coder) canIncrementallyDecodeFromData:imageData]) {
self.progressiveCoder = [[[coder class] alloc] init]; self.progressiveCoder = [[[coder class] alloc] init];
@ -361,7 +361,7 @@ didReceiveResponse:(NSURLResponse *)response
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL]; NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
image = [self scaledImageForKey:key image:image]; image = [self scaledImageForKey:key image:image];
if (self.shouldDecompressImages) { if (self.shouldDecompressImages) {
image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&imageData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(NO)}]; image = [[SDWebImageCodersManager sharedManager] decompressedImageWithImage:image data:&imageData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(NO)}];
} }
// We do not keep the progressive decoding image even when `finished`=YES. Because they are for view rendering but not take full function from downloader options. And some coders implementation may not keep consistent between progressive decoding and normal decoding. // We do not keep the progressive decoding image even when `finished`=YES. Because they are for view rendering but not take full function from downloader options. And some coders implementation may not keep consistent between progressive decoding and normal decoding.
@ -427,7 +427,7 @@ didReceiveResponse:(NSURLResponse *)response
} else { } else {
// decode the image in coder queue // decode the image in coder queue
dispatch_async(self.coderQueue, ^{ dispatch_async(self.coderQueue, ^{
UIImage *image = [[SDWebImageCodersManager sharedInstance] decodedImageWithData:imageData]; UIImage *image = [[SDWebImageCodersManager sharedManager] decodedImageWithData:imageData];
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL]; NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
image = [self scaledImageForKey:key image:image]; image = [self scaledImageForKey:key image:image];
@ -447,7 +447,7 @@ didReceiveResponse:(NSURLResponse *)response
if (shouldDecode) { if (shouldDecode) {
if (self.shouldDecompressImages) { if (self.shouldDecompressImages) {
BOOL shouldScaleDown = self.options & SDWebImageDownloaderScaleDownLargeImages; BOOL shouldScaleDown = self.options & SDWebImageDownloaderScaleDownLargeImages;
image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&imageData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(shouldScaleDown)}]; image = [[SDWebImageCodersManager sharedManager] decompressedImageWithImage:image data:&imageData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(shouldScaleDown)}];
} }
} }
CGSize imageSize = image.size; CGSize imageSize = image.size;

View File

@ -18,6 +18,6 @@
*/ */
@interface SDWebImageGIFCoder : NSObject <SDWebImageCoder> @interface SDWebImageGIFCoder : NSObject <SDWebImageCoder>
+ (nonnull instancetype)sharedCoder; @property (nonatomic, class, readonly, nonnull) SDWebImageGIFCoder *sharedCoder;
@end @end

View File

@ -25,6 +25,6 @@
*/ */
@interface SDWebImageImageIOCoder : NSObject <SDWebImageProgressiveCoder> @interface SDWebImageImageIOCoder : NSObject <SDWebImageProgressiveCoder>
+ (nonnull instancetype)sharedCoder; @property (nonatomic, class, readonly, nonnull) SDWebImageImageIOCoder *sharedCoder;
@end @end

View File

@ -213,11 +213,9 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager];
@property (nonatomic, copy, nullable) SDWebImageCacheKeyFilterBlock cacheKeyFilter; @property (nonatomic, copy, nullable) SDWebImageCacheKeyFilterBlock cacheKeyFilter;
/** /**
* Returns global SDWebImageManager instance. * Returns global shared manager instance.
*
* @return SDWebImageManager shared instance
*/ */
+ (nonnull instancetype)sharedManager; @property (nonatomic, class, readonly, nonnull) SDWebImageManager *sharedManager;
/** /**
* Allows to specify instance of cache and image downloader used with image manager. * Allows to specify instance of cache and image downloader used with image manager.

View File

@ -80,9 +80,9 @@ typedef void(^SDWebImagePrefetcherCompletionBlock)(NSUInteger noOfFinishedUrls,
@property (weak, nonatomic, nullable) id <SDWebImagePrefetcherDelegate> delegate; @property (weak, nonatomic, nullable) id <SDWebImagePrefetcherDelegate> delegate;
/** /**
* Return the global image prefetcher instance. * Returns the global shared image prefetcher instance.
*/ */
+ (nonnull instancetype)sharedImagePrefetcher; @property (nonatomic, class, readonly, nonnull) SDWebImagePrefetcher *sharedImagePrefetcher;
/** /**
* Allows you to instantiate a prefetcher with any arbitrary image manager. * Allows you to instantiate a prefetcher with any arbitrary image manager.

View File

@ -16,7 +16,7 @@
*/ */
@interface SDWebImageWebPCoder : NSObject <SDWebImageProgressiveCoder> @interface SDWebImageWebPCoder : NSObject <SDWebImageProgressiveCoder>
+ (nonnull instancetype)sharedCoder; @property (nonatomic, class, readonly, nonnull) SDWebImageWebPCoder *sharedCoder;
@end @end

View File

@ -16,7 +16,7 @@
return nil; return nil;
} }
NSData *tempData; NSData *tempData;
return [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&tempData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(NO)}]; return [[SDWebImageCodersManager sharedManager] decompressedImageWithImage:image data:&tempData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(NO)}];
} }
+ (UIImage *)sd_decodedAndScaledDownImageWithImage:(UIImage *)image { + (UIImage *)sd_decodedAndScaledDownImageWithImage:(UIImage *)image {
@ -24,7 +24,7 @@
return nil; return nil;
} }
NSData *tempData; NSData *tempData;
return [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&tempData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(YES)}]; return [[SDWebImageCodersManager sharedManager] decompressedImageWithImage:image data:&tempData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(YES)}];
} }
@end @end

View File

@ -12,7 +12,7 @@
@implementation UIImage (MultiFormat) @implementation UIImage (MultiFormat)
+ (nullable UIImage *)sd_imageWithData:(nullable NSData *)data { + (nullable UIImage *)sd_imageWithData:(nullable NSData *)data {
return [[SDWebImageCodersManager sharedInstance] decodedImageWithData:data]; return [[SDWebImageCodersManager sharedManager] decodedImageWithData:data];
} }
- (nullable NSData *)sd_imageData { - (nullable NSData *)sd_imageData {
@ -22,7 +22,7 @@
- (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat { - (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat {
NSData *imageData = nil; NSData *imageData = nil;
if (self) { if (self) {
imageData = [[SDWebImageCodersManager sharedInstance] encodedDataWithImage:self format:imageFormat]; imageData = [[SDWebImageCodersManager sharedManager] encodedDataWithImage:self format:imageFormat];
} }
return imageData; return imageData;
} }

View File

@ -264,7 +264,7 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
XCTestExpectation *expectation = [self expectationWithDescription:@"Custom decoder for SDImageCache not works"]; XCTestExpectation *expectation = [self expectationWithDescription:@"Custom decoder for SDImageCache not works"];
SDImageCache *cache = [[SDImageCache alloc] initWithNamespace:@"TestDecode"]; SDImageCache *cache = [[SDImageCache alloc] initWithNamespace:@"TestDecode"];
SDWebImageTestDecoder *testDecoder = [[SDWebImageTestDecoder alloc] init]; SDWebImageTestDecoder *testDecoder = [[SDWebImageTestDecoder alloc] init];
[[SDWebImageCodersManager sharedInstance] addCoder:testDecoder]; [[SDWebImageCodersManager sharedManager] addCoder:testDecoder];
NSString * testImagePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestImage" ofType:@"png"]; NSString * testImagePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestImage" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:testImagePath]; UIImage *image = [[UIImage alloc] initWithContentsOfFile:testImagePath];
NSString *key = @"TestPNGImageEncodedToDataAndRetrieveToJPEG"; NSString *key = @"TestPNGImageEncodedToDataAndRetrieveToJPEG";
@ -298,7 +298,7 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
XCTFail(@"Custom decoder not work for SDImageCache, check -[SDWebImageTestDecoder decodedImageWithData:]"); XCTFail(@"Custom decoder not work for SDImageCache, check -[SDWebImageTestDecoder decodedImageWithData:]");
} }
[[SDWebImageCodersManager sharedInstance] removeCoder:testDecoder]; [[SDWebImageCodersManager sharedManager] removeCoder:testDecoder];
[[SDImageCache sharedImageCache] removeImageForKey:key withCompletion:^{ [[SDImageCache sharedImageCache] removeImageForKey:key withCompletion:^{
[expectation fulfill]; [expectation fulfill];

View File

@ -17,7 +17,6 @@
* Category for SDWebImageDownloader so we can access the operationClass * Category for SDWebImageDownloader so we can access the operationClass
*/ */
@interface SDWebImageDownloader () @interface SDWebImageDownloader ()
@property (assign, nonatomic, nullable) Class operationClass;
@property (strong, nonatomic, nonnull) NSOperationQueue *downloadQueue; @property (strong, nonatomic, nonnull) NSOperationQueue *downloadQueue;
- (nullable SDWebImageDownloadToken *)addProgressCallback:(SDWebImageDownloaderProgressBlock)progressBlock - (nullable SDWebImageDownloadToken *)addProgressCallback:(SDWebImageDownloaderProgressBlock)progressBlock
@ -367,7 +366,7 @@
XCTestExpectation *expectation = [self expectationWithDescription:@"Custom decoder for SDWebImageDownloader not works"]; XCTestExpectation *expectation = [self expectationWithDescription:@"Custom decoder for SDWebImageDownloader not works"];
SDWebImageDownloader *downloader = [[SDWebImageDownloader alloc] init]; SDWebImageDownloader *downloader = [[SDWebImageDownloader alloc] init];
SDWebImageTestDecoder *testDecoder = [[SDWebImageTestDecoder alloc] init]; SDWebImageTestDecoder *testDecoder = [[SDWebImageTestDecoder alloc] init];
[[SDWebImageCodersManager sharedInstance] addCoder:testDecoder]; [[SDWebImageCodersManager sharedManager] addCoder:testDecoder];
NSURL * testImageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImage" withExtension:@"png"]; NSURL * testImageURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TestImage" withExtension:@"png"];
// Decoded result is JPEG // Decoded result is JPEG
@ -385,7 +384,7 @@
if (![str1 isEqualToString:str2]) { if (![str1 isEqualToString:str2]) {
XCTFail(@"The image data is not modified by the custom decoder, check -[SDWebImageTestDecoder decompressedImageWithImage:data:options:]"); XCTFail(@"The image data is not modified by the custom decoder, check -[SDWebImageTestDecoder decompressedImageWithImage:data:options:]");
} }
[[SDWebImageCodersManager sharedInstance] removeCoder:testDecoder]; [[SDWebImageCodersManager sharedManager] removeCoder:testDecoder];
[expectation fulfill]; [expectation fulfill];
}]; }];