react-native-navigation的迁移库

RNNComponentViewController.m 4.7KB

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