Skip to content

Instantly share code, notes, and snippets.

@kean
Created September 11, 2015 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kean/fe861b2b7c32f38d7539 to your computer and use it in GitHub Desktop.
Save kean/fe861b2b7c32f38d7539 to your computer and use it in GitHub Desktop.
Extend DFImageManaging API
// DFProxyImageManager.h
#import "DFImageManaging.h"
#import "DFImageRequest.h"
#import <Foundation/Foundation.h>
/*! The DFProxyRequestTransforming protocol defines an interface for transforming application-specific classes or protocols to the resources supported by the DFImageManager.
*/
@protocol DFProxyRequestTransforming <NSObject>
/*! Returns the result of transforming a given request.
@param request The copy of the original request, users can modify passed request.
*/
- (nonnull DFImageRequest *)transformedRequest:(nonnull DFImageRequest *)request;
@end
/*! The DFProxyImageManager is used to modify image requests before they are received by the actual image manager. For example, the users can use it to transform application-specific classes or protocols to the resources supported by the DFImageManager.
*/
@interface DFProxyImageManager : NSProxy <DFImageManaging>
/*! Image manager that the receiver was initialized with.
*/
@property (nonnull, nonatomic) id<DFImageManaging> imageManager;
/*! Initializes proxy with the image manager.
*/
- (nonnull instancetype)initWithImageManager:(nonnull id<DFImageManaging>)imageManager;
/*! Sets the request transformer.
*/
@property (nullable, nonatomic) id<DFProxyRequestTransforming> transformer;
/*! Sets request transformer with a given block. Overwrites transformer value.
*/
- (void)setRequestTransformerWithBlock:(DFImageRequest *__nonnull (^__nullable)(DFImageRequest *__nonnull request))block;
@end
// DFProxyImageManager.m
#import "DFProxyImageManager.h"
#import "DFImageRequest.h"
#import "DFImageTask.h"
@interface _DFProxyRequestTransformer : NSObject <DFProxyRequestTransforming>
@end
@implementation _DFProxyRequestTransformer {
DFImageRequest *(^_block)(DFImageRequest *);
}
- (instancetype)initWithBlock:(DFImageRequest * __nonnull (^ __nonnull)(DFImageRequest * __nonnull))block {
if (self = [super init]) {
_block = [block copy];
}
return self;
}
- (nonnull DFImageRequest *)transformedRequest:(nonnull DFImageRequest *)request {
return _block(request);
}
@end
#define _DF_TRANSFORMED_REQUEST(request) (_transformer ? [_transformer transformedRequest:request] : (request))
@implementation DFProxyImageManager
- (nonnull instancetype)initWithImageManager:(nonnull id<DFImageManaging>)imageManager {
_imageManager = imageManager;
return self;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
[anInvocation invokeWithTarget:_imageManager];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
return [(NSObject *)_imageManager methodSignatureForSelector:aSelector];
}
- (void)setRequestTransformerWithBlock:(DFImageRequest * __nonnull (^ __nullable)(DFImageRequest * __nonnull))block {
self.transformer = [[_DFProxyRequestTransformer alloc] initWithBlock:block];
}
#pragma mark <DFImageManaging>
- (BOOL)canHandleRequest:(nonnull DFImageRequest *)request {
return [_imageManager canHandleRequest:_DF_TRANSFORMED_REQUEST(request)];
}
- (nonnull DFImageTask *)imageTaskForResource:(nonnull id)resource completion:(nullable DFImageTaskCompletion)completion {
return [self imageTaskForRequest:[DFImageRequest requestWithResource:resource] completion:completion];
}
- (nonnull DFImageTask *)imageTaskForRequest:(nonnull DFImageRequest *)request completion:(nullable DFImageTaskCompletion)completion {
return [_imageManager imageTaskForRequest:_DF_TRANSFORMED_REQUEST(request) completion:completion];
}
- (void)startPreheatingImagesForRequests:(nonnull NSArray *)requests {
[_imageManager startPreheatingImagesForRequests:[self _transformedRequests:requests]];
}
- (void)stopPreheatingImagesForRequests:(nonnull NSArray *)requests {
[_imageManager stopPreheatingImagesForRequests:[self _transformedRequests:requests]];
}
- (nonnull NSArray *)_transformedRequests:(nonnull NSArray *)requests {
NSMutableArray *transformedRequests = [NSMutableArray new];
for (DFImageRequest *request in requests) {
[transformedRequests addObject:_DF_TRANSFORMED_REQUEST(request)];
}
return [transformedRequests copy];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment