react-native-navigation的迁移库

RNNComponentViewController.m 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. if (@available(iOS 13.0, *)) {
  9. self.navigationItem.standardAppearance = [UINavigationBarAppearance new];
  10. }
  11. return self;
  12. }
  13. - (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  14. _defaultOptions = defaultOptions;
  15. [_presenter setDefaultOptions:defaultOptions];
  16. }
  17. - (void)overrideOptions:(RNNNavigationOptions *)options {
  18. [self.options overrideOptions:options];
  19. }
  20. - (void)viewWillAppear:(BOOL)animated {
  21. [super viewWillAppear:animated];
  22. [_presenter applyOptions:self.resolveOptions];
  23. [self.parentViewController onChildWillAppear];
  24. }
  25. - (void)viewDidAppear:(BOOL)animated {
  26. [super viewDidAppear:animated];
  27. [self componentDidAppear];
  28. }
  29. - (void)viewDidDisappear:(BOOL)animated {
  30. [super viewDidDisappear:animated];
  31. [self componentDidDisappear];
  32. }
  33. - (void)loadView {
  34. [self renderReactViewIfNeeded];
  35. }
  36. - (void)render {
  37. if (!self.waitForRender)
  38. [self readyForPresentation];
  39. [self renderReactViewIfNeeded];
  40. }
  41. - (void)renderReactViewIfNeeded {
  42. if (!self.isViewLoaded) {
  43. self.view = [self.creator createRootView:self.layoutInfo.name
  44. rootViewId:self.layoutInfo.componentId
  45. ofType:RNNComponentTypeComponent
  46. reactViewReadyBlock:^{
  47. [self->_presenter renderComponents:self.resolveOptions perform:^{
  48. [self readyForPresentation];
  49. }];
  50. }];
  51. } else {
  52. [self readyForPresentation];
  53. }
  54. }
  55. - (UIViewController *)getCurrentChild {
  56. return nil;
  57. }
  58. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  59. [self.eventEmitter sendOnSearchBarUpdated:self.layoutInfo.componentId
  60. text:searchController.searchBar.text
  61. isFocused:searchController.searchBar.isFirstResponder];
  62. }
  63. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  64. [self.eventEmitter sendOnSearchBarCancelPressed:self.layoutInfo.componentId];
  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