react-native-navigation的迁移库

RNNComponentViewController.m 7.3KB

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