Skip to content

Commit

Permalink
Added lightweight generic annotations
Browse files Browse the repository at this point in the history
Summary: public

Added lightweight genarics annotations to make the code more readable and help the compiler catch bugs.

Fixed some type bugs and improved bridge validation in a few places.

Reviewed By: javache

Differential Revision: D2600189

fb-gh-sync-id: f81e22f2cdc107bf8d0b15deec6d5b83aacc5b56
  • Loading branch information
nicklockwood authored and facebook-github-bot-7 committed Nov 3, 2015
1 parent 3156578 commit c5b990f
Show file tree
Hide file tree
Showing 78 changed files with 497 additions and 422 deletions.
6 changes: 5 additions & 1 deletion Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
14D6D7291B2222EF001FB087 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 14AADF041AC3DB95002390C9 /* libReact.a */; };
14DC67F41AB71881001358AB /* libRCTPushNotification.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 14DC67F11AB71876001358AB /* libRCTPushNotification.a */; };
3578590A1B28D2CF00341EDB /* libRCTLinking.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 357859011B28D2C500341EDB /* libRCTLinking.a */; };
3D36915B1BDA8CBB007B22D8 /* uie_thumb_big.png in Resources */ = {isa = PBXBuildFile; fileRef = 3D36915A1BDA8CBB007B22D8 /* uie_thumb_big.png */; settings = {ASSET_TAGS = (); }; };
3D36915B1BDA8CBB007B22D8 /* uie_thumb_big.png in Resources */ = {isa = PBXBuildFile; fileRef = 3D36915A1BDA8CBB007B22D8 /* uie_thumb_big.png */; };
3DB99D0C1BA0340600302749 /* UIExplorerIntegrationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3DB99D0B1BA0340600302749 /* UIExplorerIntegrationTests.m */; };
834C36EC1AF8DED70019C93C /* libRCTSettings.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 834C36D21AF8DA610019C93C /* libRCTSettings.a */; };
83636F8F1B53F22C009F943E /* RCTUIManagerScenarioTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 83636F8E1B53F22C009F943E /* RCTUIManagerScenarioTests.m */; };
Expand Down Expand Up @@ -1082,6 +1082,7 @@
"$(inherited)",
);
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_MISSING_NEWLINE = YES;
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
Expand Down Expand Up @@ -1109,6 +1110,7 @@
WARNING_CFLAGS = (
"-Wextra",
"-Wall",
"-Wincompatible-pointer-types",
);
};
name = Debug;
Expand Down Expand Up @@ -1139,6 +1141,7 @@
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_MISSING_NEWLINE = YES;
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
Expand Down Expand Up @@ -1166,6 +1169,7 @@
WARNING_CFLAGS = (
"-Wextra",
"-Wall",
"-Wincompatible-pointer-types",
);
};
name = Release;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/ART/Brushes/ARTLinearGradient.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ @implementation ARTLinearGradient
CGPoint _endPoint;
}

