Permalink
Browse files

Apple TV support 1: existing Objective C code should compile for tvOS

Summary:
First commit for Apple TV support: changes to existing Objective-C code so that it will compile correctly for tvOS.
Closes #9649

Differential Revision: D3916021

Pulled By: javache

fbshipit-source-id: 34acc9daf3efff835ffe38c43ba5d4098a02c830
  • Loading branch information...
1 parent 3395310 commit d368ebfab2c389cc093f6a73e5c1021408cc6a73 @dlowder-salesforce dlowder-salesforce committed with Facebook Github Bot 5 Sep 27, 2016
@@ -19,7 +19,9 @@
#import "RCTJavaScriptLoader.h"
#import "RCTLinkingManager.h"
#import "RCTRootView.h"
+#if !TARGET_OS_TV
#import "RCTPushNotificationManager.h"
+#endif
@interface AppDelegate() <RCTBridgeDelegate>
@@ -79,6 +81,8 @@ - (void)loadSourceForBridge:(RCTBridge *)bridge
# pragma mark - Push Notifications
+#if !TARGET_OS_TV
+
// Required to register for notifications
- (void)application:(__unused UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
@@ -109,4 +113,6 @@ - (void)application:(__unused UIApplication *)application didReceiveLocalNotific
[RCTPushNotificationManager didReceiveLocalNotification:notification];
}
+#endif
+
@end
@@ -64,7 +64,9 @@ - (instancetype)initWithFrame:(CGRect)frame
[_resizableRootView setSizeFlexibility:RCTRootViewSizeFlexibilityHeight];
_currentSizeTextView = [UITextView new];
+#ifndef TARGET_OS_TV
_currentSizeTextView.editable = NO;
+#endif
_currentSizeTextView.text = @"Resizable view has not been resized yet";
_currentSizeTextView.textColor = [UIColor blackColor];
_currentSizeTextView.backgroundColor = [UIColor whiteColor];
@@ -63,11 +63,13 @@ - (void)testSize
- (void)testFamily
{
+#if !TARGET_OS_TV
{
UIFont *expected = [UIFont fontWithName:@"Cochin" size:14];
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"Cochin"}];
RCTAssertEqualFonts(expected, result);
}
+#endif
{
UIFont *expected = [UIFont fontWithName:@"HelveticaNeue" size:14];
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"Helvetica Neue"}];
@@ -135,6 +137,7 @@ - (void)testFamilyAndWeight
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"HelveticaNeue-Bold", @"fontWeight": @"normal"}];
RCTAssertEqualFonts(expected, result);
}
+#if !TARGET_OS_TV
{
UIFont *expected = [UIFont fontWithName:@"Cochin-Bold" size:14];
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"Cochin", @"fontWeight": @"700"}];
@@ -145,6 +148,7 @@ - (void)testFamilyAndWeight
UIFont *result = [RCTConvert UIFont:@{@"fontFamily": @"Cochin", @"fontWeight": @"100"}];
RCTAssertEqualFonts(expected, result);
}
+#endif
}
- (void)testFamilyAndStyle
@@ -96,12 +96,16 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
_textView = [[RCTUITextView alloc] initWithFrame:CGRectZero];
_textView.backgroundColor = [UIColor clearColor];
_textView.textColor = [UIColor blackColor];
+#if !TARGET_OS_TV
_textView.scrollsToTop = NO;
+#endif
_textView.scrollEnabled = NO;
_textView.delegate = self;
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
+#if !TARGET_OS_TV
_scrollView.scrollsToTop = NO;
+#endif
[_scrollView addSubview:_textView];
[self addSubview:_scrollView];
@@ -283,11 +287,13 @@ - (void)updatePlaceholder
if (_placeholder) {
_placeholderView = [[UITextView alloc] initWithFrame:self.bounds];
- _placeholderView.editable = NO;
_placeholderView.userInteractionEnabled = NO;
_placeholderView.backgroundColor = [UIColor clearColor];
_placeholderView.scrollEnabled = NO;
+#if !TARGET_OS_TV
+ _placeholderView.editable = NO;
_placeholderView.scrollsToTop = NO;
+#endif
_placeholderView.attributedText =
[[NSAttributedString alloc] initWithString:_placeholder attributes:@{
NSFontAttributeName : (_textView.font ? _textView.font : [self defaultPlaceholderFont]),
@@ -70,8 +70,11 @@ - (void)setUp
[_socket setDelegateDispatchQueue:_jsQueue];
NSURL *startDevToolsURL = [NSURL URLWithString:@"/launch-js-devtools" relativeToURL:_url];
- [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:startDevToolsURL] delegate:nil];
+ NSURLSession *session = [NSURLSession sharedSession];
+ NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:[NSURLRequest requestWithURL:startDevToolsURL]
+ completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){}];
+ [dataTask resume];
if (![self connectToProxy]) {
RCTLogError(@"Connection to %@ timed out. Are you running node proxy? If "
"you are running on the device, check if you have the right IP "
@@ -72,10 +72,14 @@ typedef NSURL RCTFileURL;
+ (UIKeyboardType)UIKeyboardType:(id)json;
+ (UIKeyboardAppearance)UIKeyboardAppearance:(id)json;
+ (UIReturnKeyType)UIReturnKeyType:(id)json;
+#if !TARGET_OS_TV
+ (UIDataDetectorTypes)UIDataDetectorTypes:(id)json;
+#endif
+ (UIViewContentMode)UIViewContentMode:(id)json;
+#if !TARGET_OS_TV
+ (UIBarStyle)UIBarStyle:(id)json;
+#endif
+ (CGFloat)CGFloat:(id)json;
+ (CGPoint)CGPoint:(id)json;
@@ -330,6 +330,7 @@ + (NSDate *)NSDate:(id)json
@"numeric": @(UIKeyboardTypeDecimalPad),
}), UIKeyboardTypeDefault, integerValue)
+#if !TARGET_OS_TV
RCT_MULTI_ENUM_CONVERTER(UIDataDetectorTypes, (@{
@"phoneNumber": @(UIDataDetectorTypePhoneNumber),
@"link": @(UIDataDetectorTypeLink),
@@ -338,6 +339,7 @@ + (NSDate *)NSDate:(id)json
@"none": @(UIDataDetectorTypeNone),
@"all": @(UIDataDetectorTypeAll),
}), UIDataDetectorTypePhoneNumber, unsignedLongLongValue)
+#endif
RCT_ENUM_CONVERTER(UIKeyboardAppearance, (@{
@"default": @(UIKeyboardAppearanceDefault),
@@ -379,10 +381,12 @@ + (NSDate *)NSDate:(id)json
@"stretch": @(UIViewContentModeScaleToFill),
}), UIViewContentModeScaleAspectFill, integerValue)
+#if !TARGET_OS_TV
RCT_ENUM_CONVERTER(UIBarStyle, (@{
@"default": @(UIBarStyleDefault),
@"black": @(UIBarStyleBlack),
}), UIBarStyleDefault, integerValue)
+#endif
// TODO: normalise the use of w/width so we can do away with the alias values (#6566645)
static void RCTConvertCGStructValue(const char *type, NSArray *fields, NSDictionary *aliases, CGFloat *result, id json)
@@ -92,14 +92,6 @@ RCT_EXTERN UIViewController *__nullable RCTPresentedViewController(void);
// Does this device support force touch (aka 3D Touch)?
RCT_EXTERN BOOL RCTForceTouchAvailable(void);
-// Return a UIAlertView initialized with the given values
-// or nil if running in an app extension
-RCT_EXTERN UIAlertView *__nullable RCTAlertView(NSString *title,
- NSString *__nullable message,
- id __nullable delegate,
- NSString *__nullable cancelButtonTitle,
- NSArray<NSString *> *__nullable otherButtonTitles);
-
// Create an NSError in the RCTErrorDomain
RCT_EXTERN NSError *RCTErrorWithMessage(NSString *message);
View
@@ -493,31 +493,6 @@ BOOL RCTForceTouchAvailable(void)
(RCTKeyWindow() ?: [UIView new]).traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable;
}
-UIAlertView *__nullable RCTAlertView(NSString *title,
- NSString *__nullable message,
- id __nullable delegate,
- NSString *__nullable cancelButtonTitle,
- NSArray<NSString *> *__nullable otherButtonTitles)
-{
- if (RCTRunningInAppExtension()) {
- RCTLogError(@"RCTAlertView is unavailable when running in an app extension");
- return nil;
- }
-
- UIAlertView *alertView = [UIAlertView new];
- alertView.title = title;
- alertView.message = message;
- alertView.delegate = delegate;
- if (cancelButtonTitle != nil) {
- [alertView addButtonWithTitle:cancelButtonTitle];
- alertView.cancelButtonIndex = 0;
- }
- for (NSString *buttonTitle in otherButtonTitles) {
- [alertView addButtonWithTitle:buttonTitle];
- }
- return alertView;
-}
-
NSError *RCTErrorWithMessage(NSString *message)
{
NSDictionary<NSString *, id> *errorInfo = @{NSLocalizedDescriptionKey: message};
@@ -12,6 +12,14 @@
#import "RCTBridgeModule.h"
#import "RCTInvalidating.h"
+typedef NS_ENUM(NSInteger, RCTAlertViewStyle) {
+ RCTAlertViewStyleDefault = 0,
+ RCTAlertViewStyleSecureTextInput,
+ RCTAlertViewStylePlainTextInput,
+ RCTAlertViewStyleLoginAndPasswordInput
+};
+
+
@interface RCTAlertManager : NSObject <RCTBridgeModule, RCTInvalidating>
@end
@@ -16,22 +16,21 @@
@implementation RCTConvert (UIAlertViewStyle)
-RCT_ENUM_CONVERTER(UIAlertViewStyle, (@{
- @"default": @(UIAlertViewStyleDefault),
- @"secure-text": @(UIAlertViewStyleSecureTextInput),
- @"plain-text": @(UIAlertViewStylePlainTextInput),
- @"login-password": @(UIAlertViewStyleLoginAndPasswordInput),
-}), UIAlertViewStyleDefault, integerValue)
+RCT_ENUM_CONVERTER(RCTAlertViewStyle, (@{
+ @"default": @(RCTAlertViewStyleDefault),
+ @"secure-text": @(RCTAlertViewStyleSecureTextInput),
+ @"plain-text": @(RCTAlertViewStylePlainTextInput),
+ @"login-password": @(RCTAlertViewStyleLoginAndPasswordInput),
+}), RCTAlertViewStyleDefault, integerValue)
@end
-@interface RCTAlertManager() <UIAlertViewDelegate>
+@interface RCTAlertManager()
@end
@implementation RCTAlertManager
{
- NSMutableArray<UIAlertView *> *_alerts;
NSMutableArray<UIAlertController *> *_alertControllers;
NSMutableArray<RCTResponseSenderBlock> *_alertCallbacks;
NSMutableArray<NSArray<NSString *> *> *_alertButtonKeys;
@@ -46,9 +45,6 @@ - (dispatch_queue_t)methodQueue
- (void)invalidate
{
- for (UIAlertView *alert in _alerts) {
- [alert dismissWithClickedButtonIndex:0 animated:YES];
- }
for (UIAlertController *alertController in _alertControllers) {
[alertController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
@@ -73,7 +69,7 @@ - (void)invalidate
{
NSString *title = [RCTConvert NSString:args[@"title"]];
NSString *message = [RCTConvert NSString:args[@"message"]];
- UIAlertViewStyle type = [RCTConvert UIAlertViewStyle:args[@"type"]];
+ RCTAlertViewStyle type = [RCTConvert RCTAlertViewStyle:args[@"type"]];
NSArray<NSDictionary *> *buttons = [RCTConvert NSDictionaryArray:args[@"buttons"]];
NSString *defaultValue = [RCTConvert NSString:args[@"defaultValue"]];
NSString *cancelButtonKey = [RCTConvert NSString:args[@"cancelButtonKey"]];
@@ -85,7 +81,7 @@ - (void)invalidate
}
if (buttons.count == 0) {
- if (type == UIAlertViewStyleDefault) {
+ if (type == RCTAlertViewStyleDefault) {
buttons = @[@{@"0": RCTUIKitLocalizedString(@"OK")}];
cancelButtonKey = @"0";
} else {
@@ -108,22 +104,22 @@ - (void)invalidate
message:nil
preferredStyle:UIAlertControllerStyleAlert];
switch (type) {
- case UIAlertViewStylePlainTextInput: {
+ case RCTAlertViewStylePlainTextInput: {
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.secureTextEntry = NO;
textField.text = defaultValue;
}];
break;
}
- case UIAlertViewStyleSecureTextInput: {
+ case RCTAlertViewStyleSecureTextInput: {
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = RCTUIKitLocalizedString(@"Password");
textField.secureTextEntry = YES;
textField.text = defaultValue;
}];
break;
}
- case UIAlertViewStyleLoginAndPasswordInput: {
+ case RCTAlertViewStyleLoginAndPasswordInput: {
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = RCTUIKitLocalizedString(@"Login");
textField.text = defaultValue;
@@ -134,7 +130,7 @@ - (void)invalidate
}];
break;
}
- case UIAlertViewStyleDefault:
+ case RCTAlertViewStyleDefault:
break;
}
@@ -156,19 +152,19 @@ - (void)invalidate
style:buttonStyle
handler:^(__unused UIAlertAction *action) {
switch (type) {
- case UIAlertViewStylePlainTextInput:
- case UIAlertViewStyleSecureTextInput:
+ case RCTAlertViewStylePlainTextInput:
+ case RCTAlertViewStyleSecureTextInput:
callback(@[buttonKey, [alertController.textFields.firstObject text]]);
break;
- case UIAlertViewStyleLoginAndPasswordInput: {
+ case RCTAlertViewStyleLoginAndPasswordInput: {
NSDictionary<NSString *, NSString *> *loginCredentials = @{
@"login": [alertController.textFields.firstObject text],
@"password": [alertController.textFields.lastObject text]
};
callback(@[buttonKey, loginCredentials]);
break;
}
- case UIAlertViewStyleDefault:
+ case RCTAlertViewStyleDefault:
callback(@[buttonKey]);
break;
}
@@ -183,37 +179,4 @@ - (void)invalidate
[presentingController presentViewController:alertController animated:YES completion:nil];
}
-#pragma mark - UIAlertViewDelegate
-
-- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
-{
- NSUInteger index = [_alerts indexOfObject:alertView];
- RCTAssert(index != NSNotFound, @"Dismissed alert was not recognised");
-
- RCTResponseSenderBlock callback = _alertCallbacks[index];
- NSArray<NSString *> *buttonKeys = _alertButtonKeys[index];
-
- switch (alertView.alertViewStyle) {
- case UIAlertViewStylePlainTextInput:
- case UIAlertViewStyleSecureTextInput:
- callback(@[buttonKeys[buttonIndex], [alertView textFieldAtIndex:0].text]);
- break;
- case UIAlertViewStyleLoginAndPasswordInput: {
- NSDictionary<NSString *, NSString *> *loginCredentials = @{
- @"login": [alertView textFieldAtIndex:0].text,
- @"password": [alertView textFieldAtIndex:1].text,
- };
- callback(@[buttonKeys[buttonIndex], loginCredentials]);
- break;
- }
- case UIAlertViewStyleDefault:
- callback(@[buttonKeys[buttonIndex]]);
- break;
- }
-
- [_alerts removeObjectAtIndex:index];
- [_alertCallbacks removeObjectAtIndex:index];
- [_alertButtonKeys removeObjectAtIndex:index];
-}
-
@end
@@ -67,7 +67,11 @@ static void RCTAppendError(NSDictionary *error, NSMutableArray<NSDictionary *> *
static NSString *storageDirectory = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
+#if TARGET_OS_TV
+ storageDirectory = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
+#else
storageDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
+#endif
storageDirectory = [storageDirectory stringByAppendingPathComponent:RCTStorageDirectory];
});
return storageDirectory;
@@ -214,6 +218,10 @@ - (NSDictionary *)_ensureSetup
{
RCTAssertThread(RCTGetMethodQueue(), @"Must be executed on storage thread");
+#if TARGET_OS_TV
+ RCTLogWarn(@"Persistent storage is not supported on tvOS, your data may be removed at any point.")
+#endif
+
NSError *error = nil;
if (!RCTHasCreatedStorageDirectory) {
[[NSFileManager defaultManager] createDirectoryAtPath:RCTGetStorageDirectory()
Oops, something went wrong.

0 comments on commit d368ebf

Please sign in to comment.