react-native-navigation的迁移库

RNNRootViewController.m 7.5KB

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