Skip to content

Commit

Permalink
AssetsLibrary.framework --> Photos.framework; ReactNative stuff
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jdthomas authored and facebook-github-bot committed Jun 19, 2018
1 parent 53ea464 commit 366c80e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
16 changes: 14 additions & 2 deletions RCTAssetsLibraryRequestHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -23,10 +25,20 @@ @implementation RCTAssetsLibraryRequestHandler
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
Expand Down
65 changes: 49 additions & 16 deletions RCTCameraRollManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -44,22 +46,52 @@ @implementation RCTConvert (ALAssetGroup)

}), 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],
};
});

Expand All @@ -68,7 +100,7 @@ + (ALAssetsFilter *)ALAssetsFilter:(id)json
RCTLogError(@"Invalid filter option: '%@'. Expected one of 'photos',"
"'videos' or 'all'.", json);
}
return filter ?: [ALAssetsFilter allPhotos];
return filter ?: [_ALAssetsFilter allPhotos];
}

@end
Expand Down Expand Up @@ -149,6 +181,7 @@ static void RCTResolvePromise(RCTPromiseResolveBlock resolve,
{
checkPhotoLibraryConfig();

ensureAssetsLibLoaded();
NSUInteger first = [RCTConvert NSInteger:params[@"first"]];
NSString *afterCursor = [RCTConvert NSString:params[@"after"]];
NSString *groupName = [RCTConvert NSString:params[@"groupName"]];
Expand All @@ -161,12 +194,12 @@ static void RCTResolvePromise(RCTPromiseResolveBlock resolve,
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;
Expand All @@ -183,18 +216,18 @@ static void RCTResolvePromise(RCTPromiseResolveBlock resolve,
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],
Expand Down

0 comments on commit 366c80e

Please sign in to comment.