react-native-navigation的迁移库

RNNComponentViewController.m 7.2KB

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