Add cache control for iOS.

This commit is contained in:
Dylan Vann 2018-06-13 23:47:10 -04:00
parent 30a2ff7909
commit cf9a595ee9
5 changed files with 56 additions and 11 deletions

View File

@ -7,6 +7,12 @@ typedef NS_ENUM(NSInteger, FFFPriority) {
FFFPriorityHigh FFFPriorityHigh
}; };
typedef NS_ENUM(NSInteger, FFFCacheControl) {
FFFCacheControlImmutable,
FFFCacheControlWeb,
FFFCacheControlCacheOnly
};
// Object containing an image uri and metadata. // Object containing an image uri and metadata.
@interface FFFastImageSource : NSObject @interface FFFastImageSource : NSObject
@ -16,9 +22,12 @@ typedef NS_ENUM(NSInteger, FFFPriority) {
@property (nonatomic) FFFPriority priority; @property (nonatomic) FFFPriority priority;
// headers for the image request // headers for the image request
@property (nonatomic) NSDictionary *headers; @property (nonatomic) NSDictionary *headers;
// cache control mode
@property (nonatomic) FFFCacheControl cacheControl;
- (instancetype)initWithURL:(NSURL *)url - (instancetype)initWithURL:(NSURL *)url
priority:(FFFPriority)priority priority:(FFFPriority)priority
headers:(NSDictionary *)headers; headers:(NSDictionary *)headers
cacheControl:(FFFCacheControl)cacheControl;
@end @end

View File

@ -5,12 +5,14 @@
- (instancetype)initWithURL:(NSURL *)url - (instancetype)initWithURL:(NSURL *)url
priority:(FFFPriority)priority priority:(FFFPriority)priority
headers:(NSDictionary *)headers headers:(NSDictionary *)headers
cacheControl:(FFFCacheControl)cacheControl
{ {
self = [super init]; self = [super init];
if (self) { if (self) {
_url = url; _url = url;
_priority = priority; _priority = priority;
_headers = headers; _headers = headers;
_cacheControl = cacheControl;
} }
return self; return self;
} }

View File

@ -114,6 +114,17 @@
break; break;
} }
switch (_source.cacheControl) {
case FFFCacheControlWeb:
options |= SDWebImageRefreshCached;
break;
case FFFCacheControlCacheOnly:
options |= SDWebImageCacheMemoryOnly;
break;
case FFFCacheControlImmutable:
break;
}
if (_onFastImageLoadStart) { if (_onFastImageLoadStart) {
_onFastImageLoadStart(@{}); _onFastImageLoadStart(@{});
hasSentOnLoadStart = YES; hasSentOnLoadStart = YES;

View File

@ -4,10 +4,16 @@
@implementation RCTConvert (FFFastImage) @implementation RCTConvert (FFFastImage)
RCT_ENUM_CONVERTER(FFFPriority, (@{ RCT_ENUM_CONVERTER(FFFPriority, (@{
@"low": @(FFFPriorityLow), @"low": @(FFFPriorityLow),
@"normal": @(FFFPriorityNormal), @"normal": @(FFFPriorityNormal),
@"high": @(FFFPriorityHigh), @"high": @(FFFPriorityHigh),
}), FFFPriorityNormal, integerValue); }), FFFPriorityNormal, integerValue);
RCT_ENUM_CONVERTER(FFFCacheControl, (@{
@"immutable": @(FFFCacheControlImmutable),
@"web": @(FFFCacheControlWeb),
@"cacheOnly": @(FFFCacheControlCacheOnly),
}), FFFCacheControlImmutable, integerValue);
+ (FFFastImageSource *)FFFastImageSource:(id)json { + (FFFastImageSource *)FFFastImageSource:(id)json {
if (!json) { if (!json) {
@ -18,14 +24,15 @@ RCT_ENUM_CONVERTER(FFFPriority, (@{
NSURL *uri = [self NSURL:uriString]; NSURL *uri = [self NSURL:uriString];
FFFPriority priority = [self FFFPriority:json[@"priority"]]; FFFPriority priority = [self FFFPriority:json[@"priority"]];
FFFCacheControl cacheControl = [self FFFCacheControl:json[@"cache"]];
NSDictionary *headers = [self NSDictionary:json[@"headers"]]; NSDictionary *headers = [self NSDictionary:json[@"headers"]];
if (headers) { if (headers) {
__block BOOL allHeadersAreStrings = YES; __block BOOL allHeadersAreStrings = YES;
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id header, BOOL *stop) { [headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id header, BOOL *stop) {
if (![header isKindOfClass:[NSString class]]) { if (![header isKindOfClass:[NSString class]]) {
RCTLogError(@"Values of HTTP headers passed must be of type string. " 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; allHeadersAreStrings = NO;
*stop = YES; *stop = YES;
} }
@ -35,9 +42,9 @@ RCT_ENUM_CONVERTER(FFFPriority, (@{
headers = nil; 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; return imageSource;
} }

View File

@ -39,7 +39,10 @@ class FastImage extends Component {
if (fallback) { if (fallback) {
return ( return (
<View style={[style, styles.imageContainer]} ref={this.captureRef}> <View
style={[style, styles.imageContainer]}
ref={this.captureRef}
>
<FastImageView <FastImageView
{...props} {...props}
style={StyleSheet.absoluteFill} style={StyleSheet.absoluteFill}
@ -87,11 +90,23 @@ FastImage.resizeMode = {
} }
FastImage.priority = { FastImage.priority = {
// lower than usual.
low: 'low', low: 'low',
// normal, the default.
normal: 'normal', normal: 'normal',
// higher than usual.
high: 'high', high: 'high',
} }
FastImage.cacheControl = {
// Ignore headers, use uri as cache key, fetch only if not in cache.
immutable: 'immutable',
// Respect http headers, no aggressive caching.
web: 'web',
// Only load from cache.
cacheOnly: 'cacheOnly',
}
FastImage.preload = sources => { FastImage.preload = sources => {
FastImageViewNativeModule.preload(sources) FastImageViewNativeModule.preload(sources)
} }
@ -104,6 +119,7 @@ const FastImageSourcePropType = PropTypes.shape({
uri: PropTypes.string, uri: PropTypes.string,
headers: PropTypes.objectOf(PropTypes.string), headers: PropTypes.objectOf(PropTypes.string),
priority: PropTypes.oneOf(Object.keys(FastImage.priority)), priority: PropTypes.oneOf(Object.keys(FastImage.priority)),
cache: PropTypes.oneOf(Object.keys(FastImage.cacheControl)),
}) })
FastImage.propTypes = { FastImage.propTypes = {