From d7e0fe2c2c0cf8412d97ae16d0627c1111b59bf8 Mon Sep 17 00:00:00 2001 From: Jeff Thomas Date: Tue, 19 Jun 2018 13:18:51 -0700 Subject: [PATCH] AssetsLibrary.framework --> Photos.framework; ReactNative stuff Summary: AssetsLibrary.framework --> Photos.framework; ReactNative Port RN off of AssetsLibrary where trivial, lazy load AssetsLibrary where remaining. Reviewed By: fkgozali Differential Revision: D7999924 fbshipit-source-id: 1d48a2d820984564459ed43680cd3f601399d352 --- .../RCTAssetsLibraryRequestHandler.m | 16 ++++- Libraries/CameraRoll/RCTCameraRollManager.m | 65 ++++++++++++++----- 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/Libraries/CameraRoll/RCTAssetsLibraryRequestHandler.m b/Libraries/CameraRoll/RCTAssetsLibraryRequestHandler.m index e9637d7c4..9721b392b 100644 --- a/Libraries/CameraRoll/RCTAssetsLibraryRequestHandler.m +++ b/Libraries/CameraRoll/RCTAssetsLibraryRequestHandler.m @@ -8,6 +8,8 @@ #import "RCTAssetsLibraryRequestHandler.h" #import +#import +#import #import #import @@ -23,10 +25,20 @@ RCT_EXPORT_MODULE() @synthesize bridge = _bridge; - +static Class _ALAssetsLibrary = nil; +static void ensureAssetsLibLoaded(void) +{ + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + void * handle = dlopen("/System/Library/Frameworks/AssetsLibrary.framework/AssetsLibrary", RTLD_LAZY); +#pragma unused(handle) + _ALAssetsLibrary = objc_getClass("ALAssetsLibrary"); + }); +} - (ALAssetsLibrary *)assetsLibrary { - return _assetsLibrary ?: (_assetsLibrary = [ALAssetsLibrary new]); + ensureAssetsLibLoaded(); + return _assetsLibrary ?: (_assetsLibrary = [_ALAssetsLibrary new]); } #pragma mark - RCTURLRequestHandler diff --git a/Libraries/CameraRoll/RCTCameraRollManager.m b/Libraries/CameraRoll/RCTCameraRollManager.m index b5d9a85da..e6e397392 100644 --- a/Libraries/CameraRoll/RCTCameraRollManager.m +++ b/Libraries/CameraRoll/RCTCameraRollManager.m @@ -11,6 +11,8 @@ #import #import #import +#import +#import #import #import @@ -44,22 +46,52 @@ RCT_ENUM_CONVERTER(ALAssetsGroupType, (@{ }), ALAssetsGroupSavedPhotos, integerValue) +static Class _ALAssetsFilter = nil; +static NSString *_ALAssetsGroupPropertyName = nil; +static NSString *_ALAssetPropertyAssetURL = nil; +static NSString *_ALAssetPropertyLocation = nil; +static NSString *_ALAssetPropertyDate = nil; +static NSString *_ALAssetPropertyType = nil; +static NSString *_ALAssetPropertyDuration = nil; +static NSString *_ALAssetTypeVideo = nil; +static NSString *lookupNSString(void * handle, const char * name) +{ + void ** sym = dlsym(handle, name); + return (__bridge NSString *)(sym ? *sym : nil); +} +static void ensureAssetsLibLoaded(void) +{ + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + void * handle = dlopen("/System/Library/Frameworks/AssetsLibrary.framework/AssetsLibrary", RTLD_LAZY); + RCTAssert(handle != NULL, @"Unable to load AssetsLibrary.framework."); + _ALAssetsFilter = objc_getClass("ALAssetsFilter"); + _ALAssetsGroupPropertyName = lookupNSString(handle, "ALAssetsGroupPropertyName"); + _ALAssetPropertyAssetURL = lookupNSString(handle, "ALAssetPropertyAssetURL"); + _ALAssetPropertyLocation = lookupNSString(handle, "ALAssetPropertyLocation"); + _ALAssetPropertyDate = lookupNSString(handle, "ALAssetPropertyDate"); + _ALAssetPropertyType = lookupNSString(handle, "ALAssetPropertyType"); + _ALAssetPropertyDuration = lookupNSString(handle, "ALAssetPropertyDuration"); + _ALAssetTypeVideo = lookupNSString(handle, "ALAssetTypeVideo"); + }); +} + + (ALAssetsFilter *)ALAssetsFilter:(id)json { static NSDictionary *options; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ + ensureAssetsLibLoaded(); options = @{ - // New values - @"photos": [ALAssetsFilter allPhotos], - @"videos": [ALAssetsFilter allVideos], - @"all": [ALAssetsFilter allAssets], + @"photos": [_ALAssetsFilter allPhotos], + @"videos": [_ALAssetsFilter allVideos], + @"all": [_ALAssetsFilter allAssets], // Legacy values - @"Photos": [ALAssetsFilter allPhotos], - @"Videos": [ALAssetsFilter allVideos], - @"All": [ALAssetsFilter allAssets], + @"Photos": [_ALAssetsFilter allPhotos], + @"Videos": [_ALAssetsFilter allVideos], + @"All": [_ALAssetsFilter allAssets], }; }); @@ -68,7 +100,7 @@ RCT_ENUM_CONVERTER(ALAssetsGroupType, (@{ RCTLogError(@"Invalid filter option: '%@'. Expected one of 'photos'," "'videos' or 'all'.", json); } - return filter ?: [ALAssetsFilter allPhotos]; + return filter ?: [_ALAssetsFilter allPhotos]; } @end @@ -149,6 +181,7 @@ RCT_EXPORT_METHOD(getPhotos:(NSDictionary *)params { checkPhotoLibraryConfig(); + ensureAssetsLibLoaded(); NSUInteger first = [RCTConvert NSInteger:params[@"first"]]; NSString *afterCursor = [RCTConvert NSString:params[@"after"]]; NSString *groupName = [RCTConvert NSString:params[@"groupName"]]; @@ -161,12 +194,12 @@ RCT_EXPORT_METHOD(getPhotos:(NSDictionary *)params NSMutableArray *> *assets = [NSMutableArray new]; [_bridge.assetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stopGroups) { - if (group && (groupName == nil || [groupName isEqualToString:[group valueForProperty:ALAssetsGroupPropertyName]])) { + if (group && (groupName == nil || [groupName isEqualToString:[group valueForProperty:_ALAssetsGroupPropertyName]])) { [group setAssetsFilter:assetType]; [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stopAssets) { if (result) { - NSString *uri = ((NSURL *)[result valueForProperty:ALAssetPropertyAssetURL]).absoluteString; + NSString *uri = ((NSURL *)[result valueForProperty:_ALAssetPropertyAssetURL]).absoluteString; if (afterCursor && !foundAfter) { if ([afterCursor isEqualToString:uri]) { foundAfter = YES; @@ -183,18 +216,18 @@ RCT_EXPORT_METHOD(getPhotos:(NSDictionary *)params return; } CGSize dimensions = [result defaultRepresentation].dimensions; - CLLocation *loc = [result valueForProperty:ALAssetPropertyLocation]; - NSDate *date = [result valueForProperty:ALAssetPropertyDate]; + CLLocation *loc = [result valueForProperty:_ALAssetPropertyLocation]; + NSDate *date = [result valueForProperty:_ALAssetPropertyDate]; NSString *filename = [result defaultRepresentation].filename; int64_t duration = 0; - if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) { - duration = [[result valueForProperty:ALAssetPropertyDuration] intValue]; + if ([[result valueForProperty:_ALAssetPropertyType] isEqualToString:_ALAssetTypeVideo]) { + duration = [[result valueForProperty:_ALAssetPropertyDuration] intValue]; } [assets addObject:@{ @"node": @{ - @"type": [result valueForProperty:ALAssetPropertyType], - @"group_name": [group valueForProperty:ALAssetsGroupPropertyName], + @"type": [result valueForProperty:_ALAssetPropertyType], + @"group_name": [group valueForProperty:_ALAssetsGroupPropertyName], @"image": @{ @"uri": uri, @"filename" : filename ?: [NSNull null],