react-native-navigation的迁移库

RNNRootViewController.m 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 mergeOptions: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. if (self.resolveOptions.statusBar.visible.hasValue) {
  92. return ![self.resolveOptions.statusBar.visible get];
  93. } else if ([self.resolveOptions.statusBar.hideWithTopBar getWithDefaultValue:NO]) {
  94. return self.navigationController.isNavigationBarHidden;
  95. }
  96. return NO;
  97. }
  98. - (UIStatusBarStyle)preferredStatusBarStyle {
  99. if ([[self.resolveOptions.statusBar.style getWithDefaultValue:@"default"] isEqualToString:@"light"]) {
  100. return UIStatusBarStyleLightContent;
  101. } else {
  102. return UIStatusBarStyleDefault;
  103. }
  104. }
  105. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  106. return self.resolveOptions.layout.supportedOrientations;
  107. }
  108. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  109. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  110. if (![[vc.self.resolveOptions.topBar.backButton.transition getWithDefaultValue:@""] isEqualToString:@"custom"]){
  111. navigationController.delegate = nil;
  112. }
  113. }
  114. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  115. animationControllerForOperation:(UINavigationControllerOperation)operation
  116. fromViewController:(UIViewController*)fromVC
  117. toViewController:(UIViewController*)toVC {
  118. if (self.animator) {
  119. return self.animator;
  120. } else if (operation == UINavigationControllerOperationPush && self.resolveOptions.animations.push.hasCustomAnimation) {
  121. return [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:self.resolveOptions.animations.push isDismiss:NO];
  122. } else if (operation == UINavigationControllerOperationPop && self.resolveOptions.animations.pop.hasCustomAnimation) {
  123. return [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:self.resolveOptions.animations.pop isDismiss:YES];
  124. } else {
  125. return nil;
  126. }
  127. return nil;
  128. }
  129. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  130. return self.previewController;
  131. }
  132. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  133. if (self.previewCallback) {
  134. self.previewCallback(self);
  135. }
  136. }
  137. - (void)onActionPress:(NSString *)id {
  138. [_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
  139. }
  140. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  141. NSString *actionId = action[@"id"];
  142. NSString *actionTitle = action[@"title"];
  143. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  144. if ([action[@"style"] isEqualToString:@"selected"]) {
  145. actionStyle = UIPreviewActionStyleSelected;
  146. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  147. actionStyle = UIPreviewActionStyleDestructive;
  148. }
  149. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  150. [self onActionPress:actionId];
  151. }];
  152. }
  153. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  154. NSMutableArray *actions = [[NSMutableArray alloc] init];
  155. for (NSDictionary *previewAction in self.resolveOptions.preview.actions) {
  156. UIPreviewAction *action = [self convertAction:previewAction];
  157. NSDictionary *actionActions = previewAction[@"actions"];
  158. if (actionActions.count > 0) {
  159. NSMutableArray *group = [[NSMutableArray alloc] init];
  160. for (NSDictionary *previewGroupAction in actionActions) {
  161. [group addObject:[self convertAction:previewGroupAction]];
  162. }
  163. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  164. [actions addObject:actionGroup];
  165. } else {
  166. [actions addObject:action];
  167. }
  168. }
  169. return actions;
  170. }
  171. -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
  172. [self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
  173. }
  174. @end