react-native-navigation的迁移库

RNNComponentViewController.m 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #import "RNNComponentViewController.h"
  2. #import "UIViewController+LayoutProtocol.h"
  3. @implementation RNNComponentViewController
  4. @synthesize previewCallback;
  5. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo rootViewCreator:(id<RNNComponentViewCreator>)creator eventEmitter:(RNNEventEmitter *)eventEmitter presenter:(RNNComponentPresenter *)presenter options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions {
  6. self = [super initWithLayoutInfo:layoutInfo creator:creator options:options defaultOptions:defaultOptions presenter:presenter eventEmitter:eventEmitter childViewControllers:nil];
  7. self.extendedLayoutIncludesOpaqueBars = YES;
  8. return self;
  9. }
  10. - (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  11. _defaultOptions = defaultOptions;
  12. [_presenter setDefaultOptions:defaultOptions];
  13. }
  14. - (void)overrideOptions:(RNNNavigationOptions *)options {
  15. [self.options overrideOptions:options];
  16. }
  17. - (void)viewWillAppear:(BOOL)animated {
  18. [super viewWillAppear:animated];
  19. [_presenter applyOptions:self.resolveOptions];
  20. [self.parentViewController onChildWillAppear];
  21. }
  22. - (void)viewDidAppear:(BOOL)animated {
  23. [super viewDidAppear:animated];
  24. [self componentDidAppear];
  25. }
  26. - (void)viewDidDisappear:(BOOL)animated {
  27. [super viewDidDisappear:animated];
  28. [self componentDidDisappear];
  29. }
  30. - (void)loadView {
  31. [self renderReactViewIfNeeded];
  32. }
  33. - (void)render {
  34. if (!self.waitForRender)
  35. [self readyForPresentation];
  36. [self renderReactViewIfNeeded];
  37. }
  38. - (void)renderReactViewIfNeeded {
  39. if (!self.isViewLoaded) {
  40. self.view = [self.creator createRootView:self.layoutInfo.name
  41. rootViewId:self.layoutInfo.componentId
  42. ofType:RNNComponentTypeComponent
  43. reactViewReadyBlock:^{
  44. [self->_presenter renderComponents:self.resolveOptions perform:^{
  45. [self readyForPresentation];
  46. }];
  47. }];
  48. } else {
  49. [self readyForPresentation];
  50. }
  51. }
  52. - (UIViewController *)getCurrentChild {
  53. return nil;
  54. }
  55. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  56. [self.eventEmitter sendOnSearchBarUpdated:self.layoutInfo.componentId
  57. text:searchController.searchBar.text
  58. isFocused:searchController.searchBar.isFirstResponder];
  59. }
  60. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  61. [self.eventEmitter sendOnSearchBarCancelPressed:self.layoutInfo.componentId];
  62. }
  63. - (BOOL)prefersStatusBarHidden {
  64. return [_presenter isStatusBarVisibility:self.navigationController resolvedOptions:self.resolveOptions];
  65. }
  66. - (UIStatusBarStyle)preferredStatusBarStyle {
  67. return [_presenter getStatusBarStyle:[self resolveOptions]];
  68. }
  69. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  70. return self.previewController;
  71. }
  72. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  73. if (self.previewCallback) {
  74. self.previewCallback(self);
  75. }
  76. }
  77. - (void)onActionPress:(NSString *)id {
  78. [_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
  79. }
  80. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  81. NSString *actionId = action[@"id"];
  82. NSString *actionTitle = action[@"title"];
  83. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  84. if ([action[@"style"] isEqualToString:@"selected"]) {
  85. actionStyle = UIPreviewActionStyleSelected;
  86. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  87. actionStyle = UIPreviewActionStyleDestructive;
  88. }
  89. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  90. [self onActionPress:actionId];
  91. }];
  92. }
  93. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  94. NSMutableArray *actions = [[NSMutableArray alloc] init];
  95. for (NSDictionary *previewAction in self.resolveOptions.preview.actions) {
  96. UIPreviewAction *action = [self convertAction:previewAction];
  97. NSDictionary *actionActions = previewAction[@"actions"];
  98. if (actionActions.count > 0) {
  99. NSMutableArray *group = [[NSMutableArray alloc] init];
  100. for (NSDictionary *previewGroupAction in actionActions) {
  101. [group addObject:[self convertAction:previewGroupAction]];
  102. }
  103. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  104. [actions addObject:actionGroup];
  105. } else {
  106. [actions addObject:action];
  107. }
  108. }
  109. return actions;
  110. }
  111. -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
  112. [self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
  113. }
  114. @end