react-native-navigation的迁移库

RNNComponentPresenter.m 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.bottomTabs.drawBehind.hasValue) {
  100. [viewController setDrawBehindTabBar:options.bottomTabs.drawBehind.get];
  101. }
  102. if (options.topBar.title.text.hasValue) {
  103. [viewController setNavigationItemTitle:options.topBar.title.text.get];
  104. }
  105. if (options.topBar.largeTitle.visible.hasValue) {
  106. [viewController setTopBarPrefersLargeTitle:options.topBar.largeTitle.visible.get];
  107. }
  108. if (options.layout.componentBackgroundColor.hasValue) {
  109. [viewController setBackgroundColor:options.layout.componentBackgroundColor.get];
  110. }
  111. if (options.bottomTab.badgeColor.hasValue) {
  112. [viewController setTabBarItemBadgeColor:options.bottomTab.badgeColor.get];
  113. }
  114. if (options.bottomTab.visible.hasValue) {
  115. [viewController.tabBarController setCurrentTabIndex:[viewController.tabBarController.viewControllers indexOfObject:viewController]];
  116. }
  117. if (options.statusBar.blur.hasValue) {
  118. [viewController setStatusBarBlur:options.statusBar.blur.get];
  119. }
  120. if (options.statusBar.style.hasValue) {
  121. [viewController setStatusBarStyle:options.statusBar.style.get animated:[withDefault.statusBar.animate getWithDefaultValue:YES]];
  122. }
  123. if (options.topBar.backButton.visible.hasValue) {
  124. [viewController setBackButtonVisible:options.topBar.backButton.visible.get];
  125. }
  126. if (options.topBar.leftButtons || options.topBar.rightButtons) {
  127. [_navigationButtons applyLeftButtons:options.topBar.leftButtons rightButtons:options.topBar.rightButtons defaultLeftButtonStyle:withDefault.topBar.leftButtonStyle defaultRightButtonStyle:withDefault.topBar.rightButtonStyle];
  128. }
  129. if (options.overlay.interceptTouchOutside.hasValue) {
  130. RCTRootView* rootView = (RCTRootView*)viewController.view;
  131. rootView.passThroughTouches = !options.overlay.interceptTouchOutside.get;
  132. }
  133. [_topBarTitlePresenter mergeOptions:options.topBar resolvedOptions:withDefault.topBar];
  134. }
  135. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  136. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  137. [_topBarTitlePresenter renderComponents:withDefault.topBar perform:readyBlock];
  138. }
  139. - (void)dealloc {
  140. [self.componentRegistry clearComponentsForParentId:self.boundComponentId];
  141. }
  142. @end