react-native-navigation的迁移库

RNNComponentPresenter.m 7.5KB

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