From cf9a595ee929c1397063b97f44441f0556317f36 Mon Sep 17 00:00:00 2001 From: Dylan Vann Date: Wed, 13 Jun 2018 23:47:10 -0400 Subject: [PATCH] Add cache control for iOS. --- ios/FastImage/FFFastImageSource.h | 11 ++++++++++- ios/FastImage/FFFastImageSource.m | 2 ++ ios/FastImage/FFFastImageView.m | 11 +++++++++++ ios/FastImage/RCTConvert+FFFastImage.m | 25 ++++++++++++++++--------- src/index.js | 18 +++++++++++++++++- 5 files changed, 56 insertions(+), 11 deletions(-) diff --git a/ios/FastImage/FFFastImageSource.h b/ios/FastImage/FFFastImageSource.h index 0a4dc21..7f97869 100644 --- a/ios/FastImage/FFFastImageSource.h +++ b/ios/FastImage/FFFastImageSource.h @@ -7,6 +7,12 @@ typedef NS_ENUM(NSInteger, FFFPriority) { FFFPriorityHigh }; +typedef NS_ENUM(NSInteger, FFFCacheControl) { + FFFCacheControlImmutable, + FFFCacheControlWeb, + FFFCacheControlCacheOnly +}; + // Object containing an image uri and metadata. @interface FFFastImageSource : NSObject @@ -16,9 +22,12 @@ typedef NS_ENUM(NSInteger, FFFPriority) { @property (nonatomic) FFFPriority priority; // headers for the image request @property (nonatomic) NSDictionary *headers; +// cache control mode +@property (nonatomic) FFFCacheControl cacheControl; - (instancetype)initWithURL:(NSURL *)url priority:(FFFPriority)priority - headers:(NSDictionary *)headers; + headers:(NSDictionary *)headers + cacheControl:(FFFCacheControl)cacheControl; @end diff --git a/ios/FastImage/FFFastImageSource.m b/ios/FastImage/FFFastImageSource.m index 0888d89..fc47524 100644 --- a/ios/FastImage/FFFastImageSource.m +++ b/ios/FastImage/FFFastImageSource.m @@ -5,12 +5,14 @@ - (instancetype)initWithURL:(NSURL *)url priority:(FFFPriority)priority headers:(NSDictionary *)headers + cacheControl:(FFFCacheControl)cacheControl { self = [super init]; if (self) { _url = url; _priority = priority; _headers = headers; + _cacheControl = cacheControl; } return self; } diff --git a/ios/FastImage/FFFastImageView.m b/ios/FastImage/FFFastImageView.m index 472e23f..5fa8d91 100644 --- a/ios/FastImage/FFFastImageView.m +++ b/ios/FastImage/FFFastImageView.m @@ -114,6 +114,17 @@ break; } + switch (_source.cacheControl) { + case FFFCacheControlWeb: + options |= SDWebImageRefreshCached; + break; + case FFFCacheControlCacheOnly: + options |= SDWebImageCacheMemoryOnly; + break; + case FFFCacheControlImmutable: + break; + } + if (_onFastImageLoadStart) { _onFastImageLoadStart(@{}); hasSentOnLoadStart = YES; diff --git a/ios/FastImage/RCTConvert+FFFastImage.m b/ios/FastImage/RCTConvert+FFFastImage.m index ab2ab88..43f8922 100644 --- a/ios/FastImage/RCTConvert+FFFastImage.m +++ b/ios/FastImage/RCTConvert+FFFastImage.m @@ -4,10 +4,16 @@ @implementation RCTConvert (FFFastImage) RCT_ENUM_CONVERTER(FFFPriority, (@{ - @"low": @(FFFPriorityLow), - @"normal": @(FFFPriorityNormal), - @"high": @(FFFPriorityHigh), -}), FFFPriorityNormal, integerValue); + @"low": @(FFFPriorityLow), + @"normal": @(FFFPriorityNormal), + @"high": @(FFFPriorityHigh), + }), FFFPriorityNormal, integerValue); + +RCT_ENUM_CONVERTER(FFFCacheControl, (@{ + @"immutable": @(FFFCacheControlImmutable), + @"web": @(FFFCacheControlWeb), + @"cacheOnly": @(FFFCacheControlCacheOnly), + }), FFFCacheControlImmutable, integerValue); + (FFFastImageSource *)FFFastImageSource:(id)json { if (!json) { @@ -18,14 +24,15 @@ RCT_ENUM_CONVERTER(FFFPriority, (@{ NSURL *uri = [self NSURL:uriString]; FFFPriority priority = [self FFFPriority:json[@"priority"]]; - + FFFCacheControl cacheControl = [self FFFCacheControl:json[@"cache"]]; + NSDictionary *headers = [self NSDictionary:json[@"headers"]]; if (headers) { __block BOOL allHeadersAreStrings = YES; [headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id header, BOOL *stop) { if (![header isKindOfClass:[NSString class]]) { RCTLogError(@"Values of HTTP headers passed must be of type string. " - "Value of header '%@' is not a string.", key); + "Value of header '%@' is not a string.", key); allHeadersAreStrings = NO; *stop = YES; } @@ -35,9 +42,9 @@ RCT_ENUM_CONVERTER(FFFPriority, (@{ headers = nil; } } - - FFFastImageSource *imageSource = [[FFFastImageSource alloc] initWithURL:uri priority:priority headers:headers]; - + + FFFastImageSource *imageSource = [[FFFastImageSource alloc] initWithURL:uri priority:priority headers:headers cacheControl:cacheControl]; + return imageSource; } diff --git a/src/index.js b/src/index.js index 50d2336..35ed2f0 100644 --- a/src/index.js +++ b/src/index.js @@ -39,7 +39,10 @@ class FastImage extends Component { if (fallback) { return ( - + { FastImageViewNativeModule.preload(sources) } @@ -104,6 +119,7 @@ const FastImageSourcePropType = PropTypes.shape({ uri: PropTypes.string, headers: PropTypes.objectOf(PropTypes.string), priority: PropTypes.oneOf(Object.keys(FastImage.priority)), + cache: PropTypes.oneOf(Object.keys(FastImage.cacheControl)), }) FastImage.propTypes = {