2015-07-28 14:31:26 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTModalHostView.h"
|
|
|
|
|
|
|
|
#import "RCTAssert.h"
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTModalHostViewController.h"
|
|
|
|
#import "RCTTouchHandler.h"
|
|
|
|
#import "RCTUIManager.h"
|
|
|
|
#import "UIView+React.h"
|
|
|
|
|
|
|
|
@implementation RCTModalHostView
|
|
|
|
{
|
2015-11-17 14:35:46 +00:00
|
|
|
__weak RCTBridge *_bridge;
|
2015-10-06 17:09:47 +00:00
|
|
|
BOOL _isPresented;
|
2015-07-28 14:31:26 +00:00
|
|
|
RCTModalHostViewController *_modalViewController;
|
|
|
|
RCTTouchHandler *_touchHandler;
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:coder)
|
2015-07-28 14:31:26 +00:00
|
|
|
|
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
|
|
|
{
|
|
|
|
if ((self = [super initWithFrame:CGRectZero])) {
|
|
|
|
_bridge = bridge;
|
2015-08-17 14:35:34 +00:00
|
|
|
_modalViewController = [RCTModalHostViewController new];
|
2015-07-28 14:31:26 +00:00
|
|
|
_touchHandler = [[RCTTouchHandler alloc] initWithBridge:bridge];
|
2015-10-06 17:09:47 +00:00
|
|
|
_isPresented = NO;
|
2015-07-28 14:31:26 +00:00
|
|
|
|
2015-09-19 00:18:09 +00:00
|
|
|
__weak typeof(self) weakSelf = self;
|
2015-07-28 14:31:26 +00:00
|
|
|
_modalViewController.boundsDidChangeBlock = ^(CGRect newBounds) {
|
|
|
|
[weakSelf notifyForBoundsChange:newBounds];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)notifyForBoundsChange:(CGRect)newBounds
|
|
|
|
{
|
2015-10-06 17:09:47 +00:00
|
|
|
if (_modalViewController.view && _isPresented) {
|
2015-07-28 14:31:26 +00:00
|
|
|
[_bridge.uiManager setFrame:newBounds forView:_modalViewController.view];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (NSArray<UIView *> *)reactSubviews
|
2015-07-28 14:31:26 +00:00
|
|
|
{
|
2015-11-03 22:45:46 +00:00
|
|
|
return _modalViewController.view ? @[_modalViewController.view] : @[];
|
2015-07-28 14:31:26 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (void)insertReactSubview:(UIView *)subview atIndex:(__unused NSInteger)atIndex
|
2015-07-28 14:31:26 +00:00
|
|
|
{
|
|
|
|
[subview addGestureRecognizer:_touchHandler];
|
|
|
|
_modalViewController.view = subview;
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (void)removeReactSubview:(UIView *)subview
|
2015-07-28 14:31:26 +00:00
|
|
|
{
|
|
|
|
RCTAssert(subview == _modalViewController.view, @"Cannot remove view other than modal view");
|
|
|
|
_modalViewController.view = nil;
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:09:47 +00:00
|
|
|
- (void)dismissModalViewController
|
|
|
|
{
|
|
|
|
if (_isPresented) {
|
|
|
|
[_modalViewController dismissViewControllerAnimated:self.animated completion:nil];
|
|
|
|
_isPresented = NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-31 00:03:23 +00:00
|
|
|
- (void)didMoveToWindow
|
2015-07-28 14:31:26 +00:00
|
|
|
{
|
2015-10-31 00:03:23 +00:00
|
|
|
[super didMoveToWindow];
|
2015-07-28 14:31:26 +00:00
|
|
|
|
2015-10-31 00:03:23 +00:00
|
|
|
if (!_isPresented && self.window) {
|
2015-08-13 15:27:16 +00:00
|
|
|
RCTAssert(self.reactViewController, @"Can't present modal view controller without a presenting view controller");
|
2015-07-31 18:23:29 +00:00
|
|
|
[self.reactViewController presentViewController:_modalViewController animated:self.animated completion:nil];
|
2015-10-06 17:09:47 +00:00
|
|
|
_isPresented = YES;
|
2015-10-31 00:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didMoveToSuperview
|
|
|
|
{
|
|
|
|
[super didMoveToSuperview];
|
|
|
|
|
|
|
|
if (_isPresented && !self.superview) {
|
2015-10-06 17:09:47 +00:00
|
|
|
[self dismissModalViewController];
|
2015-07-28 14:31:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 16:09:10 +00:00
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2015-10-06 17:09:47 +00:00
|
|
|
[self dismissModalViewController];
|
2015-08-13 16:09:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isTransparent
|
|
|
|
{
|
|
|
|
return _modalViewController.modalPresentationStyle == UIModalPresentationCustom;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setTransparent:(BOOL)transparent
|
|
|
|
{
|
|
|
|
_modalViewController.modalPresentationStyle = transparent ? UIModalPresentationCustom : UIModalPresentationFullScreen;
|
|
|
|
}
|
|
|
|
|
2015-07-28 14:31:26 +00:00
|
|
|
@end
|