react-native-navigation的迁移库

RNNComponentViewController.m 5.4KB

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