Replaced RCTSparseArray with NSDictionary

Reviewed By: jspahrsummers

Differential Revision: D2651920

fb-gh-sync-id: 953e2ea33abfc7a3a553da95b13e9ab2bccc5a1c
This commit is contained in:
Nick Lockwood 2015-11-14 10:25:00 -08:00 committed by facebook-github-bot-4
parent a48929b591
commit 81c289a9cd
1 changed files with 39 additions and 38 deletions

View File

@ -46,7 +46,7 @@ RCT_ENUM_CONVERTER(ALAssetsGroupType, (@{
+ (ALAssetsFilter *)ALAssetsFilter:(id)json
{
static NSDictionary *options;
static NSDictionary<NSString *, ALAssetsFilter *> *options;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
options = @{
@ -102,29 +102,31 @@ RCT_EXPORT_METHOD(saveImageWithTag:(NSString *)imageTag
}];
}
- (void)callCallback:(RCTResponseSenderBlock)callback
withAssets:(NSArray<NSDictionary *> *)assets
hasNextPage:(BOOL)hasNextPage
static void RCTCallCallback(RCTResponseSenderBlock callback,
NSArray<NSDictionary<NSString *, id> *> *assets,
BOOL hasNextPage)
{
if (!assets.count) {
callback(@[@{
@"edges": assets,
@"page_info": @{
@"has_next_page": @NO}
}]);
@"edges": assets,
@"page_info": @{
@"has_next_page": @NO,
}
}]);
return;
}
callback(@[@{
@"edges": assets,
@"page_info": @{
@"start_cursor": assets[0][@"node"][@"image"][@"uri"],
@"end_cursor": assets[assets.count - 1][@"node"][@"image"][@"uri"],
@"has_next_page": @(hasNextPage)}
}]);
@"edges": assets,
@"page_info": @{
@"start_cursor": assets[0][@"node"][@"image"][@"uri"],
@"end_cursor": assets[assets.count - 1][@"node"][@"image"][@"uri"],
@"has_next_page": @(hasNextPage),
}
}]);
}
RCT_EXPORT_METHOD(getPhotos:(NSDictionary *)params
callback:(RCTResponseSenderBlock)callback
successCallback:(RCTResponseSenderBlock)successCallback
errorCallback:(RCTResponseErrorBlock)errorCallback)
{
NSUInteger first = [RCTConvert NSInteger:params[@"first"]];
@ -136,7 +138,7 @@ RCT_EXPORT_METHOD(getPhotos:(NSDictionary *)params
BOOL __block foundAfter = NO;
BOOL __block hasNextPage = NO;
BOOL __block calledCallback = NO;
NSMutableArray<NSDictionary *> *assets = [NSMutableArray new];
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]])) {
@ -156,7 +158,7 @@ RCT_EXPORT_METHOD(getPhotos:(NSDictionary *)params
*stopGroups = YES;
hasNextPage = YES;
RCTAssert(calledCallback == NO, @"Called the callback before we finished processing the results.");
[self callCallback:callback withAssets:assets hasNextPage:hasNextPage];
RCTCallCallback(successCallback, assets, hasNextPage);
calledCallback = YES;
return;
}
@ -164,33 +166,32 @@ RCT_EXPORT_METHOD(getPhotos:(NSDictionary *)params
CLLocation *loc = [result valueForProperty:ALAssetPropertyLocation];
NSDate *date = [result valueForProperty:ALAssetPropertyDate];
[assets addObject:@{
@"node": @{
@"type": [result valueForProperty:ALAssetPropertyType],
@"group_name": [group valueForProperty:ALAssetsGroupPropertyName],
@"image": @{
@"uri": uri,
@"height": @(dimensions.height),
@"width": @(dimensions.width),
@"isStored": @YES,
},
@"timestamp": @(date.timeIntervalSince1970),
@"location": loc ?
@{
@"latitude": @(loc.coordinate.latitude),
@"longitude": @(loc.coordinate.longitude),
@"altitude": @(loc.altitude),
@"heading": @(loc.course),
@"speed": @(loc.speed),
} : @{},
}
}];
@"node": @{
@"type": [result valueForProperty:ALAssetPropertyType],
@"group_name": [group valueForProperty:ALAssetsGroupPropertyName],
@"image": @{
@"uri": uri,
@"height": @(dimensions.height),
@"width": @(dimensions.width),
@"isStored": @YES,
},
@"timestamp": @(date.timeIntervalSince1970),
@"location": loc ? @{
@"latitude": @(loc.coordinate.latitude),
@"longitude": @(loc.coordinate.longitude),
@"altitude": @(loc.altitude),
@"heading": @(loc.course),
@"speed": @(loc.speed),
} : @{},
}
}];
}
}];
} else {
// Sometimes the enumeration continues even if we set stop above, so we guard against calling the callback
// multiple times here.
if (!calledCallback) {
[self callCallback:callback withAssets:assets hasNextPage:hasNextPage];
RCTCallCallback(successCallback, assets, hasNextPage);
calledCallback = YES;
}
}