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"
|
|
|
|
|
2017-01-09 08:09:38 +00:00
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
2015-07-28 14:31:26 +00:00
|
|
|
#import "RCTAssert.h"
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTModalHostViewController.h"
|
|
|
|
#import "RCTTouchHandler.h"
|
|
|
|
#import "RCTUIManager.h"
|
2017-04-01 10:16:52 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-07-28 14:31:26 +00:00
|
|
|
#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;
|
2016-04-20 18:19:37 +00:00
|
|
|
UIView *_reactSubview;
|
2017-08-17 22:05:26 +00:00
|
|
|
#if TARGET_OS_TV
|
|
|
|
UITapGestureRecognizer *_menuButtonGestureRecognizer;
|
|
|
|
#else
|
2016-09-07 13:06:12 +00:00
|
|
|
UIInterfaceOrientation _lastKnownOrientation;
|
2016-09-27 13:19:45 +00:00
|
|
|
#endif
|
2017-08-17 22:05:26 +00:00
|
|
|
|
2015-07-28 14:31:26 +00:00
|
|
|
}
|
|
|
|
|
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];
|
2016-04-20 18:19:37 +00:00
|
|
|
UIView *containerView = [UIView new];
|
2016-09-26 22:49:10 +00:00
|
|
|
containerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
|
2016-04-20 18:19:37 +00:00
|
|
|
_modalViewController.view = containerView;
|
2015-07-28 14:31:26 +00:00
|
|
|
_touchHandler = [[RCTTouchHandler alloc] initWithBridge:bridge];
|
2017-08-17 22:05:26 +00:00
|
|
|
#if TARGET_OS_TV
|
|
|
|
_menuButtonGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuButtonPressed:)];
|
|
|
|
_menuButtonGestureRecognizer.allowedPressTypes = @[@(UIPressTypeMenu)];
|
|
|
|
#endif
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-17 22:05:26 +00:00
|
|
|
#if TARGET_OS_TV
|
|
|
|
- (void)menuButtonPressed:(__unused UIGestureRecognizer *)gestureRecognizer
|
|
|
|
{
|
|
|
|
if (_onRequestClose) {
|
|
|
|
_onRequestClose(nil);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setOnRequestClose:(RCTDirectEventBlock)onRequestClose
|
|
|
|
{
|
|
|
|
_onRequestClose = onRequestClose;
|
|
|
|
if (_reactSubview) {
|
|
|
|
if (_onRequestClose && _menuButtonGestureRecognizer) {
|
|
|
|
[_reactSubview addGestureRecognizer:_menuButtonGestureRecognizer];
|
|
|
|
} else {
|
|
|
|
[_reactSubview removeGestureRecognizer:_menuButtonGestureRecognizer];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-28 14:31:26 +00:00
|
|
|
- (void)notifyForBoundsChange:(CGRect)newBounds
|
|
|
|
{
|
2016-04-20 18:19:37 +00:00
|
|
|
if (_reactSubview && _isPresented) {
|
2017-02-06 18:46:45 +00:00
|
|
|
[_bridge.uiManager setSize:newBounds.size forView:_reactSubview];
|
2016-09-07 13:06:12 +00:00
|
|
|
[self notifyForOrientationChange];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)notifyForOrientationChange
|
|
|
|
{
|
2016-09-27 13:19:45 +00:00
|
|
|
#if !TARGET_OS_TV
|
2016-09-07 13:06:12 +00:00
|
|
|
if (!_onOrientationChange) {
|
|
|
|
return;
|
2015-07-28 14:31:26 +00:00
|
|
|
}
|
2016-09-07 13:06:12 +00:00
|
|
|
|
2017-04-01 10:16:52 +00:00
|
|
|
UIInterfaceOrientation currentOrientation = [RCTSharedApplication() statusBarOrientation];
|
2016-09-07 13:06:12 +00:00
|
|
|
if (currentOrientation == _lastKnownOrientation) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_lastKnownOrientation = currentOrientation;
|
|
|
|
|
|
|
|
BOOL isPortrait = currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown;
|
|
|
|
NSDictionary *eventPayload =
|
|
|
|
@{
|
|
|
|
@"orientation": isPortrait ? @"portrait" : @"landscape",
|
|
|
|
};
|
|
|
|
_onOrientationChange(eventPayload);
|
2016-09-27 13:19:45 +00:00
|
|
|
#endif
|
2015-07-28 14:31:26 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 07:08:16 +00:00
|
|
|
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex
|
2015-07-28 14:31:26 +00:00
|
|
|
{
|
2016-04-20 18:19:37 +00:00
|
|
|
RCTAssert(_reactSubview == nil, @"Modal view can only have one subview");
|
2016-06-07 07:08:16 +00:00
|
|
|
[super insertReactSubview:subview atIndex:atIndex];
|
2017-01-09 08:09:38 +00:00
|
|
|
[_touchHandler attachToView:subview];
|
2017-08-17 22:05:26 +00:00
|
|
|
#if TARGET_OS_TV
|
|
|
|
if (_onRequestClose) {
|
|
|
|
[subview addGestureRecognizer:_menuButtonGestureRecognizer];
|
|
|
|
}
|
|
|
|
#endif
|
2016-02-22 12:43:51 +00:00
|
|
|
subview.autoresizingMask = UIViewAutoresizingFlexibleHeight |
|
|
|
|
UIViewAutoresizingFlexibleWidth;
|
2016-04-20 18:19:37 +00:00
|
|
|
|
|
|
|
[_modalViewController.view insertSubview:subview atIndex:0];
|
|
|
|
_reactSubview = subview;
|
2015-07-28 14:31:26 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (void)removeReactSubview:(UIView *)subview
|
2015-07-28 14:31:26 +00:00
|
|
|
{
|
2016-04-20 18:19:37 +00:00
|
|
|
RCTAssert(subview == _reactSubview, @"Cannot remove view other than modal view");
|
2017-08-17 22:05:26 +00:00
|
|
|
// Superclass (category) removes the `subview` from actual `superview`.
|
2016-06-07 07:08:16 +00:00
|
|
|
[super removeReactSubview:subview];
|
2017-01-09 08:09:38 +00:00
|
|
|
[_touchHandler detachFromView:subview];
|
2017-08-17 22:05:26 +00:00
|
|
|
#if TARGET_OS_TV
|
|
|
|
if (_menuButtonGestureRecognizer) {
|
|
|
|
[subview removeGestureRecognizer:_menuButtonGestureRecognizer];
|
|
|
|
}
|
|
|
|
#endif
|
2016-04-20 18:19:37 +00:00
|
|
|
_reactSubview = nil;
|
2015-07-28 14:31:26 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 07:08:16 +00:00
|
|
|
- (void)didUpdateReactSubviews
|
|
|
|
{
|
|
|
|
// Do nothing, as subview (singular) is managed by `insertReactSubview:atIndex:`
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:09:47 +00:00
|
|
|
- (void)dismissModalViewController
|
|
|
|
{
|
|
|
|
if (_isPresented) {
|
2016-08-23 23:50:23 +00:00
|
|
|
[_delegate dismissModalHostView:self withViewController:_modalViewController animated:[self hasAnimationType]];
|
2015-10-06 17:09:47 +00:00
|
|
|
_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");
|
2016-04-28 22:59:11 +00:00
|
|
|
|
2016-09-27 13:19:45 +00:00
|
|
|
#if !TARGET_OS_TV
|
2016-09-07 13:06:12 +00:00
|
|
|
_modalViewController.supportedInterfaceOrientations = [self supportedOrientationsMask];
|
2016-09-27 13:19:45 +00:00
|
|
|
#endif
|
2016-04-28 22:59:11 +00:00
|
|
|
if ([self.animationType isEqualToString:@"fade"]) {
|
|
|
|
_modalViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
|
|
|
|
} else if ([self.animationType isEqualToString:@"slide"]) {
|
|
|
|
_modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
|
|
|
|
}
|
2017-06-21 02:02:27 +00:00
|
|
|
if (self.presentationStyle != UIModalPresentationNone) {
|
|
|
|
_modalViewController.modalPresentationStyle = self.presentationStyle;
|
|
|
|
}
|
2016-08-23 23:50:23 +00:00
|
|
|
[_delegate presentModalHostView:self withViewController:_modalViewController animated:[self hasAnimationType]];
|
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
|
|
|
|
{
|
2016-09-26 22:49:10 +00:00
|
|
|
return _modalViewController.modalPresentationStyle == UIModalPresentationOverFullScreen;
|
2015-08-13 16:09:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 22:59:11 +00:00
|
|
|
- (BOOL)hasAnimationType
|
|
|
|
{
|
|
|
|
return ![self.animationType isEqualToString:@"none"];
|
|
|
|
}
|
|
|
|
|
2015-08-13 16:09:10 +00:00
|
|
|
- (void)setTransparent:(BOOL)transparent
|
|
|
|
{
|
2017-06-21 02:02:27 +00:00
|
|
|
if (self.isTransparent != transparent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-26 22:49:10 +00:00
|
|
|
_modalViewController.modalPresentationStyle = transparent ? UIModalPresentationOverFullScreen : UIModalPresentationFullScreen;
|
2015-08-13 16:09:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 13:19:45 +00:00
|
|
|
#if !TARGET_OS_TV
|
2016-09-07 13:06:12 +00:00
|
|
|
- (UIInterfaceOrientationMask)supportedOrientationsMask
|
|
|
|
{
|
|
|
|
if (_supportedOrientations.count == 0) {
|
|
|
|
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
|
|
|
|
return UIInterfaceOrientationMaskAll;
|
|
|
|
} else {
|
|
|
|
return UIInterfaceOrientationMaskPortrait;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UIInterfaceOrientationMask supportedOrientations = 0;
|
|
|
|
for (NSString *orientation in _supportedOrientations) {
|
|
|
|
if ([orientation isEqualToString:@"portrait"]) {
|
|
|
|
supportedOrientations |= UIInterfaceOrientationMaskPortrait;
|
|
|
|
} else if ([orientation isEqualToString:@"portrait-upside-down"]) {
|
|
|
|
supportedOrientations |= UIInterfaceOrientationMaskPortraitUpsideDown;
|
|
|
|
} else if ([orientation isEqualToString:@"landscape"]) {
|
|
|
|
supportedOrientations |= UIInterfaceOrientationMaskLandscape;
|
|
|
|
} else if ([orientation isEqualToString:@"landscape-left"]) {
|
|
|
|
supportedOrientations |= UIInterfaceOrientationMaskLandscapeLeft;
|
|
|
|
} else if ([orientation isEqualToString:@"landscape-right"]) {
|
|
|
|
supportedOrientations |= UIInterfaceOrientationMaskLandscapeRight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return supportedOrientations;
|
|
|
|
}
|
2016-09-27 13:19:45 +00:00
|
|
|
#endif
|
2016-09-07 13:06:12 +00:00
|
|
|
|
2015-07-28 14:31:26 +00:00
|
|
|
@end
|