react-native-navigation的迁移库

RNNComponentViewController.m 6.3KB

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