react-native-navigation的迁移库

RNNRootViewController.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #import "RNNRootViewController.h"
  2. #import <React/RCTConvert.h>
  3. #import "RNNAnimator.h"
  4. #import "RNNCustomTitleView.h"
  5. #import "RNNPushAnimation.h"
  6. #import "RNNReactView.h"
  7. @interface RNNRootViewController() {
  8. RNNReactView* _customTitleView;
  9. UIView* _customTopBar;
  10. UIView* _customTopBarBackground;
  11. BOOL _isBeingPresented;
  12. }
  13. @property (nonatomic) BOOL _statusBarHidden;
  14. @property (nonatomic) BOOL isExternalComponent;
  15. @property (nonatomic) BOOL _optionsApplied;
  16. @property (nonatomic, copy) void (^rotationBlock)(void);
  17. @property (nonatomic, copy) RNNReactViewReadyCompletionBlock reactViewReadyBlock;
  18. @end
  19. @implementation RNNRootViewController
  20. @synthesize previewCallback;
  21. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
  22. rootViewCreator:(id<RNNRootViewCreator>)creator
  23. eventEmitter:(RNNEventEmitter*)eventEmitter
  24. isExternalComponent:(BOOL)isExternalComponent {
  25. self = [super init];
  26. self.eventEmitter = eventEmitter;
  27. self.creator = creator;
  28. self.isExternalComponent = isExternalComponent;
  29. self.layoutInfo = layoutInfo;
  30. self.animator = [[RNNAnimator alloc] initWithTransitionOptions:self.layoutInfo.options.customTransition];
  31. if (!self.isExternalComponent) {
  32. self.view = [creator createRootView:self.layoutInfo.name rootViewId:self.layoutInfo.componentId];
  33. [[NSNotificationCenter defaultCenter] addObserver:self
  34. selector:@selector(reactViewReady)
  35. name: @"RCTContentDidAppearNotification"
  36. object:nil];
  37. }
  38. [[NSNotificationCenter defaultCenter] addObserver:self
  39. selector:@selector(onJsReload)
  40. name:RCTJavaScriptWillStartLoadingNotification
  41. object:nil];
  42. self.navigationController.delegate = self;
  43. [[NSNotificationCenter defaultCenter] addObserver:self
  44. selector:@selector(orientationDidChange:)
  45. name:UIDeviceOrientationDidChangeNotification
  46. object:nil];
  47. return self;
  48. }
  49. -(void)viewWillAppear:(BOOL)animated{
  50. [super viewWillAppear:animated];
  51. _isBeingPresented = YES;
  52. [self.layoutInfo.options applyOn:self];
  53. }
  54. -(void)viewDidAppear:(BOOL)animated {
  55. [super viewDidAppear:animated];
  56. [self.eventEmitter sendComponentDidAppear:self.layoutInfo.componentId componentName:self.layoutInfo.name];
  57. }
  58. - (void)viewWillDisappear:(BOOL)animated {
  59. [super viewWillDisappear:animated];
  60. _isBeingPresented = NO;
  61. }
  62. -(void)viewDidDisappear:(BOOL)animated {
  63. [super viewDidDisappear:animated];
  64. [self.eventEmitter sendComponentDidDisappear:self.layoutInfo.componentId componentName:self.layoutInfo.name];
  65. }
  66. - (void)reactViewReady {
  67. if (_reactViewReadyBlock) {
  68. _reactViewReadyBlock();
  69. _reactViewReadyBlock = nil;
  70. }
  71. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RCTContentDidAppearNotification" object:nil];
  72. }
  73. - (void)waitForReactViewRender:(BOOL)wait perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  74. if (wait && !_isExternalComponent) {
  75. [self onReactViewReady:readyBlock];
  76. } else {
  77. readyBlock();
  78. }
  79. }
  80. - (UIViewController *)getLeafViewController {
  81. return self;
  82. }
  83. - (void)onReactViewReady:(RNNReactViewReadyCompletionBlock)readyBlock {
  84. if (self.isCustomViewController) {
  85. readyBlock();
  86. } else {
  87. self.reactViewReadyBlock = readyBlock;
  88. }
  89. }
  90. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  91. [self.eventEmitter sendOnSearchBarUpdated:self.layoutInfo.componentId
  92. text:searchController.searchBar.text
  93. isFocused:searchController.searchBar.isFirstResponder];
  94. }
  95. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  96. [self.eventEmitter sendOnSearchBarCancelPressed:self.layoutInfo.componentId];
  97. }
  98. - (void)viewDidLoad {
  99. [super viewDidLoad];
  100. }
  101. - (void)optionsUpdated {
  102. [self setCustomNavigationTitleView];
  103. [self setCustomNavigationBarView];
  104. [self setCustomNavigationComponentBackground];
  105. }
  106. - (void)applyModalOptions {
  107. [self.layoutInfo.options applyOn:self];
  108. [self.layoutInfo.options applyModalOptions:self];
  109. }
  110. - (void)setCustomNavigationTitleView {
  111. if (!_customTitleView && _isBeingPresented) {
  112. if (self.layoutInfo.options.topBar.title.component.name) {
  113. _customTitleView = (RNNReactView*)[_creator createRootViewFromComponentOptions:self.layoutInfo.options.topBar.title.component];
  114. _customTitleView.backgroundColor = UIColor.clearColor;
  115. [_customTitleView setAlignment:self.layoutInfo.options.topBar.title.component.alignment];
  116. BOOL isCenter = [self.layoutInfo.options.topBar.title.component.alignment isEqualToString:@"center"];
  117. __weak RNNReactView *weakTitleView = _customTitleView;
  118. CGRect frame = self.navigationController.navigationBar.bounds;
  119. [_customTitleView setFrame:frame];
  120. [_customTitleView setRootViewDidChangeIntrinsicSize:^(CGSize intrinsicContentSize) {
  121. if (isCenter) {
  122. [weakTitleView setFrame:CGRectMake(0, 0, intrinsicContentSize.width, intrinsicContentSize.height)];
  123. } else {
  124. [weakTitleView setFrame:frame];
  125. }
  126. }];
  127. self.navigationItem.titleView = _customTitleView;
  128. }
  129. } else if (_customTitleView && _customTitleView.superview == nil) {
  130. if ([self.navigationItem.title isKindOfClass:[RNNCustomTitleView class]] && !_customTitleView) {
  131. self.navigationItem.title = nil;
  132. }
  133. self.navigationItem.titleView = _customTitleView;
  134. }
  135. }
  136. - (void)setCustomNavigationBarView {
  137. if (!_customTopBar) {
  138. if (self.layoutInfo.options.topBar.component.name) {
  139. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.layoutInfo.options.topBar.component];
  140. _customTopBar = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  141. reactView.backgroundColor = UIColor.clearColor;
  142. _customTopBar.backgroundColor = UIColor.clearColor;
  143. [self.navigationController.navigationBar addSubview:_customTopBar];
  144. } else if ([[self.navigationController.navigationBar.subviews lastObject] isKindOfClass:[RNNCustomTitleView class]] && !_customTopBar) {
  145. [[self.navigationController.navigationBar.subviews lastObject] removeFromSuperview];
  146. }
  147. } else if (_customTopBar && _customTopBar.superview == nil) {
  148. if ([[self.navigationController.navigationBar.subviews lastObject] isKindOfClass:[RNNCustomTitleView class]] && !_customTopBar) {
  149. [[self.navigationController.navigationBar.subviews lastObject] removeFromSuperview];
  150. }
  151. [self.navigationController.navigationBar addSubview:_customTopBar];
  152. }
  153. }
  154. - (void)setCustomNavigationComponentBackground {
  155. if (!_customTopBarBackground) {
  156. if (self.layoutInfo.options.topBar.background.component.name) {
  157. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.layoutInfo.options.topBar.background.component];
  158. _customTopBarBackground = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  159. [self.navigationController.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  160. } else if (self.navigationController.navigationBar.subviews.count && [[self.navigationController.navigationBar.subviews objectAtIndex:1] isKindOfClass:[RNNCustomTitleView class]]) {
  161. [[self.navigationController.navigationBar.subviews objectAtIndex:1] removeFromSuperview];
  162. }
  163. if (self.layoutInfo.options.topBar.background.clipToBounds) {
  164. self.navigationController.navigationBar.clipsToBounds = YES;
  165. } else {
  166. self.navigationController.navigationBar.clipsToBounds = NO;
  167. }
  168. } if (_customTopBarBackground && _customTopBarBackground.superview == nil) {
  169. if (self.navigationController.navigationBar.subviews.count && [[self.navigationController.navigationBar.subviews objectAtIndex:1] isKindOfClass:[RNNCustomTitleView class]]) {
  170. [[self.navigationController.navigationBar.subviews objectAtIndex:1] removeFromSuperview];
  171. }
  172. [self.navigationController.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  173. self.navigationController.navigationBar.clipsToBounds = YES;
  174. }
  175. }
  176. -(BOOL)isCustomTransitioned {
  177. return self.layoutInfo.options.customTransition.animations != nil;
  178. }
  179. - (BOOL)isCustomViewController {
  180. return self.isExternalComponent;
  181. }
  182. - (BOOL)prefersStatusBarHidden {
  183. if (self.layoutInfo.options.statusBar.visible) {
  184. return ![self.layoutInfo.options.statusBar.visible boolValue];
  185. } else if ([self.layoutInfo.options.statusBar.hideWithTopBar boolValue]) {
  186. return self.navigationController.isNavigationBarHidden;
  187. }
  188. return NO;
  189. }
  190. - (UIStatusBarStyle)preferredStatusBarStyle {
  191. if (self.layoutInfo.options.statusBar.style && [self.layoutInfo.options.statusBar.style isEqualToString:@"light"]) {
  192. return UIStatusBarStyleLightContent;
  193. } else {
  194. return UIStatusBarStyleDefault;
  195. }
  196. }
  197. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  198. return self.layoutInfo.options.layout.supportedOrientations;
  199. }
  200. - (BOOL)hidesBottomBarWhenPushed
  201. {
  202. if (self.layoutInfo.options.bottomTabs && self.layoutInfo.options.bottomTabs.visible) {
  203. return ![self.layoutInfo.options.bottomTabs.visible boolValue];
  204. }
  205. return NO;
  206. }
  207. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  208. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  209. if (![vc.layoutInfo.options.topBar.backButton.transition isEqualToString:@"custom"]){
  210. navigationController.delegate = nil;
  211. }
  212. }
  213. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  214. animationControllerForOperation:(UINavigationControllerOperation)operation
  215. fromViewController:(UIViewController*)fromVC
  216. toViewController:(UIViewController*)toVC {
  217. {
  218. if (self.animator) {
  219. return self.animator;
  220. } else if (operation == UINavigationControllerOperationPush && self.layoutInfo.options.animations.push.hasCustomAnimation) {
  221. return [[RNNPushAnimation alloc] initWithScreenTransition:self.layoutInfo.options.animations.push];
  222. } else if (operation == UINavigationControllerOperationPop && self.layoutInfo.options.animations.pop.hasCustomAnimation) {
  223. return [[RNNPushAnimation alloc] initWithScreenTransition:self.layoutInfo.options.animations.pop];
  224. } else {
  225. return nil;
  226. }
  227. }
  228. return nil;
  229. }
  230. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
  231. return [[RNNModalAnimation alloc] initWithScreenTransition:self.layoutInfo.options.animations.showModal isDismiss:NO];
  232. }
  233. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  234. return [[RNNModalAnimation alloc] initWithScreenTransition:self.layoutInfo.options.animations.dismissModal isDismiss:YES];
  235. }
  236. - (void)performOnRotation:(void (^)(void))block {
  237. _rotationBlock = block;
  238. }
  239. - (void)orientationDidChange:(NSNotification*)notification {
  240. if (_rotationBlock) {
  241. _rotationBlock();
  242. }
  243. }
  244. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  245. return self.previewController;
  246. }
  247. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  248. if (self.previewCallback) {
  249. self.previewCallback(self);
  250. }
  251. }
  252. - (void)onActionPress:(NSString *)id {
  253. [_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
  254. }
  255. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  256. NSString *actionId = action[@"id"];
  257. NSString *actionTitle = action[@"title"];
  258. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  259. if ([action[@"style"] isEqualToString:@"selected"]) {
  260. actionStyle = UIPreviewActionStyleSelected;
  261. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  262. actionStyle = UIPreviewActionStyleDestructive;
  263. }
  264. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  265. [self onActionPress:actionId];
  266. }];
  267. }
  268. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  269. NSMutableArray *actions = [[NSMutableArray alloc] init];
  270. for (NSDictionary *previewAction in self.layoutInfo.options.preview.actions) {
  271. UIPreviewAction *action = [self convertAction:previewAction];
  272. NSDictionary *actionActions = previewAction[@"actions"];
  273. if (actionActions.count > 0) {
  274. NSMutableArray *group = [[NSMutableArray alloc] init];
  275. for (NSDictionary *previewGroupAction in actionActions) {
  276. [group addObject:[self convertAction:previewGroupAction]];
  277. }
  278. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  279. [actions addObject:actionGroup];
  280. } else {
  281. [actions addObject:action];
  282. }
  283. }
  284. return actions;
  285. }
  286. -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
  287. [self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
  288. }
  289. /**
  290. * fix for #877, #878
  291. */
  292. -(void)onJsReload {
  293. [self cleanReactLeftovers];
  294. }
  295. /**
  296. * fix for #880
  297. */
  298. -(void)dealloc {
  299. [self cleanReactLeftovers];
  300. }
  301. -(void)cleanReactLeftovers {
  302. [[NSNotificationCenter defaultCenter] removeObserver:self];
  303. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  304. self.view = nil;
  305. self.navigationItem.titleView = nil;
  306. self.navigationItem.rightBarButtonItems = nil;
  307. self.navigationItem.leftBarButtonItems = nil;
  308. _customTopBar = nil;
  309. _customTitleView = nil;
  310. _customTopBarBackground = nil;
  311. }
  312. @end