2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTRootView.h"
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
#import <objc/runtime.h>
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTContextExecutor.h"
|
2015-03-26 00:33:54 +00:00
|
|
|
#import "RCTDevMenu.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
#import "RCTKeyCommands.h"
|
|
|
|
#import "RCTLog.h"
|
2015-03-13 00:49:54 +00:00
|
|
|
#import "RCTSourceCode.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTTouchHandler.h"
|
|
|
|
#import "RCTUIManager.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
#import "RCTWebViewExecutor.h"
|
2015-03-26 09:58:06 +00:00
|
|
|
#import "UIView+React.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
NSString *const RCTJavaScriptDidLoadNotification = @"RCTJavaScriptDidLoadNotification";
|
2015-03-01 23:33:55 +00:00
|
|
|
NSString *const RCTReloadNotification = @"RCTReloadNotification";
|
2015-04-02 14:33:21 +00:00
|
|
|
NSString *const RCTReloadViewsNotification = @"RCTReloadViewsNotification";
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-03-26 01:59:42 +00:00
|
|
|
/**
|
|
|
|
* HACK(t6568049) This should be removed soon, hiding to prevent people from
|
|
|
|
* relying on it
|
|
|
|
*/
|
|
|
|
@interface RCTBridge (RCTRootView)
|
|
|
|
|
|
|
|
- (void)setJavaScriptExecutor:(id<RCTJavaScriptExecutor>)executor;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
@interface RCTUIManager (RCTRootView)
|
|
|
|
|
|
|
|
- (NSNumber *)allocateRootTag;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@implementation RCTRootView
|
|
|
|
{
|
2015-03-26 00:33:54 +00:00
|
|
|
RCTDevMenu *_devMenu;
|
2015-02-20 04:10:52 +00:00
|
|
|
RCTBridge *_bridge;
|
|
|
|
RCTTouchHandler *_touchHandler;
|
2015-04-02 14:33:21 +00:00
|
|
|
NSString *_moduleName;
|
2015-03-25 00:37:03 +00:00
|
|
|
BOOL _registered;
|
2015-03-26 01:59:42 +00:00
|
|
|
NSDictionary *_launchOptions;
|
2015-04-02 14:33:21 +00:00
|
|
|
UIView *_contentView;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
|
|
|
moduleName:(NSString *)moduleName
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-02 14:33:21 +00:00
|
|
|
RCTAssert(bridge, @"A bridge instance is required to create an RCTRootView");
|
|
|
|
RCTAssert(moduleName, @"A moduleName is required to create an RCTRootView");
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
if ((self = [super init])) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
_enableDevMenu = YES;
|
2015-02-20 04:10:52 +00:00
|
|
|
#endif
|
2015-04-02 14:33:21 +00:00
|
|
|
_bridge = bridge;
|
|
|
|
_moduleName = moduleName;
|
|
|
|
self.backgroundColor = [UIColor whiteColor];
|
|
|
|
[self setUp];
|
|
|
|
}
|
|
|
|
return self;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:59:42 +00:00
|
|
|
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
|
|
|
|
moduleName:(NSString *)moduleName
|
|
|
|
launchOptions:(NSDictionary *)launchOptions
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-02 14:33:21 +00:00
|
|
|
RCTBridge *bridge = [[RCTBridge alloc] initWithBundlePath:bundleURL.absoluteString
|
|
|
|
moduleProvider:nil
|
|
|
|
launchOptions:launchOptions];
|
|
|
|
return [self initWithBridge:bridge
|
|
|
|
moduleName:moduleName];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self tearDown];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setUp
|
|
|
|
{
|
|
|
|
if (!_registered) {
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(reload)
|
|
|
|
name:RCTReloadViewsNotification
|
|
|
|
object:_bridge];
|
|
|
|
if (_bridge.loaded) {
|
|
|
|
[self bundleFinishedLoading];
|
|
|
|
} else {
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(bundleFinishedLoading)
|
|
|
|
name:RCTJavaScriptDidLoadNotification
|
|
|
|
object:_bridge];
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
- (void)tearDown
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-10 14:28:10 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-04-02 14:33:21 +00:00
|
|
|
if (_registered) {
|
|
|
|
_registered = NO;
|
|
|
|
[_contentView removeGestureRecognizer:_touchHandler];
|
|
|
|
[_contentView removeFromSuperview];
|
|
|
|
[_touchHandler invalidate];
|
|
|
|
[_bridge enqueueJSCall:@"ReactIOS.unmountComponentAtNodeAndRemoveContainer"
|
|
|
|
args:@[_contentView.reactTag]];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
- (BOOL)isValid
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-02 14:33:21 +00:00
|
|
|
return _registered;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
[self tearDown];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIViewController *)backingViewController {
|
|
|
|
return _backingViewController ?: [super backingViewController];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 00:33:54 +00:00
|
|
|
- (BOOL)canBecomeFirstResponder
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
|
|
|
|
{
|
|
|
|
if (motion == UIEventSubtypeMotionShake && self.enableDevMenu) {
|
|
|
|
if (!_devMenu) {
|
2015-04-02 14:33:21 +00:00
|
|
|
_devMenu = [[RCTDevMenu alloc] initWithBridge:self.bridge];
|
2015-03-26 00:33:54 +00:00
|
|
|
}
|
|
|
|
[_devMenu show];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:42:43 +00:00
|
|
|
RCT_IMPORT_METHOD(AppRegistry, runApplication)
|
|
|
|
RCT_IMPORT_METHOD(ReactIOS, unmountComponentAtNodeAndRemoveContainer)
|
2015-02-24 17:06:57 +00:00
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
- (void)bundleFinishedLoading
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-02 14:33:21 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2015-03-25 00:37:03 +00:00
|
|
|
_registered = YES;
|
2015-04-10 14:28:10 +00:00
|
|
|
/**
|
|
|
|
* Every root view that is created must have a unique react tag.
|
|
|
|
* Numbering of these tags goes from 1, 11, 21, 31, etc
|
|
|
|
*
|
|
|
|
* NOTE: Since the bridge persists, the RootViews might be reused, so now
|
|
|
|
* the react tag is assigned every time we load new content.
|
|
|
|
*/
|
|
|
|
_contentView = [[UIView alloc] initWithFrame:self.bounds];
|
|
|
|
_contentView.reactTag = [_bridge.uiManager allocateRootTag];
|
|
|
|
_touchHandler = [[RCTTouchHandler alloc] initWithBridge:_bridge];
|
|
|
|
[_contentView addGestureRecognizer:_touchHandler];
|
|
|
|
[self addSubview:_contentView];
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
NSString *moduleName = _moduleName ?: @"";
|
|
|
|
NSDictionary *appParameters = @{
|
2015-04-08 12:42:43 +00:00
|
|
|
@"rootTag": _contentView.reactTag,
|
|
|
|
@"initialProps": self.initialProperties ?: @{},
|
|
|
|
};
|
2015-04-02 14:33:21 +00:00
|
|
|
[_bridge.uiManager registerRootView:_contentView];
|
2015-02-20 04:10:52 +00:00
|
|
|
[_bridge enqueueJSCall:@"AppRegistry.runApplication"
|
|
|
|
args:@[moduleName, appParameters]];
|
2015-04-02 14:33:21 +00:00
|
|
|
});
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
- (void)layoutSubviews
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-02 14:33:21 +00:00
|
|
|
[super layoutSubviews];
|
|
|
|
_contentView.frame = self.bounds;
|
|
|
|
if (_registered) {
|
|
|
|
[_bridge.uiManager setFrame:self.frame forRootView:_contentView];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
- (void)setFrame:(CGRect)frame
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-02 14:33:21 +00:00
|
|
|
[super setFrame:frame];
|
2015-04-08 04:42:11 +00:00
|
|
|
_contentView.frame = self.bounds;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
- (void)reload
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-02 14:33:21 +00:00
|
|
|
[self tearDown];
|
|
|
|
[self setUp];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
+ (void)reloadAll
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-02 14:33:21 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTReloadNotification
|
|
|
|
object:self];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
- (NSNumber *)reactTag
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-02 14:33:21 +00:00
|
|
|
return _contentView.reactTag;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 12:05:35 +00:00
|
|
|
- (void)startOrResetInteractionTiming
|
|
|
|
{
|
|
|
|
[_touchHandler startOrResetInteractionTiming];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *)endAndResetInteractionTiming
|
|
|
|
{
|
|
|
|
return [_touchHandler endAndResetInteractionTiming];
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@end
|
2015-04-02 14:33:21 +00:00
|
|
|
|
|
|
|
@implementation RCTUIManager (RCTRootView)
|
|
|
|
|
|
|
|
- (NSNumber *)allocateRootTag
|
|
|
|
{
|
|
|
|
NSNumber *rootTag = objc_getAssociatedObject(self, _cmd) ?: @1;
|
|
|
|
objc_setAssociatedObject(self, _cmd, @(rootTag.integerValue + 10), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
|
|
return rootTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|