react-native-navigation的迁移库

RNNComponentViewController.m 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. self.extendedLayoutIncludesOpaqueBars = YES;
  7. if (@available(iOS 13.0, *)) {
  8. self.navigationItem.standardAppearance = [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. - (UIStatusBarStyle)preferredStatusBarStyle {
  66. return [_presenter getStatusBarStyle:[self resolveOptions]];
  67. }
  68. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  69. return self.previewController;
  70. }
  71. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  72. if (self.previewCallback) {
  73. self.previewCallback(self);
  74. }
  75. }
  76. - (void)onActionPress:(NSString *)id {
  77. [_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
  78. }
  79. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  80. NSString *actionId = action[@"id"];
  81. NSString *actionTitle = action[@"title"];
  82. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  83. if ([action[@"style"] isEqualToString:@"selected"]) {
  84. actionStyle = UIPreviewActionStyleSelected;
  85. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  86. actionStyle = UIPreviewActionStyleDestructive;
  87. }
  88. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  89. [self onActionPress:actionId];
  90. }];
  91. }
  92. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  93. NSMutableArray *actions = [[NSMutableArray alloc] init];
  94. for (NSDictionary *previewAction in self.resolveOptions.preview.actions) {
  95. UIPreviewAction *action = [self convertAction:previewAction];
  96. NSDictionary *actionActions = previewAction[@"actions"];
  97. if (actionActions.count > 0) {
  98. NSMutableArray *group = [[NSMutableArray alloc] init];
  99. for (NSDictionary *previewGroupAction in actionActions) {
  100. [group addObject:[self convertAction:previewGroupAction]];
  101. }
  102. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  103. [actions addObject:actionGroup];
  104. } else {
  105. [actions addObject:action];
  106. }
  107. }
  108. return actions;
  109. }
  110. -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
  111. [self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
  112. }
  113. @end