react-native-navigation的迁移库

RNNComponentViewController.m 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. _defaultOptions = defaultOptions;
  23. [_presenter setDefaultOptions:defaultOptions];
  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. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  71. [self.eventEmitter sendOnSearchBarUpdated:self.layoutInfo.componentId
  72. text:searchController.searchBar.text
  73. isFocused:searchController.searchBar.isFirstResponder];
  74. }
  75. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  76. [self.eventEmitter sendOnSearchBarCancelPressed:self.layoutInfo.componentId];
  77. }
  78. -(BOOL)isCustomTransitioned {
  79. return self.resolveOptions.customTransition.animations != nil;
  80. }
  81. - (BOOL)isExternalViewController {
  82. return !self.creator;
  83. }
  84. - (BOOL)prefersStatusBarHidden {
  85. return [_presenter isStatusBarVisibility:self.navigationController resolvedOptions:self.resolveOptions];
  86. }
  87. - (UIStatusBarStyle)preferredStatusBarStyle {
  88. return [_presenter getStatusBarStyle:[self resolveOptions]];
  89. }
  90. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  91. RNNComponentViewController* vc = (RNNComponentViewController*)viewController;
  92. if (![[vc.self.resolveOptions.topBar.backButton.transition getWithDefaultValue:@""] isEqualToString:@"custom"]){
  93. navigationController.delegate = nil;
  94. }
  95. }
  96. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  97. animationControllerForOperation:(UINavigationControllerOperation)operation
  98. fromViewController:(UIViewController*)fromVC
  99. toViewController:(UIViewController*)toVC {
  100. if (self.animator) {
  101. return self.animator;
  102. } else if (operation == UINavigationControllerOperationPush && self.resolveOptions.animations.push.hasCustomAnimation) {
  103. return [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:self.resolveOptions.animations.push isDismiss:NO];
  104. } else if (operation == UINavigationControllerOperationPop && self.resolveOptions.animations.pop.hasCustomAnimation) {
  105. return [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:self.resolveOptions.animations.pop isDismiss:YES];
  106. } else {
  107. return nil;
  108. }
  109. return nil;
  110. }
  111. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  112. return self.previewController;
  113. }
  114. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  115. if (self.previewCallback) {
  116. self.previewCallback(self);
  117. }
  118. }
  119. - (void)onActionPress:(NSString *)id {
  120. [_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
  121. }
  122. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  123. NSString *actionId = action[@"id"];
  124. NSString *actionTitle = action[@"title"];
  125. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  126. if ([action[@"style"] isEqualToString:@"selected"]) {
  127. actionStyle = UIPreviewActionStyleSelected;
  128. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  129. actionStyle = UIPreviewActionStyleDestructive;
  130. }
  131. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  132. [self onActionPress:actionId];
  133. }];
  134. }
  135. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  136. NSMutableArray *actions = [[NSMutableArray alloc] init];
  137. for (NSDictionary *previewAction in self.resolveOptions.preview.actions) {
  138. UIPreviewAction *action = [self convertAction:previewAction];
  139. NSDictionary *actionActions = previewAction[@"actions"];
  140. if (actionActions.count > 0) {
  141. NSMutableArray *group = [[NSMutableArray alloc] init];
  142. for (NSDictionary *previewGroupAction in actionActions) {
  143. [group addObject:[self convertAction:previewGroupAction]];
  144. }
  145. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  146. [actions addObject:actionGroup];
  147. } else {
  148. [actions addObject:action];
  149. }
  150. }
  151. return actions;
  152. }
  153. -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
  154. [self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
  155. }
  156. @end