RCTModalHostView.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RCTModalHostView.h"
  8. #import <UIKit/UIKit.h>
  9. #import "RCTAssert.h"
  10. #import "RCTBridge.h"
  11. #import "RCTModalHostViewController.h"
  12. #import "RCTTouchHandler.h"
  13. #import "RCTUIManager.h"
  14. #import "RCTUtils.h"
  15. #import "UIView+React.h"
  16. #if TARGET_OS_TV
  17. #import "RCTTVRemoteHandler.h"
  18. #endif
  19. @implementation RCTModalHostView {
  20. __weak RCTBridge *_bridge;
  21. BOOL _isPresented;
  22. RCTModalHostViewController *_modalViewController;
  23. RCTTouchHandler *_touchHandler;
  24. UIView *_reactSubview;
  25. #if TARGET_OS_TV
  26. UITapGestureRecognizer *_menuButtonGestureRecognizer;
  27. #else
  28. UIInterfaceOrientation _lastKnownOrientation;
  29. #endif
  30. }
  31. RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame)
  32. RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : coder)
  33. - (instancetype)initWithBridge:(RCTBridge *)bridge
  34. {
  35. if ((self = [super initWithFrame:CGRectZero])) {
  36. _bridge = bridge;
  37. _modalViewController = [RCTModalHostViewController new];
  38. UIView *containerView = [UIView new];
  39. containerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  40. _modalViewController.view = containerView;
  41. _touchHandler = [[RCTTouchHandler alloc] initWithBridge:bridge];
  42. #if TARGET_OS_TV
  43. _menuButtonGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
  44. action:@selector(menuButtonPressed:)];
  45. _menuButtonGestureRecognizer.allowedPressTypes = @[ @(UIPressTypeMenu) ];
  46. self.tvRemoteHandler = [RCTTVRemoteHandler new];
  47. #endif
  48. _isPresented = NO;
  49. __weak typeof(self) weakSelf = self;
  50. _modalViewController.boundsDidChangeBlock = ^(CGRect newBounds) {
  51. [weakSelf notifyForBoundsChange:newBounds];
  52. };
  53. }
  54. return self;
  55. }
  56. #if TARGET_OS_TV
  57. - (void)menuButtonPressed:(__unused UIGestureRecognizer *)gestureRecognizer
  58. {
  59. if (_onRequestClose) {
  60. _onRequestClose(nil);
  61. }
  62. }
  63. - (void)setOnRequestClose:(RCTDirectEventBlock)onRequestClose
  64. {
  65. _onRequestClose = onRequestClose;
  66. if (_reactSubview) {
  67. if (_onRequestClose && _menuButtonGestureRecognizer) {
  68. [_reactSubview addGestureRecognizer:_menuButtonGestureRecognizer];
  69. } else {
  70. [_reactSubview removeGestureRecognizer:_menuButtonGestureRecognizer];
  71. }
  72. }
  73. }
  74. #endif
  75. - (void)notifyForBoundsChange:(CGRect)newBounds
  76. {
  77. if (_reactSubview && _isPresented) {
  78. [_bridge.uiManager setSize:newBounds.size forView:_reactSubview];
  79. [self notifyForOrientationChange];
  80. }
  81. }
  82. - (void)notifyForOrientationChange
  83. {
  84. #if !TARGET_OS_TV
  85. if (!_onOrientationChange) {
  86. return;
  87. }
  88. UIInterfaceOrientation currentOrientation = [RCTSharedApplication() statusBarOrientation];
  89. if (currentOrientation == _lastKnownOrientation) {
  90. return;
  91. }
  92. _lastKnownOrientation = currentOrientation;
  93. BOOL isPortrait = currentOrientation == UIInterfaceOrientationPortrait ||
  94. currentOrientation == UIInterfaceOrientationPortraitUpsideDown;
  95. NSDictionary *eventPayload = @{
  96. @"orientation" : isPortrait ? @"portrait" : @"landscape",
  97. };
  98. _onOrientationChange(eventPayload);
  99. #endif
  100. }
  101. - (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex
  102. {
  103. RCTAssert(_reactSubview == nil, @"Modal view can only have one subview");
  104. [super insertReactSubview:subview atIndex:atIndex];
  105. [_touchHandler attachToView:subview];
  106. #if TARGET_OS_TV
  107. for (NSString *key in [self.tvRemoteHandler.tvRemoteGestureRecognizers allKeys]) {
  108. if (![key isEqualToString:RCTTVRemoteEventMenu]) {
  109. [subview addGestureRecognizer:self.tvRemoteHandler.tvRemoteGestureRecognizers[key]];
  110. }
  111. }
  112. if (_onRequestClose) {
  113. [subview addGestureRecognizer:_menuButtonGestureRecognizer];
  114. }
  115. #endif
  116. [_modalViewController.view insertSubview:subview atIndex:0];
  117. _reactSubview = subview;
  118. }
  119. - (void)removeReactSubview:(UIView *)subview
  120. {
  121. RCTAssert(subview == _reactSubview, @"Cannot remove view other than modal view");
  122. // Superclass (category) removes the `subview` from actual `superview`.
  123. [super removeReactSubview:subview];
  124. [_touchHandler detachFromView:subview];
  125. #if TARGET_OS_TV
  126. if (_menuButtonGestureRecognizer) {
  127. [subview removeGestureRecognizer:_menuButtonGestureRecognizer];
  128. }
  129. for (UIGestureRecognizer *gr in self.tvRemoteHandler.tvRemoteGestureRecognizers) {
  130. [subview removeGestureRecognizer:gr];
  131. }
  132. #endif
  133. _reactSubview = nil;
  134. }
  135. - (void)didUpdateReactSubviews
  136. {
  137. // Do nothing, as subview (singular) is managed by `insertReactSubview:atIndex:`
  138. }
  139. - (void)dismissModalViewController
  140. {
  141. if (_isPresented) {
  142. [_delegate dismissModalHostView:self withViewController:_modalViewController animated:[self hasAnimationType]];
  143. _isPresented = NO;
  144. }
  145. }
  146. - (void)didMoveToWindow
  147. {
  148. [super didMoveToWindow];
  149. // In the case where there is a LayoutAnimation, we will be reinserted into the view hierarchy but only for aesthetic
  150. // purposes. In such a case, we should NOT represent the <Modal>.
  151. if (!self.userInteractionEnabled && ![self.superview.reactSubviews containsObject:self]) {
  152. return;
  153. }
  154. if (!_isPresented && self.window) {
  155. RCTAssert(self.reactViewController, @"Can't present modal view controller without a presenting view controller");
  156. #if !TARGET_OS_TV
  157. _modalViewController.supportedInterfaceOrientations = [self supportedOrientationsMask];
  158. #endif
  159. if ([self.animationType isEqualToString:@"fade"]) {
  160. _modalViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  161. } else if ([self.animationType isEqualToString:@"slide"]) {
  162. _modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  163. }
  164. if (self.presentationStyle != UIModalPresentationNone) {
  165. _modalViewController.modalPresentationStyle = self.presentationStyle;
  166. }
  167. [_delegate presentModalHostView:self withViewController:_modalViewController animated:[self hasAnimationType]];
  168. _isPresented = YES;
  169. }
  170. }
  171. - (void)didMoveToSuperview
  172. {
  173. [super didMoveToSuperview];
  174. if (_isPresented && !self.superview) {
  175. [self dismissModalViewController];
  176. }
  177. }
  178. - (void)invalidate
  179. {
  180. dispatch_async(dispatch_get_main_queue(), ^{
  181. [self dismissModalViewController];
  182. });
  183. }
  184. - (BOOL)isTransparent
  185. {
  186. return _modalViewController.modalPresentationStyle == UIModalPresentationOverFullScreen;
  187. }
  188. - (BOOL)hasAnimationType
  189. {
  190. return ![self.animationType isEqualToString:@"none"];
  191. }
  192. - (void)setTransparent:(BOOL)transparent
  193. {
  194. if (self.isTransparent != transparent) {
  195. return;
  196. }
  197. _modalViewController.modalPresentationStyle =
  198. transparent ? UIModalPresentationOverFullScreen : UIModalPresentationFullScreen;
  199. }
  200. #if !TARGET_OS_TV
  201. - (UIInterfaceOrientationMask)supportedOrientationsMask
  202. {
  203. if (_supportedOrientations.count == 0) {
  204. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
  205. return UIInterfaceOrientationMaskAll;
  206. } else {
  207. return UIInterfaceOrientationMaskPortrait;
  208. }
  209. }
  210. UIInterfaceOrientationMask supportedOrientations = 0;
  211. for (NSString *orientation in _supportedOrientations) {
  212. if ([orientation isEqualToString:@"portrait"]) {
  213. supportedOrientations |= UIInterfaceOrientationMaskPortrait;
  214. } else if ([orientation isEqualToString:@"portrait-upside-down"]) {
  215. supportedOrientations |= UIInterfaceOrientationMaskPortraitUpsideDown;
  216. } else if ([orientation isEqualToString:@"landscape"]) {
  217. supportedOrientations |= UIInterfaceOrientationMaskLandscape;
  218. } else if ([orientation isEqualToString:@"landscape-left"]) {
  219. supportedOrientations |= UIInterfaceOrientationMaskLandscapeLeft;
  220. } else if ([orientation isEqualToString:@"landscape-right"]) {
  221. supportedOrientations |= UIInterfaceOrientationMaskLandscapeRight;
  222. }
  223. }
  224. return supportedOrientations;
  225. }
  226. #endif
  227. @end