Skip to content

Commit

Permalink
Add method to specify the default NSURLSessionConfiguration for (face…
Browse files Browse the repository at this point in the history
…bookarchive#2609)

ASPINRemoteImageDownloader. This will allow callers to supply their own custom
logic whether that is background downloads, custom http headers, cookie
storage, etc.
  • Loading branch information
appleguy authored Nov 15, 2016
2 parents a080773 + 7769883 commit 4d16d58
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
11 changes: 11 additions & 0 deletions AsyncDisplayKit/Details/ASPINRemoteImageDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ NS_ASSUME_NONNULL_BEGIN
*/
+ (ASPINRemoteImageDownloader *)sharedDownloader;


/**
* Sets the default NSURLSessionConfiguration that will be used by @c ASNetworkImageNodes and @c ASMultiplexImageNodes
* while loading images off the network. This must be specified early in the application lifecycle before
* `sharedDownloader` is accessed.
*
* @param configuration The session configuration that will be used by `sharedDownloader`
*
*/
+ (void)setSharedImageManagerWithConfiguration:(nullable NSURLSessionConfiguration *)configuration;

/**
* The shared instance of a @c PINRemoteImageManager used by all @c ASPINRemoteImageDownloaders
*
Expand Down
69 changes: 43 additions & 26 deletions AsyncDisplayKit/Details/ASPINRemoteImageDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,48 +77,65 @@ @implementation ASPINRemoteImageManager
//Share image cache with sharedImageManager image cache.
- (id <PINRemoteImageCaching>)defaultImageCache
{
return [[PINRemoteImageManager sharedImageManager] cache];
return [[PINRemoteImageManager sharedImageManager] cache];
}

@end


static ASPINRemoteImageDownloader *sharedDownloader = nil;

@interface ASPINRemoteImageDownloader ()
@end

@implementation ASPINRemoteImageDownloader

+ (instancetype)sharedDownloader
{
static ASPINRemoteImageDownloader *sharedDownloader = nil;

static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
sharedDownloader = [[ASPINRemoteImageDownloader alloc] init];
});
return sharedDownloader;
}

+ (void)setSharedImageManagerWithConfiguration:(nullable NSURLSessionConfiguration *)configuration
{
NSAssert(sharedDownloader == nil, @"Singleton has been created and session can no longer be configured.");
__unused PINRemoteImageManager *sharedManager = [[self class] sharedPINRemoteImageManagerWithConfiguration:configuration];
}

- (PINRemoteImageManager *)sharedPINRemoteImageManager
{
static ASPINRemoteImageManager *sharedPINRemoteImageManager = nil;
return [self sharedPINRemoteImageManagerWithConfiguration:nil];
}

- (PINRemoteImageManager *)sharedPINRemoteImageManagerWithConfiguration:(NSURLSessionConfiguration *)configuration
{
static ASPINRemoteImageManager *sharedPINRemoteImageManager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

#if PIN_ANIMATED_AVAILABLE
// Check that Carthage users have linked both PINRemoteImage & PINCache by testing for one file each
if (!(NSClassFromString(@"PINRemoteImageManager"))) {
NSException *e = [NSException
exceptionWithName:@"FrameworkSetupException"
reason:@"Missing the path to the PINRemoteImage framework."
userInfo:nil];
@throw e;
NSException *e = [NSException
exceptionWithName:@"FrameworkSetupException"
reason:@"Missing the path to the PINRemoteImage framework."
userInfo:nil];
@throw e;
}
if (!(NSClassFromString(@"PINCache"))) {
NSException *e = [NSException
exceptionWithName:@"FrameworkSetupException"
reason:@"Missing the path to the PINCache framework."
userInfo:nil];
@throw e;
NSException *e = [NSException
exceptionWithName:@"FrameworkSetupException"
reason:@"Missing the path to the PINCache framework."
userInfo:nil];
@throw e;
}
sharedPINRemoteImageManager = [[ASPINRemoteImageManager alloc] initWithSessionConfiguration:nil alternativeRepresentationProvider:self];
sharedPINRemoteImageManager = [[ASPINRemoteImageManager alloc] initWithSessionConfiguration:configuration alternativeRepresentationProvider:self];
#else
sharedPINRemoteImageManager = [[ASPINRemoteImageManager alloc] initWithSessionConfiguration:nil];
sharedPINRemoteImageManager = [[ASPINRemoteImageManager alloc] initWithSessionConfiguration:configuration];
#endif
});
return sharedPINRemoteImageManager;
Expand Down Expand Up @@ -187,7 +204,7 @@ - (nullable id)downloadImageWithURL:(NSURL *)URL
{
return [[self sharedPINRemoteImageManager] downloadImageWithURL:URL options:PINRemoteImageManagerDownloadOptionsSkipDecode progressDownload:^(int64_t completedBytes, int64_t totalBytes) {
if (downloadProgress == nil) { return; }

/// If we're targeting the main queue and we're on the main thread, call immediately.
if (ASDisplayNodeThreadIsMain() && callbackQueue == dispatch_get_main_queue()) {
downloadProgress(completedBytes / (CGFloat)totalBytes);
Expand Down Expand Up @@ -229,15 +246,15 @@ - (void)cancelImageDownloadForIdentifier:(id)downloadIdentifier
if (!downloadIdentifier) {
return;
}

ASDisplayNodeAssert([downloadIdentifier isKindOfClass:[NSUUID class]], @"downloadIdentifier must be NSUUID");
[[self sharedPINRemoteImageManager] cancelTaskWithUUID:downloadIdentifier];
}

- (void)setProgressImageBlock:(ASImageDownloaderProgressImage)progressBlock callbackQueue:(dispatch_queue_t)callbackQueue withDownloadIdentifier:(id)downloadIdentifier
{
ASDisplayNodeAssert([downloadIdentifier isKindOfClass:[NSUUID class]], @"downloadIdentifier must be NSUUID");

if (progressBlock) {
[[self sharedPINRemoteImageManager] setProgressImageCallback:^(PINRemoteImageManagerResult * _Nonnull result) {
dispatch_async(callbackQueue, ^{
Expand All @@ -252,17 +269,17 @@ - (void)setProgressImageBlock:(ASImageDownloaderProgressImage)progressBlock call
- (void)setPriority:(ASImageDownloaderPriority)priority withDownloadIdentifier:(id)downloadIdentifier
{
ASDisplayNodeAssert([downloadIdentifier isKindOfClass:[NSUUID class]], @"downloadIdentifier must be NSUUID");

PINRemoteImageManagerPriority pi_priority = PINRemoteImageManagerPriorityMedium;
switch (priority) {
case ASImageDownloaderPriorityPreload:
pi_priority = PINRemoteImageManagerPriorityMedium;
break;

case ASImageDownloaderPriorityImminent:
pi_priority = PINRemoteImageManagerPriorityHigh;
break;

case ASImageDownloaderPriorityVisible:
pi_priority = PINRemoteImageManagerPriorityVeryHigh;
break;
Expand All @@ -275,11 +292,11 @@ - (void)setPriority:(ASImageDownloaderPriority)priority withDownloadIdentifier:(
- (id)alternateRepresentationWithData:(NSData *)data options:(PINRemoteImageManagerDownloadOptions)options
{
#if PIN_ANIMATED_AVAILABLE
if ([data pin_isGIF]) {
return data;
}
if ([data pin_isGIF]) {
return data;
}
#endif
return nil;
return nil;
}

@end
Expand Down

0 comments on commit 4d16d58

Please sign in to comment.