2019-10-16 20:03:47 +03:00
|
|
|
/*
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-07-28 18:46:50 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-07-28 18:46:50 +03:00
|
|
|
*/
|
|
|
|
|
CocoaPods frameworks compatibility: Step 2 (#25619)
Summary:
This is my proposal for fixing `use_frameworks!` compatibility without breaking all `<React/*>` imports I outlined in https://github.com/facebook/react-native/pull/25393#issuecomment-508457700. If accepted, it will fix https://github.com/facebook/react-native/issues/25349.
It builds on the changes I made in https://github.com/facebook/react-native/pull/25496 by ensuring each podspec has a unique value for `header_dir` so that framework imports do not conflict. Every podspec which should be included in the `<React/*>` namespace now includes it's headers from `React-Core.podspec`.
The following pods can still be imported with `<React/*>` and so should not have breaking changes: `React-ART`,`React-DevSupport`, `React-CoreModules`, `React-RCTActionSheet`, `React-RCTAnimation`, `React-RCTBlob`, `React-RCTImage`, `React-RCTLinking`, `React-RCTNetwork`, `React-RCTPushNotification`, `React-RCTSettings`, `React-RCTText`, `React-RCTSettings`, `React-RCTVibration`, `React-RCTWebSocket` .
There are still a few breaking changes which I hope will be acceptable:
- `React-Core.podspec` has been moved to the root of the project. Any `Podfile` that references it will need to update the path.
- ~~`React-turbomodule-core`'s headers now live under `<turbomodule/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- ~~`React-turbomodulesamples`'s headers now live under `<turbomodulesamples/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- ~~`React-TypeSaferty`'s headers now live under `<TypeSafety/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511040967.
- ~~`React-jscallinvoker`'s headers now live under `<jscallinvoker/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- Each podspec now uses `s.static_framework = true`. This means that a minimum of CocoaPods 1.5 ([released in April 2018](http://blog.cocoapods.org/CocoaPods-1.5.0/)) is now required. This is needed so that the ` __has_include` conditions can still work when frameworks are enabled.
Still to do:
- ~~Including `React-turbomodule-core` with `use_frameworks!` enabled causes the C++ import failures we saw in https://github.com/facebook/react-native/issues/25349. I'm sure it will be possible to fix this but I need to dig deeper (perhaps a custom modulemap would be needed).~~ Addressed by https://github.com/facebook/react-native/pull/25619/commits/33573511f02f3502a28bad48e085e9a4b8608302.
- I haven't got Fabric working yet. I wonder if it would be acceptable to move Fabric out of the `<React/*>` namespace since it is new? �
## Changelog
[iOS] [Fixed] - Fixed compatibility with CocoaPods frameworks.
Pull Request resolved: https://github.com/facebook/react-native/pull/25619
Test Plan:
### FB
```
buck build catalyst
```
### Sample Project
Everything should work exactly as before, where `use_frameworks!` is not in `Podfile`s. I have a branch on my [sample project](https://github.com/jtreanor/react-native-cocoapods-frameworks) here which has `use_frameworks!` in its `Podfile` to demonstrate this is fixed.
You can see that it works with these steps:
1. `git clone git@github.com:jtreanor/react-native-cocoapods-frameworks.git`
2. `git checkout fix-frameworks-subspecs`
3. `cd ios && pod install`
4. `cd .. && react-native run-ios`
The sample app will build and run successfully. To see that it still works without frameworks, remove `use_frameworks!` from the `Podfile` and do steps 3 and 4 again.
### RNTesterPods
`RNTesterPodsPods` can now work with or without `use_frameworks!`.
1. Go to the `RNTester` directory and run `pod install`.
2. Run the tests in `RNTesterPods.xcworkspace` to see that everything still works fine.
3. Uncomment the `use_frameworks!` line at the top of `RNTester/Podfile` and run `pod install` again.
4. Run the tests again and see that it still works with frameworks enabled.
Reviewed By: PeteTheHeat
Differential Revision: D16465247
Pulled By: PeteTheHeat
fbshipit-source-id: cad837e9cced06d30cc5b372af1c65c7780b9e7a
2019-07-25 08:26:42 +03:00
|
|
|
#import <React/RCTImageCache.h>
|
2016-07-28 18:46:50 +03:00
|
|
|
|
|
|
|
#import <objc/runtime.h>
|
|
|
|
|
2016-11-23 18:47:52 +03:00
|
|
|
#import <ImageIO/ImageIO.h>
|
|
|
|
|
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
#import <React/RCTNetworking.h>
|
|
|
|
#import <React/RCTUtils.h>
|
2018-05-14 21:40:50 +03:00
|
|
|
#import <React/RCTResizeMode.h>
|
2016-11-23 18:47:52 +03:00
|
|
|
|
CocoaPods frameworks compatibility: Step 2 (#25619)
Summary:
This is my proposal for fixing `use_frameworks!` compatibility without breaking all `<React/*>` imports I outlined in https://github.com/facebook/react-native/pull/25393#issuecomment-508457700. If accepted, it will fix https://github.com/facebook/react-native/issues/25349.
It builds on the changes I made in https://github.com/facebook/react-native/pull/25496 by ensuring each podspec has a unique value for `header_dir` so that framework imports do not conflict. Every podspec which should be included in the `<React/*>` namespace now includes it's headers from `React-Core.podspec`.
The following pods can still be imported with `<React/*>` and so should not have breaking changes: `React-ART`,`React-DevSupport`, `React-CoreModules`, `React-RCTActionSheet`, `React-RCTAnimation`, `React-RCTBlob`, `React-RCTImage`, `React-RCTLinking`, `React-RCTNetwork`, `React-RCTPushNotification`, `React-RCTSettings`, `React-RCTText`, `React-RCTSettings`, `React-RCTVibration`, `React-RCTWebSocket` .
There are still a few breaking changes which I hope will be acceptable:
- `React-Core.podspec` has been moved to the root of the project. Any `Podfile` that references it will need to update the path.
- ~~`React-turbomodule-core`'s headers now live under `<turbomodule/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- ~~`React-turbomodulesamples`'s headers now live under `<turbomodulesamples/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- ~~`React-TypeSaferty`'s headers now live under `<TypeSafety/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511040967.
- ~~`React-jscallinvoker`'s headers now live under `<jscallinvoker/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- Each podspec now uses `s.static_framework = true`. This means that a minimum of CocoaPods 1.5 ([released in April 2018](http://blog.cocoapods.org/CocoaPods-1.5.0/)) is now required. This is needed so that the ` __has_include` conditions can still work when frameworks are enabled.
Still to do:
- ~~Including `React-turbomodule-core` with `use_frameworks!` enabled causes the C++ import failures we saw in https://github.com/facebook/react-native/issues/25349. I'm sure it will be possible to fix this but I need to dig deeper (perhaps a custom modulemap would be needed).~~ Addressed by https://github.com/facebook/react-native/pull/25619/commits/33573511f02f3502a28bad48e085e9a4b8608302.
- I haven't got Fabric working yet. I wonder if it would be acceptable to move Fabric out of the `<React/*>` namespace since it is new? �
## Changelog
[iOS] [Fixed] - Fixed compatibility with CocoaPods frameworks.
Pull Request resolved: https://github.com/facebook/react-native/pull/25619
Test Plan:
### FB
```
buck build catalyst
```
### Sample Project
Everything should work exactly as before, where `use_frameworks!` is not in `Podfile`s. I have a branch on my [sample project](https://github.com/jtreanor/react-native-cocoapods-frameworks) here which has `use_frameworks!` in its `Podfile` to demonstrate this is fixed.
You can see that it works with these steps:
1. `git clone git@github.com:jtreanor/react-native-cocoapods-frameworks.git`
2. `git checkout fix-frameworks-subspecs`
3. `cd ios && pod install`
4. `cd .. && react-native run-ios`
The sample app will build and run successfully. To see that it still works without frameworks, remove `use_frameworks!` from the `Podfile` and do steps 3 and 4 again.
### RNTesterPods
`RNTesterPodsPods` can now work with or without `use_frameworks!`.
1. Go to the `RNTester` directory and run `pod install`.
2. Run the tests in `RNTesterPods.xcworkspace` to see that everything still works fine.
3. Uncomment the `use_frameworks!` line at the top of `RNTester/Podfile` and run `pod install` again.
4. Run the tests again and see that it still works with frameworks enabled.
Reviewed By: PeteTheHeat
Differential Revision: D16465247
Pulled By: PeteTheHeat
fbshipit-source-id: cad837e9cced06d30cc5b372af1c65c7780b9e7a
2019-07-25 08:26:42 +03:00
|
|
|
#import <React/RCTImageUtils.h>
|
2016-07-28 18:46:50 +03:00
|
|
|
|
2018-11-05 16:38:22 +03:00
|
|
|
static const NSUInteger RCTMaxCachableDecodedImageSizeInBytes = 2097152; // 2 MB
|
2016-07-28 18:46:50 +03:00
|
|
|
|
|
|
|
static NSString *RCTCacheKeyForImage(NSString *imageTag, CGSize size, CGFloat scale,
|
2018-11-05 16:38:22 +03:00
|
|
|
RCTResizeMode resizeMode)
|
2016-07-28 18:46:50 +03:00
|
|
|
{
|
2018-11-05 16:38:22 +03:00
|
|
|
return [NSString stringWithFormat:@"%@|%g|%g|%g|%lld",
|
|
|
|
imageTag, size.width, size.height, scale, (long long)resizeMode];
|
2016-07-28 18:46:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@implementation RCTImageCache
|
|
|
|
{
|
|
|
|
NSOperationQueue *_imageDecodeQueue;
|
|
|
|
NSCache *_decodedImageCache;
|
2018-11-05 16:38:22 +03:00
|
|
|
NSMutableDictionary *_cacheStaleTimes;
|
2016-07-28 18:46:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
2019-01-28 14:54:53 +03:00
|
|
|
if (self = [super init]) {
|
|
|
|
_decodedImageCache = [NSCache new];
|
|
|
|
_decodedImageCache.totalCostLimit = 20 * 1024 * 1024; // 20 MB
|
|
|
|
_cacheStaleTimes = [[NSMutableDictionary alloc] init];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(clearCache)
|
|
|
|
name:UIApplicationDidReceiveMemoryWarningNotification
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(clearCache)
|
|
|
|
name:UIApplicationWillResignActiveNotification
|
|
|
|
object:nil];
|
|
|
|
}
|
2018-12-01 02:02:49 +03:00
|
|
|
|
2016-07-28 18:46:50 +03:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)clearCache
|
|
|
|
{
|
|
|
|
[_decodedImageCache removeAllObjects];
|
2018-11-05 16:38:22 +03:00
|
|
|
@synchronized(_cacheStaleTimes) {
|
|
|
|
[_cacheStaleTimes removeAllObjects];
|
|
|
|
}
|
2016-07-28 18:46:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addImageToCache:(UIImage *)image
|
|
|
|
forKey:(NSString *)cacheKey
|
|
|
|
{
|
|
|
|
if (!image) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-25 13:42:15 +03:00
|
|
|
NSInteger bytes = image.reactDecodedImageBytes;
|
2016-07-28 18:46:50 +03:00
|
|
|
if (bytes <= RCTMaxCachableDecodedImageSizeInBytes) {
|
|
|
|
[self->_decodedImageCache setObject:image
|
|
|
|
forKey:cacheKey
|
|
|
|
cost:bytes];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIImage *)imageForUrl:(NSString *)url
|
|
|
|
size:(CGSize)size
|
|
|
|
scale:(CGFloat)scale
|
|
|
|
resizeMode:(RCTResizeMode)resizeMode
|
|
|
|
{
|
2018-11-05 16:38:22 +03:00
|
|
|
NSString *cacheKey = RCTCacheKeyForImage(url, size, scale, resizeMode);
|
|
|
|
@synchronized(_cacheStaleTimes) {
|
|
|
|
id staleTime = _cacheStaleTimes[cacheKey];
|
|
|
|
if (staleTime) {
|
|
|
|
if ([[NSDate new] compare:(NSDate *)staleTime] == NSOrderedDescending) {
|
|
|
|
// cached image has expired, clear it out to make room for others
|
|
|
|
[_cacheStaleTimes removeObjectForKey:cacheKey];
|
|
|
|
[_decodedImageCache removeObjectForKey:cacheKey];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-28 18:46:50 +03:00
|
|
|
return [_decodedImageCache objectForKey:cacheKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addImageToCache:(UIImage *)image
|
|
|
|
URL:(NSString *)url
|
|
|
|
size:(CGSize)size
|
|
|
|
scale:(CGFloat)scale
|
|
|
|
resizeMode:(RCTResizeMode)resizeMode
|
2019-02-06 13:02:19 +03:00
|
|
|
response:(NSURLResponse *)response
|
2016-07-28 18:46:50 +03:00
|
|
|
{
|
2019-02-06 13:02:19 +03:00
|
|
|
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
|
|
|
NSString *cacheKey = RCTCacheKeyForImage(url, size, scale, resizeMode);
|
|
|
|
BOOL shouldCache = YES;
|
|
|
|
NSString *responseDate = ((NSHTTPURLResponse *)response).allHeaderFields[@"Date"];
|
|
|
|
NSDate *originalDate = [self dateWithHeaderString:responseDate];
|
|
|
|
NSString *cacheControl = ((NSHTTPURLResponse *)response).allHeaderFields[@"Cache-Control"];
|
|
|
|
NSDate *staleTime;
|
|
|
|
NSArray<NSString *> *components = [cacheControl componentsSeparatedByString:@","];
|
|
|
|
for (NSString *component in components) {
|
|
|
|
if ([component containsString:@"no-cache"] || [component containsString:@"no-store"] || [component hasSuffix:@"max-age=0"]) {
|
|
|
|
shouldCache = NO;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
NSRange range = [component rangeOfString:@"max-age="];
|
|
|
|
if (range.location != NSNotFound) {
|
|
|
|
NSInteger seconds = [[component substringFromIndex:range.location + range.length] integerValue];
|
|
|
|
staleTime = [originalDate dateByAddingTimeInterval:(NSTimeInterval)seconds];
|
|
|
|
}
|
2018-11-05 16:38:22 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-06 13:02:19 +03:00
|
|
|
if (shouldCache) {
|
|
|
|
if (!staleTime && originalDate) {
|
|
|
|
NSString *expires = ((NSHTTPURLResponse *)response).allHeaderFields[@"Expires"];
|
|
|
|
NSString *lastModified = ((NSHTTPURLResponse *)response).allHeaderFields[@"Last-Modified"];
|
|
|
|
if (expires) {
|
|
|
|
staleTime = [self dateWithHeaderString:expires];
|
|
|
|
} else if (lastModified) {
|
|
|
|
NSDate *lastModifiedDate = [self dateWithHeaderString:lastModified];
|
|
|
|
if (lastModifiedDate) {
|
|
|
|
NSTimeInterval interval = [originalDate timeIntervalSinceDate:lastModifiedDate] / 10;
|
|
|
|
staleTime = [originalDate dateByAddingTimeInterval:interval];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (staleTime) {
|
|
|
|
@synchronized(_cacheStaleTimes) {
|
|
|
|
_cacheStaleTimes[cacheKey] = staleTime;
|
|
|
|
}
|
2018-11-05 16:38:22 +03:00
|
|
|
}
|
2019-02-06 13:02:19 +03:00
|
|
|
return [self addImageToCache:image forKey:cacheKey];
|
2018-11-05 16:38:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDate *)dateWithHeaderString:(NSString *)headerDateString {
|
2018-12-01 02:02:49 +03:00
|
|
|
static NSDateFormatter *formatter;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
formatter = [[NSDateFormatter alloc] init];
|
|
|
|
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
|
|
|
|
formatter.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
|
|
|
|
formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
|
|
|
|
});
|
|
|
|
|
|
|
|
return [formatter dateFromString:headerDateString];
|
2016-07-28 18:46:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|