- (instancetype)initWithArray:(NSArray *)array
- (instancetype)initWithArray:(NSArray<NSNumber *> *)array
{
if ((self = [super initWithArray:array])) {
if (array.count < 5) {
Expand Down
2 changes: 1 addition & 1 deletion Libraries/ART/Brushes/ARTPattern.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ @implementation ARTPattern
CGRect _rect;
}

- (instancetype)initWithArray:(NSArray *)array
- (instancetype)initWithArray:(NSArray<id /* imagesource + numbers */> *)array
{
if ((self = [super initWithArray:array])) {
if (array.count < 6) {
Expand Down
2 changes: 1 addition & 1 deletion Libraries/ART/Brushes/ARTRadialGradient.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ @implementation ARTRadialGradient
CGFloat _radiusRatio;
}

- (instancetype)initWithArray:(NSArray *)array
- (instancetype)initWithArray:(NSArray<NSNumber *> *)array
{
if ((self = [super initWithArray:array])) {
if (array.count < 7) {
Expand Down
2 changes: 1 addition & 1 deletion Libraries/ART/Brushes/ARTSolidColor.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ @implementation ARTSolidColor
CGColorRef _color;
}

- (instancetype)initWithArray:(NSArray *)array
- (instancetype)initWithArray:(NSArray<NSNumber *> *)array
{
if ((self = [super initWithArray:array])) {
_color = CGColorRetain([RCTConvert CGColor:array offset:1]);
Expand Down
4 changes: 2 additions & 2 deletions Libraries/ActionSheetIOS/RCTActionSheetManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ - (CGRect)sourceRectInView:(UIView *)sourceView
}

NSString *title = [RCTConvert NSString:options[@"title"]];
NSArray *buttons = [RCTConvert NSStringArray:options[@"options"]];
NSArray<NSString *> *buttons = [RCTConvert NSStringArray:options[@"options"]];
NSInteger destructiveButtonIndex = options[@"destructiveButtonIndex"] ? [RCTConvert NSInteger:options[@"destructiveButtonIndex"]] : -1;
NSInteger cancelButtonIndex = options[@"cancelButtonIndex"] ? [RCTConvert NSInteger:options[@"cancelButtonIndex"]] : -1;

Expand Down Expand Up @@ -151,7 +151,7 @@ - (CGRect)sourceRectInView:(UIView *)sourceView
return;
}

NSMutableArray *items = [NSMutableArray array];
NSMutableArray<id /* NSString or NSURL */> *items = [NSMutableArray array];
NSString *message = [RCTConvert NSString:options[@"message"]];
if (message) {
[items addObject:message];
Expand Down
10 changes: 10 additions & 0 deletions Libraries/CameraRoll/RCTCameraRollManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

#import <AssetsLibrary/AssetsLibrary.h>

#import "RCTBridgeModule.h"
#import "RCTConvert.h"

@interface RCTConvert (ALAssetGroup)

+ (ALAssetsGroupType)ALAssetsGroupType:(id)json;
+ (ALAssetsFilter *)ALAssetsFilter:(id)json;

@end

@interface RCTCameraRollManager : NSObject <RCTBridgeModule>

Expand Down
97 changes: 64 additions & 33 deletions Libraries/CameraRoll/RCTCameraRollManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,70 @@

#import "RCTCameraRollManager.h"

#import <AssetsLibrary/AssetsLibrary.h>
#import <CoreLocation/CoreLocation.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#import "RCTAssetsLibraryImageLoader.h"
#import "RCTBridge.h"
#import "RCTConvert.h"
#import "RCTImageLoader.h"
#import "RCTLog.h"
#import "RCTUtils.h"

@implementation RCTConvert (ALAssetGroup)

RCT_ENUM_CONVERTER(ALAssetsGroupType, (@{

// New values
@"album": @(ALAssetsGroupAlbum),
@"all": @(ALAssetsGroupAll),
@"event": @(ALAssetsGroupEvent),
@"faces": @(ALAssetsGroupFaces),
@"library": @(ALAssetsGroupLibrary),
@"photo-stream": @(ALAssetsGroupPhotoStream),
@"saved-photos": @(ALAssetsGroupSavedPhotos),

// Legacy values
@"Album": @(ALAssetsGroupAlbum),
@"All": @(ALAssetsGroupAll),
@"Event": @(ALAssetsGroupEvent),
@"Faces": @(ALAssetsGroupFaces),
@"Library": @(ALAssetsGroupLibrary),
@"PhotoStream": @(ALAssetsGroupPhotoStream),
@"SavedPhotos": @(ALAssetsGroupSavedPhotos),

}), ALAssetsGroupSavedPhotos, integerValue)

+ (ALAssetsFilter *)ALAssetsFilter:(id)json
{
static NSDictionary *options;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
options = @{

// New values
@"photos": [ALAssetsFilter allPhotos],
@"videos": [ALAssetsFilter allVideos],
@"all": [ALAssetsFilter allAssets],

// Legacy values
@"Photos": [ALAssetsFilter allPhotos],
@"Videos": [ALAssetsFilter allVideos],
@"All": [ALAssetsFilter allAssets],
};
});

ALAssetsFilter *filter = options[json ?: @"photos"];
if (!filter) {
RCTLogError(@"Invalid filter option: '%@'. Expected one of 'photos',"
"'videos' or 'all'.", json);
}
return filter ?: [ALAssetsFilter allPhotos];
}

@end

@implementation RCTCameraRollManager

RCT_EXPORT_MODULE()
Expand Down Expand Up @@ -49,7 +102,9 @@ @implementation RCTCameraRollManager
}];
}

- (void)callCallback:(RCTResponseSenderBlock)callback withAssets:(NSArray *)assets hasNextPage:(BOOL)hasNextPage
- (void)callCallback:(RCTResponseSenderBlock)callback
withAssets:(NSArray<NSDictionary *> *)assets
hasNextPage:(BOOL)hasNextPage
{
if (!assets.count) {
callback(@[@{
Expand All @@ -72,45 +127,21 @@ - (void)callCallback:(RCTResponseSenderBlock)callback withAssets:(NSArray *)asse
callback:(RCTResponseSenderBlock)callback
errorCallback:(RCTResponseErrorBlock)errorCallback)
{
NSUInteger first = [params[@"first"] integerValue];
NSString *afterCursor = params[@"after"];
NSString *groupTypesStr = params[@"groupTypes"];
NSString *groupName = params[@"groupName"];
NSString *assetType = params[@"assetType"];
ALAssetsGroupType groupTypes;

if ([groupTypesStr isEqualToString:@"Album"]) {
groupTypes = ALAssetsGroupAlbum;
} else if ([groupTypesStr isEqualToString:@"All"]) {
groupTypes = ALAssetsGroupAll;
} else if ([groupTypesStr isEqualToString:@"Event"]) {
groupTypes = ALAssetsGroupEvent;
} else if ([groupTypesStr isEqualToString:@"Faces"]) {
groupTypes = ALAssetsGroupFaces;
} else if ([groupTypesStr isEqualToString:@"Library"]) {
groupTypes = ALAssetsGroupLibrary;
} else if ([groupTypesStr isEqualToString:@"PhotoStream"]) {
groupTypes = ALAssetsGroupPhotoStream;
} else {
groupTypes = ALAssetsGroupSavedPhotos;
}
NSUInteger first = [RCTConvert NSInteger:params[@"first"]];
NSString *afterCursor = [RCTConvert NSString:params[@"after"]];
NSString *groupName = [RCTConvert NSString:params[@"groupName"]];
ALAssetsFilter *assetType = [RCTConvert ALAssetsFilter:params[@"assetType"]];
ALAssetsGroupType groupTypes = [RCTConvert ALAssetsGroupType:params[@"groupTypes"]];

BOOL __block foundAfter = NO;
BOOL __block hasNextPage = NO;
BOOL __block calledCallback = NO;
NSMutableArray *assets = [NSMutableArray new];
NSMutableArray<NSDictionary *> *assets = [NSMutableArray new];

[_bridge.assetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stopGroups) {
if (group && (groupName == nil || [groupName isEqualToString:[group valueForProperty:ALAssetsGroupPropertyName]])) {

if (assetType == nil || [assetType isEqualToString:@"Photos"]) {
[group setAssetsFilter:ALAssetsFilter.allPhotos];
} else if ([assetType isEqualToString:@"Videos"]) {
[group setAssetsFilter:ALAssetsFilter.allVideos];
} else if ([assetType isEqualToString:@"All"]) {
[group setAssetsFilter:ALAssetsFilter.allAssets];
}

[group setAssetsFilter:assetType];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stopAssets) {
if (result) {
NSString *uri = ((NSURL *)[result valueForProperty:ALAssetPropertyAssetURL]).absoluteString;
Expand Down
14 changes: 7 additions & 7 deletions Libraries/CameraRoll/RCTImagePickerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ @interface RCTImagePickerManager ()<UIImagePickerControllerDelegate, UINavigatio

@implementation RCTImagePickerManager
{
NSMutableArray *_pickers;
NSMutableArray *_pickerCallbacks;
NSMutableArray *_pickerCancelCallbacks;
NSMutableArray<UIImagePickerController *> *_pickers;
NSMutableArray<RCTResponseSenderBlock> *_pickerCallbacks;
NSMutableArray<RCTResponseSenderBlock> *_pickerCancelCallbacks;
}

RCT_EXPORT_MODULE(ImagePickerIOS);
Expand All @@ -42,7 +42,7 @@ - (instancetype)init

RCT_EXPORT_METHOD(canRecordVideos:(RCTResponseSenderBlock)callback)
{
NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
NSArray<NSString *> *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
callback(@[@([availableMediaTypes containsObject:(NSString *)kUTTypeMovie])]);
}

Expand All @@ -59,7 +59,7 @@ - (instancetype)init
cancelCallback(@[@"Camera access is unavailable in an app extension"]);
return;
}

UIWindow *keyWindow = RCTSharedApplication().keyWindow;
UIViewController *rootViewController = keyWindow.rootViewController;

Expand All @@ -86,15 +86,15 @@ - (instancetype)init
cancelCallback(@[@"Image picker is currently unavailable in an app extension"]);
return;
}

UIWindow *keyWindow = RCTSharedApplication().keyWindow;
UIViewController *rootViewController = keyWindow.rootViewController;

UIImagePickerController *imagePicker = [UIImagePickerController new];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

NSMutableArray *allowedTypes = [NSMutableArray new];
NSMutableArray<NSString *> *allowedTypes = [NSMutableArray new];
if ([config[@"showImages"] boolValue]) {
[allowedTypes addObject:(NSString *)kUTTypeImage];
}
Expand Down
5 changes: 3 additions & 2 deletions Libraries/Geolocation/RCTLocationObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ @implementation RCTLocationObserver
{
CLLocationManager *_locationManager;
NSDictionary *_lastLocationEvent;
NSMutableArray *_pendingRequests;
NSMutableArray<RCTLocationRequest *> *_pendingRequests;
BOOL _observingLocation;
RCTLocationOptions _observerOptions;
}
Expand Down Expand Up @@ -249,7 +249,8 @@ - (void)timeout:(NSTimer *)timer

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations
{
// Create event
CLLocation *location = locations.lastObject;
Expand Down
6 changes: 3 additions & 3 deletions Libraries/Image/RCTGIFImageDecoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)imageData
if (imageCount > 1) {

NSTimeInterval duration = 0;
NSMutableArray *delays = [NSMutableArray arrayWithCapacity:imageCount];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:imageCount];
NSMutableArray<NSNumber *> *delays = [NSMutableArray arrayWithCapacity:imageCount];
NSMutableArray<id /* CGIMageRef */> *images = [NSMutableArray arrayWithCapacity:imageCount];
for (size_t i = 0; i < imageCount; i++) {

CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, i, NULL);
Expand Down Expand Up @@ -75,7 +75,7 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)imageData
}
CFRelease(imageSource);

NSMutableArray *keyTimes = [NSMutableArray arrayWithCapacity:delays.count];
NSMutableArray<NSNumber *> *keyTimes = [NSMutableArray arrayWithCapacity:delays.count];
NSTimeInterval runningDuration = 0;
for (NSNumber *delayNumber in delays) {
[keyTimes addObject:@(runningDuration / duration)];
Expand Down
12 changes: 6 additions & 6 deletions Libraries/Image/RCTImageLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ - (void)setReactKeyframeAnimation:(CAKeyframeAnimation *)reactKeyframeAnimation

@implementation RCTImageLoader
{
NSArray *_loaders;
NSArray *_decoders;
NSArray<id<RCTImageURLLoader>> *_loaders;
NSArray<id<RCTImageDataDecoder>> *_decoders;
NSURLCache *_cache;
}

Expand All @@ -47,14 +47,14 @@ @implementation RCTImageLoader
- (void)setBridge:(RCTBridge *)bridge
{
// Get image loaders and decoders
NSMutableArray *loaders = [NSMutableArray array];
NSMutableArray *decoders = [NSMutableArray array];
NSMutableArray<id<RCTImageURLLoader>> *loaders = [NSMutableArray array];
NSMutableArray<id<RCTImageDataDecoder>> *decoders = [NSMutableArray array];
for (id<RCTBridgeModule> module in bridge.modules.allValues) {
if ([module conformsToProtocol:@protocol(RCTImageURLLoader)]) {
[loaders addObject:module];
[loaders addObject:(id<RCTImageURLLoader>)module];
}
if ([module conformsToProtocol:@protocol(RCTImageDataDecoder)]) {
[decoders addObject:module];
[decoders addObject:(id<RCTImageDataDecoder>)module];
}
}

Expand Down
2 changes: 1 addition & 1 deletion Libraries/Network/RCTHTTPRequestHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ - (BOOL)isValid

- (BOOL)canHandleRequest:(NSURLRequest *)request
{
static NSSet *schemes = nil;
static NSSet<NSString *> *schemes = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// technically, RCTHTTPRequestHandler can handle file:// as well,
Expand Down
Loading

0 comments on commit c5b990f

Please sign in to comment.