react-native-navigation的迁移库

RNNComponentViewController.m 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. - (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  13. _defaultOptions = defaultOptions;
  14. [_presenter setDefaultOptions:defaultOptions];
  15. }
  16. - (void)overrideOptions:(RNNNavigationOptions *)options {
  17. [self.options overrideOptions:options];
  18. }
  19. - (void)viewWillAppear:(BOOL)animated {
  20. [super viewWillAppear:animated];
  21. [_presenter applyOptions:self.resolveOptions];
  22. [self.parentViewController onChildWillAppear];
  23. }
  24. -(void)viewDidAppear:(BOOL)animated {
  25. [super viewDidAppear:animated];
  26. [self.eventEmitter sendComponentDidAppear:self.layoutInfo.componentId componentName:self.layoutInfo.name];
  27. }
  28. - (void)viewWillDisappear:(BOOL)animated {
  29. [super viewWillDisappear:animated];
  30. }
  31. - (void)viewDidDisappear:(BOOL)animated {
  32. [super viewDidDisappear:animated];
  33. [self.eventEmitter sendComponentDidDisappear:self.layoutInfo.componentId componentName:self.layoutInfo.name];
  34. }
  35. - (void)loadView {
  36. [self renderReactViewIfNeeded];
  37. }
  38. - (void)render {
  39. if (!self.waitForRender)
  40. [self readyForPresentation];
  41. else
  42. [self renderReactViewIfNeeded];
  43. }
  44. - (void)renderReactViewIfNeeded {
  45. if (!self.isViewLoaded) {
  46. self.view = [self.creator createRootView:self.layoutInfo.name rootViewId:self.layoutInfo.componentId reactViewReadyBlock:^{
  47. [self->_presenter renderComponents:self.resolveOptions perform:^{
  48. [self readyForPresentation];
  49. }];
  50. }];
  51. } else {
  52. [self readyForPresentation];
  53. }
  54. }
  55. - (UIViewController *)getCurrentChild {
  56. return nil;
  57. }
  58. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  59. [self.eventEmitter sendOnSearchBarUpdated:self.layoutInfo.componentId
  60. text:searchController.searchBar.text
  61. isFocused:searchController.searchBar.isFirstResponder];
  62. }
  63. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  64. [self.eventEmitter sendOnSearchBarCancelPressed:self.layoutInfo.componentId];
  65. }
  66. -(BOOL)isCustomTransitioned {
  67. return self.resolveOptions.customTransition.animations != nil;
  68. }
  69. - (BOOL)prefersStatusBarHidden {
  70. return [_presenter isStatusBarVisibility:self.navigationController resolvedOptions:self.resolveOptions];
  71. }
  72. - (UIStatusBarStyle)preferredStatusBarStyle {
  73. return [_presenter getStatusBarStyle:[self resolveOptions]];
  74. }
  75. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  76. RNNComponentViewController* vc = (RNNComponentViewController*)viewController;
  77. if (![[vc.self.resolveOptions.topBar.backButton.transition getWithDefaultValue:@""] isEqualToString:@"custom"]){
  78. navigationController.delegate = nil;
  79. }
  80. }
  81. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  82. animationControllerForOperation:(UINavigationControllerOperation)operation
  83. fromViewController:(UIViewController*)fromVC
  84. toViewController:(UIViewController*)toVC {
  85. if (self.animator) {
  86. return self.animator;
  87. } else if (operation == UINavigationControllerOperationPush && self.resolveOptions.animations.push.hasCustomAnimation) {
  88. return [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:self.resolveOptions.animations.push isDismiss:NO];
  89. } else if (operation == UINavigationControllerOperationPop && self.resolveOptions.animations.pop.hasCustomAnimation) {
  90. return [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:self.resolveOptions.animations.pop isDismiss:YES];
  91. } else {
  92. return nil;
  93. }
  94. return nil;
  95. }
  96. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  97. return self.previewController;
  98. }
  99. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  100. if (self.previewCallback) {
  101. self.previewCallback(self);
  102. }
  103. }
  104. - (void)onActionPress:(NSString *)id {
  105. [_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
  106. }
  107. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  108. NSString *actionId = action[@"id"];
  109. NSString *actionTitle = action[@"title"];
  110. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  111. if ([action[@"style"] isEqualToString:@"selected"]) {
  112. actionStyle = UIPreviewActionStyleSelected;
  113. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  114. actionStyle = UIPreviewActionStyleDestructive;
  115. }
  116. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  117. [self onActionPress:actionId];
  118. }];
  119. }
  120. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  121. NSMutableArray *actions = [[NSMutableArray alloc] init];
  122. for (NSDictionary *previewAction in self.resolveOptions.preview.actions) {
  123. UIPreviewAction *action = [self convertAction:previewAction];
  124. NSDictionary *actionActions = previewAction[@"actions"];
  125. if (actionActions.count > 0) {
  126. NSMutableArray *group = [[NSMutableArray alloc] init];
  127. for (NSDictionary *previewGroupAction in actionActions) {
  128. [group addObject:[self convertAction:previewGroupAction]];
  129. }
  130. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  131. [actions addObject:actionGroup];
  132. } else {
  133. [actions addObject:action];
  134. }
  135. }
  136. return actions;
  137. }
  138. -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
  139. [self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
  140. }
  141. @end