Skip to content

Commit

Permalink
Add a method for setting preconfigured PINRemoteImageManager to ASPIN…
Browse files Browse the repository at this point in the history
…RemoteImageManager (TextureGroup#1124)

* add a method for setting preconfigured PINRemoteImageManager instead of using the self-created ASPINRemoteImageManager

* update preconfigured image manager where it can only be set once

* fix spacing in downloader

* Fix doc/comments on new api

* adding assertion to ensure either only configuration or preconfigure image manager can be set at a time

* adding assertion to ensure either only configuration or preconfigure image manager can be set at a time

* fix assertion condition

* Update CHANGELOG.md

* Remove unnecessary change
  • Loading branch information
ernestmama authored and mikezucc committed Oct 2, 2018
1 parent 7f698f9 commit 70aa5cc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
- Improve locking around clearContents [Michael Schneider](https://github.com/maicki)
- Unlock before cleanup and calling out to subclass hooks for animated images. [Michael Schneider](https://github.com/maicki) [#1087](https://github.com/TextureGroup/Texture/pull/1087)
- [ASDisplayNode] Fix interface state update for layer backed nodes when layer thrashes (interface coaleascing case).[Max Wang](https://github.com/wsdwsd0829). [#1111](https://github.com/TextureGroup/Texture/pull/1111)
- [ASPINRemoteImageManager] Add a new API for setting a preconfigured PINRemoteImageManager. [Ernest Ma](https://github.com/ernestmama) [#1124](https://github.com/TextureGroup/Texture/pull/1124)

## 2.7
- Fix pager node for interface coalescing. [Max Wang](https://github.com/wsdwsd0829) [#877](https://github.com/TextureGroup/Texture/pull/877)
Expand Down
10 changes: 10 additions & 0 deletions Source/Details/ASPINRemoteImageDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ NS_ASSUME_NONNULL_BEGIN
*/
+ (void)setSharedImageManagerWithConfiguration:(nullable NSURLSessionConfiguration *)configuration;

/**
* Sets a custom preconfigured PINRemoteImageManager 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. If nil is passed in as the PINRemoteImageManager, it will create
* a default image manager with a nil session configuration.
*
* @param PINRemoteImageManager The preconfigured remote image manager that will be used by `sharedDownloader`
*/
+ (void)setSharedPreconfiguredRemoteImageManager:(nullable PINRemoteImageManager *)preconfiguredPINRemoteImageManager;

/**
* The shared instance of a @c PINRemoteImageManager used by all @c ASPINRemoteImageDownloaders
*
Expand Down
57 changes: 35 additions & 22 deletions Source/Details/ASPINRemoteImageDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ @implementation ASPINRemoteImageManager
@end

static ASPINRemoteImageDownloader *sharedDownloader = nil;
static PINRemoteImageManager *sharedPINRemoteImageManager = nil;

@interface ASPINRemoteImageDownloader ()
@end
Expand All @@ -116,43 +117,55 @@ + (ASPINRemoteImageDownloader *)sharedDownloader NS_RETURNS_RETAINED
+ (void)setSharedImageManagerWithConfiguration:(nullable NSURLSessionConfiguration *)configuration
{
NSAssert(sharedDownloader == nil, @"Singleton has been created and session can no longer be configured.");
__unused PINRemoteImageManager *sharedManager = [self sharedPINRemoteImageManagerWithConfiguration:configuration];
NSAssert1(sharedPINRemoteImageManager == nil, @"An instance of %@ has been set. Either configuration or preconfigured image manager can be set at a time and only once.", [[sharedPINRemoteImageManager class] description]);
__unused PINRemoteImageManager *sharedManager = [self sharedPINRemoteImageManagerWithConfiguration:configuration preconfiguredPINRemoteImageManager:nil];
}

+ (PINRemoteImageManager *)sharedPINRemoteImageManagerWithConfiguration:(NSURLSessionConfiguration *)configuration
+ (void)setSharedPreconfiguredImageManager:(nullable PINRemoteImageManager *)preconfiguredPINRemoteImageManager
{
static ASPINRemoteImageManager *sharedPINRemoteImageManager;
NSAssert(sharedDownloader == nil, @"Singleton has been created and session can no longer be configured.");
NSAssert1(sharedPINRemoteImageManager == nil, @"An instance of %@ has been set. Either configuration or preconfigured image manager can be set at a time and only once.", [[sharedPINRemoteImageManager class] description]);
__unused PINRemoteImageManager *sharedManager = [self sharedPINRemoteImageManagerWithConfiguration:nil preconfiguredPINRemoteImageManager:preconfiguredPINRemoteImageManager];
}

+ (PINRemoteImageManager *)sharedPINRemoteImageManagerWithConfiguration:(NSURLSessionConfiguration *)configuration preconfiguredPINRemoteImageManager:(PINRemoteImageManager *)preconfiguredPINRemoteImageManager
{
NSAssert(!(configuration != nil && preconfiguredPINRemoteImageManager != nil), @"Either configuration or preconfigured image manager can be set at a time.");
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

if (preconfiguredPINRemoteImageManager) {
sharedPINRemoteImageManager = preconfiguredPINRemoteImageManager;
} else {
#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;
}
if (!(NSClassFromString(@"PINCache"))) {
NSException *e = [NSException
exceptionWithName:@"FrameworkSetupException"
reason:@"Missing the path to the PINCache framework."
userInfo:nil];
@throw e;
}
sharedPINRemoteImageManager = [[ASPINRemoteImageManager alloc] initWithSessionConfiguration:configuration
alternativeRepresentationProvider:[self sharedDownloader]];
// 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;
}
if (!(NSClassFromString(@"PINCache"))) {
NSException *e = [NSException
exceptionWithName:@"FrameworkSetupException"
reason:@"Missing the path to the PINCache framework."
userInfo:nil];
@throw e;
}
sharedPINRemoteImageManager = [[ASPINRemoteImageManager alloc] initWithSessionConfiguration:configuration
alternativeRepresentationProvider:[self sharedDownloader]];
#else
sharedPINRemoteImageManager = [[ASPINRemoteImageManager alloc] initWithSessionConfiguration:configuration];
sharedPINRemoteImageManager = [[ASPINRemoteImageManager alloc] initWithSessionConfiguration:configuration];
#endif
}
});
return sharedPINRemoteImageManager;
}

- (PINRemoteImageManager *)sharedPINRemoteImageManager
{
return [ASPINRemoteImageDownloader sharedPINRemoteImageManagerWithConfiguration:nil];
return [ASPINRemoteImageDownloader sharedPINRemoteImageManagerWithConfiguration:nil preconfiguredPINRemoteImageManager:nil];
}

- (BOOL)sharedImageManagerSupportsMemoryRemoval
Expand Down

0 comments on commit 70aa5cc

Please sign in to comment.