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