react-native/React/Base/RCTRootView.m

226 lines
5.6 KiB
Mathematica
Raw Normal View History

/**
* 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.
*/
#import "RCTRootView.h"
2015-04-02 14:33:21 +00:00
#import <objc/runtime.h>
#import "RCTAssert.h"
#import "RCTBridge.h"
#import "RCTContextExecutor.h"
#import "RCTEventDispatcher.h"
#import "RCTKeyCommands.h"
#import "RCTLog.h"
#import "RCTSourceCode.h"
#import "RCTTouchHandler.h"
#import "RCTUIManager.h"
#import "RCTUtils.h"
#import "RCTView.h"
#import "RCTWebViewExecutor.h"
2015-03-26 09:58:06 +00:00
#import "UIView+React.h"
@interface RCTBridge (RCTRootView)
@property (nonatomic, weak, readonly) RCTBridge *batchedBridge;
@end
2015-04-02 14:33:21 +00:00
@interface RCTUIManager (RCTRootView)
- (NSNumber *)allocateRootTag;
@end
@interface RCTRootContentView : RCTView <RCTInvalidating>
- (instancetype)initWithFrame:(CGRect)frame bridge:(RCTBridge *)bridge;
@end
@implementation RCTRootView
{
RCTBridge *_bridge;
2015-04-02 14:33:21 +00:00
NSString *_moduleName;
2015-03-26 01:59:42 +00:00
NSDictionary *_launchOptions;
RCTRootContentView *_contentView;
}
- (instancetype)initWithBridge:(RCTBridge *)bridge
2015-04-02 14:33:21 +00:00
moduleName:(NSString *)moduleName
{
RCTAssertMainThread();
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-04-02 14:33:21 +00:00
if ((self = [super init])) {
2015-04-11 22:08:00 +00:00
self.backgroundColor = [UIColor whiteColor];
2015-04-02 14:33:21 +00:00
_bridge = bridge;
_moduleName = moduleName;
2015-04-11 22:08:00 +00:00
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(javaScriptDidLoad:)
2015-04-11 22:08:00 +00:00
name:RCTJavaScriptDidLoadNotification
object:_bridge];
if (!_bridge.batchedBridge.isLoading) {
[self bundleFinishedLoading:_bridge.batchedBridge];
2015-04-11 22:08:00 +00:00
}
2015-04-02 14:33:21 +00:00
}
return self;
}
2015-03-26 01:59:42 +00:00
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
moduleName:(NSString *)moduleName
launchOptions:(NSDictionary *)launchOptions
{
2015-04-11 22:08:00 +00:00
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:bundleURL
moduleProvider:nil
launchOptions:launchOptions];
2015-04-02 14:33:21 +00:00
2015-04-11 22:08:00 +00:00
return [self initWithBridge:bridge moduleName:moduleName];
2015-04-02 14:33:21 +00:00
}
- (UIViewController *)backingViewController
{
2015-04-02 14:33:21 +00:00
return _backingViewController ?: [super backingViewController];
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
RCT_IMPORT_METHOD(AppRegistry, runApplication)
RCT_IMPORT_METHOD(ReactNative, unmountComponentAtNodeAndRemoveContainer)
- (void)javaScriptDidLoad:(NSNotification *)notification
{
RCTBridge *bridge = notification.userInfo[@"bridge"];
[self bundleFinishedLoading:bridge];
}
- (void)bundleFinishedLoading:(RCTBridge *)bridge
{
2015-04-02 14:33:21 +00:00
dispatch_async(dispatch_get_main_queue(), ^{
if (!bridge.isValid) {
return;
}
2015-04-11 22:08:00 +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.
*/
2015-04-11 22:08:00 +00:00
[_contentView removeFromSuperview];
_contentView = [[RCTRootContentView alloc] initWithFrame:self.bounds
bridge:bridge];
[self addSubview:_contentView];
NSString *moduleName = _moduleName ?: @"";
NSDictionary *appParameters = @{
@"rootTag": _contentView.reactTag,
@"initialProps": _initialProperties ?: @{},
};
[bridge enqueueJSCall:@"AppRegistry.runApplication"
args:@[moduleName, appParameters]];
2015-04-02 14:33:21 +00:00
});
}
2015-04-02 14:33:21 +00:00
- (void)layoutSubviews
{
2015-04-02 14:33:21 +00:00
[super layoutSubviews];
2015-04-11 22:08:00 +00:00
if (_contentView) {
_contentView.frame = self.bounds;
}
}
2015-04-02 14:33:21 +00:00
- (NSNumber *)reactTag
{
2015-04-02 14:33:21 +00:00
return _contentView.reactTag;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_contentView invalidate];
}
@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
@implementation RCTRootContentView
{
__weak RCTBridge *_bridge;
RCTTouchHandler *_touchHandler;
}
- (instancetype)initWithFrame:(CGRect)frame
bridge:(RCTBridge *)bridge
{
if ((self = [super init])) {
_bridge = bridge;
[self setUp];
self.frame = frame;
}
return self;
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
if (self.reactTag && _bridge.isValid) {
[_bridge.uiManager setFrame:self.bounds forRootView:self];
}
}
- (void)setUp
{
/**
* 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.
*/
self.reactTag = [_bridge.uiManager allocateRootTag];
[self addGestureRecognizer:[[RCTTouchHandler alloc] initWithBridge:_bridge]];
[_bridge.uiManager registerRootView:self];
}
- (BOOL)isValid
{
return self.userInteractionEnabled;
}
- (void)invalidate
{
if (self.isValid) {
self.userInteractionEnabled = NO;
[self removeFromSuperview];
[_bridge enqueueJSCall:@"ReactNative.unmountComponentAtNodeAndRemoveContainer"
args:@[self.reactTag]];
}
}
@end