react-native-navigation的迁移库

RNNRootViewController.m 7.9KB

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