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
This commit is contained in:
Jeff Thomas 2018-06-19 13:18:51 -07:00 committed by Facebook Github Bot
parent c6a52b6db6
commit d7e0fe2c2c
2 changed files with 63 additions and 18 deletions

View File

@ -8,6 +8,8 @@
#import "RCTAssetsLibraryRequestHandler.h"
#import <stdatomic.h>
#import <dlfcn.h>
#import <objc/runtime.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>
@ -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

View File

@ -11,6 +11,8 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
#import <dlfcn.h>
#import <objc/runtime.h>
#import <React/RCTBridge.h>
#import <React/RCTConvert.h>
@ -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<NSString *, ALAssetsFilter *> *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<NSDictionary<NSString *, id> *> *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],