react-native-navigation的迁移库

RNNComponentViewController.m 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #import "RNNComponentViewController.h"
  2. @implementation RNNComponentViewController
  3. @synthesize previewCallback;
  4. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo rootViewCreator:(id<RNNComponentViewCreator>)creator eventEmitter:(RNNEventEmitter *)eventEmitter presenter:(RNNComponentPresenter *)presenter options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions {
  5. self = [super initWithLayoutInfo:layoutInfo creator:creator options:options defaultOptions:defaultOptions presenter:presenter eventEmitter:eventEmitter childViewControllers:nil];
  6. if (@available(iOS 13.0, *)) {
  7. self.navigationItem.standardAppearance = [UINavigationBarAppearance new];
  8. self.navigationItem.scrollEdgeAppearance = [UINavigationBarAppearance new];
  9. }
  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 componentDidAppear];
  27. }
  28. - (void)viewDidDisappear:(BOOL)animated {
  29. [super viewDidDisappear:animated];
  30. [self componentDidDisappear];
  31. }
  32. - (void)loadView {
  33. [self renderReactViewIfNeeded];
  34. }
  35. - (void)render {
  36. if (!self.waitForRender)
  37. [self readyForPresentation];
  38. [self renderReactViewIfNeeded];
  39. }
  40. - (void)destroyReactView {
  41. if ([self.view isKindOfClass: [RNNReactView class]]) {
  42. [((RNNReactView *)self.view) invalidate];
  43. }
  44. }
  45. - (void)renderReactViewIfNeeded {
  46. if (!self.isViewLoaded) {
  47. self.view = [self.creator createRootView:self.layoutInfo.name
  48. rootViewId:self.layoutInfo.componentId
  49. ofType:RNNComponentTypeComponent
  50. reactViewReadyBlock:^{
  51. [self->_presenter renderComponents:self.resolveOptions perform:^{
  52. [self readyForPresentation];
  53. }];
  54. }];
  55. } else {
  56. [self readyForPresentation];
  57. }
  58. }
  59. - (UIViewController *)getCurrentChild {
  60. return nil;
  61. }
  62. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  63. [self.eventEmitter sendOnSearchBarUpdated:self.layoutInfo.componentId
  64. text:searchController.searchBar.text
  65. isFocused:searchController.searchBar.isFirstResponder];
  66. }
  67. - (void)screenPopped {
  68. [_eventEmitter sendScreenPoppedEvent:self.layoutInfo.componentId];
  69. }
  70. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  71. [self.eventEmitter sendOnSearchBarCancelPressed:self.layoutInfo.componentId];
  72. }
  73. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  74. return self.previewController;
  75. }
  76. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  77. if (self.previewCallback) {
  78. self.previewCallback(self);
  79. }
  80. }
  81. - (void)onActionPress:(NSString *)id {
  82. [_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
  83. }
  84. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  85. NSString *actionId = action[@"id"];
  86. NSString *actionTitle = action[@"title"];
  87. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  88. if ([action[@"style"] isEqualToString:@"selected"]) {
  89. actionStyle = UIPreviewActionStyleSelected;
  90. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  91. actionStyle = UIPreviewActionStyleDestructive;
  92. }
  93. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  94. [self onActionPress:actionId];
  95. }];
  96. }
  97. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  98. NSMutableArray *actions = [[NSMutableArray alloc] init];
  99. for (NSDictionary *previewAction in self.resolveOptions.preview.actions) {
  100. UIPreviewAction *action = [self convertAction:previewAction];
  101. NSDictionary *actionActions = previewAction[@"actions"];
  102. if (actionActions.count > 0) {
  103. NSMutableArray *group = [[NSMutableArray alloc] init];
  104. for (NSDictionary *previewGroupAction in actionActions) {
  105. [group addObject:[self convertAction:previewGroupAction]];
  106. }
  107. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  108. [actions addObject:actionGroup];
  109. } else {
  110. [actions addObject:action];
  111. }
  112. }
  113. return actions;
  114. }
  115. -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
  116. [self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
  117. }
  118. # pragma mark - UIViewController overrides
  119. - (void)willMoveToParentViewController:(UIViewController *)parent {
  120. [self.presenter willMoveToParentViewController:parent];
  121. }
  122. - (UIStatusBarStyle)preferredStatusBarStyle {
  123. return [self.presenter getStatusBarStyle];
  124. }
  125. - (BOOL)prefersStatusBarHidden {
  126. return [self.presenter getStatusBarVisibility];
  127. }
  128. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  129. return [self.presenter getOrientation];
  130. }
  131. - (BOOL)hidesBottomBarWhenPushed {
  132. return [self.presenter hidesBottomBarWhenPushed];
  133. }
  134. @end