react-native-navigation的迁移库

RNNComponentPresenter.m 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #import "RNNComponentPresenter.h"
  2. #import "UIViewController+RNNOptions.h"
  3. #import "UITabBarController+RNNOptions.h"
  4. #import "RCTConvert+Modal.h"
  5. #import "RNNTitleViewHelper.h"
  6. #import "UIViewController+LayoutProtocol.h"
  7. #import "RNNReactTitleView.h"
  8. #import "TopBarTitlePresenter.h"
  9. @implementation RNNComponentPresenter {
  10. TopBarTitlePresenter* _topBarTitlePresenter;
  11. }
  12. - (instancetype)initWithComponentRegistry:(RNNReactComponentRegistry *)componentRegistry defaultOptions:(RNNNavigationOptions *)defaultOptions {
  13. self = [super initWithComponentRegistry:componentRegistry defaultOptions:defaultOptions];
  14. _topBarTitlePresenter = [[TopBarTitlePresenter alloc] initWithComponentRegistry:componentRegistry defaultOptions:defaultOptions];
  15. return self;
  16. }
  17. - (void)bindViewController:(id)boundViewController {
  18. [super bindViewController:boundViewController];
  19. [_topBarTitlePresenter bindViewController:boundViewController];
  20. _navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:boundViewController componentRegistry:self.componentRegistry];
  21. }
  22. - (void)componentDidAppear {
  23. RNNReactView* component = (RNNReactView *)self.boundViewController.view;
  24. if ([component respondsToSelector:@selector(componentDidAppear)]) {
  25. [component componentDidAppear];
  26. }
  27. [_topBarTitlePresenter componentDidAppear];
  28. [_navigationButtons componentDidAppear];
  29. }
  30. - (void)componentDidDisappear {
  31. RNNReactView* component = (RNNReactView *)self.boundViewController.view;
  32. if ([component respondsToSelector:@selector(componentDidDisappear)]) {
  33. [component componentDidDisappear];
  34. }
  35. [_topBarTitlePresenter componentDidDisappear];
  36. [_navigationButtons componentDidDisappear];
  37. }
  38. - (void)applyOptionsOnWillMoveToParentViewController:(RNNNavigationOptions *)options {
  39. [super applyOptionsOnWillMoveToParentViewController:options];
  40. }
  41. - (void)applyOptions:(RNNNavigationOptions *)options {
  42. [super applyOptions:options];
  43. UIViewController* viewController = self.boundViewController;
  44. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  45. [viewController setBackgroundImage:[withDefault.backgroundImage getWithDefaultValue:nil]];
  46. [viewController setTopBarPrefersLargeTitle:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
  47. [viewController setTabBarItemBadgeColor:[withDefault.bottomTab.badgeColor getWithDefaultValue:nil]];
  48. [viewController setStatusBarBlur:[withDefault.statusBar.blur getWithDefaultValue:NO]];
  49. [viewController setStatusBarStyle:[withDefault.statusBar.style getWithDefaultValue:@"default"] animated:[withDefault.statusBar.animate getWithDefaultValue:YES]];
  50. [viewController setBackButtonVisible:[withDefault.topBar.backButton.visible getWithDefaultValue:YES]];
  51. [viewController setInterceptTouchOutside:[withDefault.overlay.interceptTouchOutside getWithDefaultValue:YES]];
  52. if (@available(iOS 13.0, *)) {
  53. [viewController setBackgroundColor:[withDefault.layout.componentBackgroundColor getWithDefaultValue:UIColor.systemBackgroundColor]];
  54. } else {
  55. [viewController setBackgroundColor:[withDefault.layout.componentBackgroundColor getWithDefaultValue:viewController.view.backgroundColor]];
  56. }
  57. if (withDefault.topBar.searchBar.hasValue) {
  58. BOOL hideNavBarOnFocusSearchBar = YES;
  59. if (withDefault.topBar.hideNavBarOnFocusSearchBar.hasValue) {
  60. hideNavBarOnFocusSearchBar = withDefault.topBar.hideNavBarOnFocusSearchBar.get;
  61. }
  62. [viewController setSearchBarWithPlaceholder:[withDefault.topBar.searchBarPlaceholder getWithDefaultValue:@""] hideNavBarOnFocusSearchBar:hideNavBarOnFocusSearchBar];
  63. }
  64. [_topBarTitlePresenter applyOptions:withDefault.topBar];
  65. }
  66. - (void)applyOptionsOnInit:(RNNNavigationOptions *)options {
  67. [super applyOptionsOnInit:options];
  68. UIViewController* viewController = self.boundViewController;
  69. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  70. [viewController setDrawBehindTopBar:[withDefault.topBar.drawBehind getWithDefaultValue:NO]];
  71. [viewController setDrawBehindTabBar:[withDefault.bottomTabs.drawBehind getWithDefaultValue:NO] || ![withDefault.bottomTabs.visible getWithDefaultValue:YES]];
  72. if ((withDefault.topBar.leftButtons || withDefault.topBar.rightButtons)) {
  73. [_navigationButtons applyLeftButtons:withDefault.topBar.leftButtons rightButtons:withDefault.topBar.rightButtons defaultLeftButtonStyle:withDefault.topBar.leftButtonStyle defaultRightButtonStyle:withDefault.topBar.rightButtonStyle];
  74. }
  75. }
  76. - (void)mergeOptions:(RNNNavigationOptions *)options resolvedOptions:(RNNNavigationOptions *)currentOptions {
  77. [super mergeOptions:options resolvedOptions:currentOptions];
  78. RNNNavigationOptions * withDefault = (RNNNavigationOptions *) [[currentOptions overrideOptions:options] withDefault:[self defaultOptions]];
  79. UIViewController* viewController = self.boundViewController;
  80. if (options.backgroundImage.hasValue) {
  81. [viewController setBackgroundImage:options.backgroundImage.get];
  82. }
  83. if (options.modalPresentationStyle.hasValue) {
  84. [viewController setModalPresentationStyle:[RCTConvert UIModalPresentationStyle:options.modalPresentationStyle.get]];
  85. }
  86. if (options.modalTransitionStyle.hasValue) {
  87. [viewController setModalTransitionStyle:[RCTConvert UIModalTransitionStyle:options.modalTransitionStyle.get]];
  88. }
  89. if (options.topBar.searchBar.hasValue) {
  90. BOOL hideNavBarOnFocusSearchBar = YES;
  91. if (options.topBar.hideNavBarOnFocusSearchBar.hasValue) {
  92. hideNavBarOnFocusSearchBar = options.topBar.hideNavBarOnFocusSearchBar.get;
  93. }
  94. [viewController setSearchBarWithPlaceholder:[options.topBar.searchBarPlaceholder getWithDefaultValue:@""] hideNavBarOnFocusSearchBar:hideNavBarOnFocusSearchBar];
  95. }
  96. if (options.topBar.drawBehind.hasValue) {
  97. [viewController setDrawBehindTopBar:options.topBar.drawBehind.get];
  98. }
  99. if (options.topBar.title.text.hasValue) {
  100. [viewController setNavigationItemTitle:options.topBar.title.text.get];
  101. }
  102. if (options.topBar.largeTitle.visible.hasValue) {
  103. [viewController setTopBarPrefersLargeTitle:options.topBar.largeTitle.visible.get];
  104. }
  105. if (options.layout.componentBackgroundColor.hasValue) {
  106. [viewController setBackgroundColor:options.layout.componentBackgroundColor.get];
  107. }
  108. if (options.bottomTab.badgeColor.hasValue) {
  109. [viewController setTabBarItemBadgeColor:options.bottomTab.badgeColor.get];
  110. }
  111. if (options.bottomTab.visible.hasValue) {
  112. [viewController.tabBarController setCurrentTabIndex:[viewController.tabBarController.viewControllers indexOfObject:viewController]];
  113. }
  114. if (options.statusBar.blur.hasValue) {
  115. [viewController setStatusBarBlur:options.statusBar.blur.get];
  116. }
  117. if (options.statusBar.style.hasValue) {
  118. [viewController setStatusBarStyle:options.statusBar.style.get animated:[withDefault.statusBar.animate getWithDefaultValue:YES]];
  119. }
  120. if (options.topBar.backButton.visible.hasValue) {
  121. [viewController setBackButtonVisible:options.topBar.backButton.visible.get];
  122. }
  123. if (options.topBar.leftButtons || options.topBar.rightButtons) {
  124. [_navigationButtons applyLeftButtons:options.topBar.leftButtons rightButtons:options.topBar.rightButtons defaultLeftButtonStyle:withDefault.topBar.leftButtonStyle defaultRightButtonStyle:withDefault.topBar.rightButtonStyle];
  125. }
  126. if (options.overlay.interceptTouchOutside.hasValue) {
  127. RCTRootView* rootView = (RCTRootView*)viewController.view;
  128. rootView.passThroughTouches = !options.overlay.interceptTouchOutside.get;
  129. }
  130. [_topBarTitlePresenter mergeOptions:options.topBar resolvedOptions:withDefault.topBar];
  131. }
  132. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  133. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  134. [_topBarTitlePresenter renderComponents:withDefault.topBar perform:readyBlock];
  135. }
  136. - (void)dealloc {
  137. [self.componentRegistry clearComponentsForParentId:self.boundComponentId];
  138. }
  139. @end