react-native-navigation的迁移库

RNNComponentViewController.m 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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)renderReactViewIfNeeded {
  41. if (!self.isViewLoaded) {
  42. self.view = [self.creator createRootView:self.layoutInfo.name
  43. rootViewId:self.layoutInfo.componentId
  44. ofType:RNNComponentTypeComponent
  45. 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. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  66. return self.previewController;
  67. }
  68. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  69. if (self.previewCallback) {
  70. self.previewCallback(self);
  71. }
  72. }
  73. - (void)onActionPress:(NSString *)id {
  74. [_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
  75. }
  76. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  77. NSString *actionId = action[@"id"];
  78. NSString *actionTitle = action[@"title"];
  79. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  80. if ([action[@"style"] isEqualToString:@"selected"]) {
  81. actionStyle = UIPreviewActionStyleSelected;
  82. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  83. actionStyle = UIPreviewActionStyleDestructive;
  84. }
  85. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  86. [self onActionPress:actionId];
  87. }];
  88. }
  89. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  90. NSMutableArray *actions = [[NSMutableArray alloc] init];
  91. for (NSDictionary *previewAction in self.resolveOptions.preview.actions) {
  92. UIPreviewAction *action = [self convertAction:previewAction];
  93. NSDictionary *actionActions = previewAction[@"actions"];
  94. if (actionActions.count > 0) {
  95. NSMutableArray *group = [[NSMutableArray alloc] init];
  96. for (NSDictionary *previewGroupAction in actionActions) {
  97. [group addObject:[self convertAction:previewGroupAction]];
  98. }
  99. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  100. [actions addObject:actionGroup];
  101. } else {
  102. [actions addObject:action];
  103. }
  104. }
  105. return actions;
  106. }
  107. -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
  108. [self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
  109. }
  110. # pragma mark - UIViewController overrides
  111. - (void)willMoveToParentViewController:(UIViewController *)parent {
  112. [self.presenter willMoveToParentViewController:parent];
  113. }
  114. - (UIStatusBarStyle)preferredStatusBarStyle {
  115. return [self.presenter getStatusBarStyle];
  116. }
  117. - (BOOL)prefersStatusBarHidden {
  118. return [self.presenter getStatusBarVisibility];
  119. }
  120. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  121. return [self.presenter getOrientation];
  122. }
  123. - (BOOL)hidesBottomBarWhenPushed {
  124. return [self.presenter hidesBottomBarWhenPushed];
  125. }
  126. @end