react-native-navigation的迁移库

RNNRootViewController.m 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #import "RNNRootViewController.h"
  2. #import "RNNAnimationsTransitionDelegate.h"
  3. #import "UIViewController+LayoutProtocol.h"
  4. @implementation RNNRootViewController
  5. @synthesize previewCallback;
  6. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo rootViewCreator:(id<RNNRootViewCreator>)creator eventEmitter:(RNNEventEmitter *)eventEmitter presenter:(RNNViewControllerPresenter *)presenter options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions {
  7. self = [super initWithLayoutInfo:layoutInfo creator:creator options:options defaultOptions:defaultOptions presenter:presenter eventEmitter:eventEmitter childViewControllers:nil];
  8. self.animator = [[RNNAnimator alloc] initWithTransitionOptions:self.resolveOptions.customTransition];
  9. self.navigationController.delegate = self;
  10. return self;
  11. }
  12. - (instancetype)initExternalComponentWithLayoutInfo:(RNNLayoutInfo *)layoutInfo eventEmitter:(RNNEventEmitter *)eventEmitter presenter:(RNNViewControllerPresenter *)presenter options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions {
  13. self = [self initWithLayoutInfo:layoutInfo rootViewCreator:nil eventEmitter:eventEmitter presenter:presenter options:options defaultOptions:defaultOptions];
  14. return self;
  15. }
  16. - (void)bindViewController:(UIViewController *)viewController {
  17. [self addChildViewController:viewController];
  18. [self.view addSubview:viewController.view];
  19. [viewController didMoveToParentViewController:self];
  20. }
  21. - (void)mergeOptions:(RNNNavigationOptions *)options {
  22. [_presenter mergeOptions:options currentOptions:self.options defaultOptions:self.defaultOptions];
  23. [self.parentViewController mergeOptions:options];
  24. }
  25. - (void)overrideOptions:(RNNNavigationOptions *)options {
  26. [self.options overrideOptions:options];
  27. }
  28. - (void)viewWillAppear:(BOOL)animated{
  29. [super viewWillAppear:animated];
  30. [_presenter applyOptions:self.resolveOptions];
  31. [_presenter renderComponents:self.resolveOptions perform:nil];
  32. [self.parentViewController onChildWillAppear];
  33. }
  34. -(void)viewDidAppear:(BOOL)animated {
  35. [super viewDidAppear:animated];
  36. [self.eventEmitter sendComponentDidAppear:self.layoutInfo.componentId componentName:self.layoutInfo.name];
  37. }
  38. - (void)viewWillDisappear:(BOOL)animated {
  39. [super viewWillDisappear:animated];
  40. }
  41. - (void)viewDidDisappear:(BOOL)animated {
  42. [super viewDidDisappear:animated];
  43. [self.eventEmitter sendComponentDidDisappear:self.layoutInfo.componentId componentName:self.layoutInfo.name];
  44. }
  45. - (void)renderTreeAndWait:(BOOL)wait perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  46. if (self.isExternalViewController) {
  47. if (readyBlock) {
  48. readyBlock();
  49. }
  50. return;
  51. }
  52. __block RNNReactViewReadyCompletionBlock readyBlockCopy = readyBlock;
  53. UIView* reactView = [self.creator createRootView:self.layoutInfo.name rootViewId:self.layoutInfo.componentId availableSize:[UIScreen mainScreen].bounds.size reactViewReadyBlock:^{
  54. [_presenter renderComponents:self.resolveOptions perform:^{
  55. if (readyBlockCopy) {
  56. readyBlockCopy();
  57. readyBlockCopy = nil;
  58. }
  59. }];
  60. }];
  61. self.view = reactView;
  62. if (!wait && readyBlock) {
  63. readyBlockCopy();
  64. readyBlockCopy = nil;
  65. }
  66. }
  67. - (UIViewController *)getCurrentChild {
  68. return nil;
  69. }
  70. - (CGFloat)getTopBarHeight {
  71. return [[self getCurrentChild] getTopBarHeight];
  72. }
  73. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  74. [self.eventEmitter sendOnSearchBarUpdated:self.layoutInfo.componentId
  75. text:searchController.searchBar.text
  76. isFocused:searchController.searchBar.isFirstResponder];
  77. }
  78. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  79. [self.eventEmitter sendOnSearchBarCancelPressed:self.layoutInfo.componentId];
  80. }
  81. -(BOOL)isCustomTransitioned {
  82. return self.resolveOptions.customTransition.animations != nil;
  83. }
  84. - (BOOL)isExternalViewController {
  85. return !self.creator;
  86. }
  87. - (BOOL)prefersStatusBarHidden {
  88. if (self.resolveOptions.statusBar.visible.hasValue) {
  89. return ![self.resolveOptions.statusBar.visible get];
  90. } else if ([self.resolveOptions.statusBar.hideWithTopBar getWithDefaultValue:NO]) {
  91. return self.navigationController.isNavigationBarHidden;
  92. }
  93. return NO;
  94. }
  95. - (UIStatusBarStyle)preferredStatusBarStyle {
  96. if ([[self.resolveOptions.statusBar.style getWithDefaultValue:@"default"] isEqualToString:@"light"]) {
  97. return UIStatusBarStyleLightContent;
  98. } else {
  99. return UIStatusBarStyleDefault;
  100. }
  101. }
  102. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  103. return self.resolveOptions.layout.supportedOrientations;
  104. }
  105. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  106. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  107. if (![[vc.self.resolveOptions.topBar.backButton.transition getWithDefaultValue:@""] isEqualToString:@"custom"]){
  108. navigationController.delegate = nil;
  109. }
  110. }
  111. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  112. animationControllerForOperation:(UINavigationControllerOperation)operation
  113. fromViewController:(UIViewController*)fromVC
  114. toViewController:(UIViewController*)toVC {
  115. if (self.animator) {
  116. return self.animator;
  117. } else if (operation == UINavigationControllerOperationPush && self.resolveOptions.animations.push.hasCustomAnimation) {
  118. return [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:self.resolveOptions.animations.push isDismiss:NO];
  119. } else if (operation == UINavigationControllerOperationPop && self.resolveOptions.animations.pop.hasCustomAnimation) {
  120. return [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:self.resolveOptions.animations.pop isDismiss:YES];
  121. } else {
  122. return nil;
  123. }
  124. return nil;
  125. }
  126. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  127. return self.previewController;
  128. }
  129. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  130. if (self.previewCallback) {
  131. self.previewCallback(self);
  132. }
  133. }
  134. - (void)onActionPress:(NSString *)id {
  135. [_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
  136. }
  137. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  138. NSString *actionId = action[@"id"];
  139. NSString *actionTitle = action[@"title"];
  140. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  141. if ([action[@"style"] isEqualToString:@"selected"]) {
  142. actionStyle = UIPreviewActionStyleSelected;
  143. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  144. actionStyle = UIPreviewActionStyleDestructive;
  145. }
  146. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  147. [self onActionPress:actionId];
  148. }];
  149. }
  150. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  151. NSMutableArray *actions = [[NSMutableArray alloc] init];
  152. for (NSDictionary *previewAction in self.resolveOptions.preview.actions) {
  153. UIPreviewAction *action = [self convertAction:previewAction];
  154. NSDictionary *actionActions = previewAction[@"actions"];
  155. if (actionActions.count > 0) {
  156. NSMutableArray *group = [[NSMutableArray alloc] init];
  157. for (NSDictionary *previewGroupAction in actionActions) {
  158. [group addObject:[self convertAction:previewGroupAction]];
  159. }
  160. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  161. [actions addObject:actionGroup];
  162. } else {
  163. [actions addObject:action];
  164. }
  165. }
  166. return actions;
  167. }
  168. -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
  169. [self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
  170. }
  171. @end