RCTModalHostView.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. {
  21. __weak RCTBridge *_bridge;
  22. BOOL _isPresented;
  23. RCTModalHostViewController *_modalViewController;
  24. RCTTouchHandler *_touchHandler;
  25. UIView *_reactSubview;
  26. #if TARGET_OS_TV
  27. UITapGestureRecognizer *_menuButtonGestureRecognizer;
  28. #else
  29. UIInterfaceOrientation _lastKnownOrientation;
  30. #endif
  31. }
  32. RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
  33. RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:coder)
  34. - (instancetype)initWithBridge:(RCTBridge *)bridge
  35. {
  36. if ((self = [super initWithFrame:CGRectZero])) {
  37. _bridge = bridge;
  38. _modalViewController = [RCTModalHostViewController new];
  39. UIView *containerView = [UIView new];
  40. containerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  41. _modalViewController.view = containerView;
  42. _touchHandler = [[RCTTouchHandler alloc] initWithBridge:bridge];
  43. #if TARGET_OS_TV
  44. _menuButtonGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self 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 || currentOrientation == UIInterfaceOrientationPortraitUpsideDown;
  94. NSDictionary *eventPayload =
  95. @{
  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. subview.autoresizingMask = UIViewAutoresizingFlexibleHeight |
  117. UIViewAutoresizingFlexibleWidth;
  118. [_modalViewController.view insertSubview:subview atIndex:0];
  119. _reactSubview = subview;
  120. }
  121. - (void)removeReactSubview:(UIView *)subview
  122. {
  123. RCTAssert(subview == _reactSubview, @"Cannot remove view other than modal view");
  124. // Superclass (category) removes the `subview` from actual `superview`.
  125. [super removeReactSubview:subview];
  126. [_touchHandler detachFromView:subview];
  127. #if TARGET_OS_TV
  128. if (_menuButtonGestureRecognizer) {
  129. [subview removeGestureRecognizer:_menuButtonGestureRecognizer];
  130. }
  131. for (UIGestureRecognizer *gr in self.tvRemoteHandler.tvRemoteGestureRecognizers) {
  132. [subview removeGestureRecognizer:gr];
  133. }
  134. #endif
  135. _reactSubview = nil;
  136. }
  137. - (void)didUpdateReactSubviews
  138. {
  139. // Do nothing, as subview (singular) is managed by `insertReactSubview:atIndex:`
  140. }
  141. - (void)dismissModalViewController
  142. {
  143. if (_isPresented) {
  144. [_delegate dismissModalHostView:self withViewController:_modalViewController animated:[self hasAnimationType]];
  145. _isPresented = NO;
  146. }
  147. }
  148. - (void)didMoveToWindow
  149. {
  150. [super didMoveToWindow];
  151. // In the case where there is a LayoutAnimation, we will be reinserted into the view hierarchy but only for aesthetic purposes.
  152. // In such a case, we should NOT represent the <Modal>.
  153. if (!self.userInteractionEnabled && ![self.superview.reactSubviews containsObject:self]) {
  154. return;
  155. }
  156. if (!_isPresented && self.window) {
  157. RCTAssert(self.reactViewController, @"Can't present modal view controller without a presenting view controller");
  158. #if !TARGET_OS_TV
  159. _modalViewController.supportedInterfaceOrientations = [self supportedOrientationsMask];
  160. #endif
  161. if ([self.animationType isEqualToString:@"fade"]) {
  162. _modalViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  163. } else if ([self.animationType isEqualToString:@"slide"]) {
  164. _modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  165. }
  166. if (self.presentationStyle != UIModalPresentationNone) {
  167. _modalViewController.modalPresentationStyle = self.presentationStyle;
  168. }
  169. [_delegate presentModalHostView:self withViewController:_modalViewController animated:[self hasAnimationType]];
  170. _isPresented = YES;
  171. }
  172. }
  173. - (void)didMoveToSuperview
  174. {
  175. [super didMoveToSuperview];
  176. if (_isPresented && !self.superview) {
  177. [self dismissModalViewController];
  178. }
  179. }
  180. - (void)invalidate
  181. {
  182. dispatch_async(dispatch_get_main_queue(), ^{
  183. [self dismissModalViewController];
  184. });
  185. }
  186. - (BOOL)isTransparent
  187. {
  188. return _modalViewController.modalPresentationStyle == UIModalPresentationOverFullScreen;
  189. }
  190. - (BOOL)hasAnimationType
  191. {
  192. return ![self.animationType isEqualToString:@"none"];
  193. }
  194. - (void)setTransparent:(BOOL)transparent
  195. {
  196. if (self.isTransparent != transparent) {
  197. return;
  198. }
  199. _modalViewController.modalPresentationStyle = transparent ? UIModalPresentationOverFullScreen : UIModalPresentationFullScreen;
  200. }
  201. #if !TARGET_OS_TV
  202. - (UIInterfaceOrientationMask)supportedOrientationsMask
  203. {
  204. if (_supportedOrientations.count == 0) {
  205. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
  206. return UIInterfaceOrientationMaskAll;
  207. } else {
  208. return UIInterfaceOrientationMaskPortrait;
  209. }
  210. }
  211. UIInterfaceOrientationMask supportedOrientations = 0;
  212. for (NSString *orientation in _supportedOrientations) {
  213. if ([orientation isEqualToString:@"portrait"]) {
  214. supportedOrientations |= UIInterfaceOrientationMaskPortrait;
  215. } else if ([orientation isEqualToString:@"portrait-upside-down"]) {
  216. supportedOrientations |= UIInterfaceOrientationMaskPortraitUpsideDown;
  217. } else if ([orientation isEqualToString:@"landscape"]) {
  218. supportedOrientations |= UIInterfaceOrientationMaskLandscape;
  219. } else if ([orientation isEqualToString:@"landscape-left"]) {
  220. supportedOrientations |= UIInterfaceOrientationMaskLandscapeLeft;
  221. } else if ([orientation isEqualToString:@"landscape-right"]) {
  222. supportedOrientations |= UIInterfaceOrientationMaskLandscapeRight;
  223. }
  224. }
  225. return supportedOrientations;
  226. }
  227. #endif
  228. @end