react-native-navigation的迁移库

RNNComponentPresenter.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. @interface RNNComponentPresenter() {
  8. RNNReactView* _customTitleView;
  9. RNNTitleViewHelper* _titleViewHelper;
  10. RNNReactComponentRegistry* _componentRegistry;
  11. }
  12. @end
  13. @implementation RNNComponentPresenter
  14. - (instancetype)initWithComponentRegistry:(RNNReactComponentRegistry *)componentRegistry:(RNNNavigationOptions *)defaultOptions {
  15. self = [self initWithDefaultOptions:defaultOptions];
  16. _componentRegistry = componentRegistry;
  17. return self;
  18. }
  19. - (void)boundViewController:(UIViewController *)boundViewController {
  20. [super boundViewController:boundViewController];
  21. _navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:self.boundViewController componentRegistry:_componentRegistry];
  22. }
  23. - (void)applyOptionsOnWillMoveToParentViewController:(RNNNavigationOptions *)options {
  24. [super applyOptionsOnWillMoveToParentViewController:options];
  25. UIViewController* viewController = self.boundViewController;
  26. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  27. [viewController setBackButtonIcon:[withDefault.topBar.backButton.icon getWithDefaultValue:nil] withColor:[withDefault.topBar.backButton.color getWithDefaultValue:nil] title:[withDefault.topBar.backButton.showTitle getWithDefaultValue:YES] ? [withDefault.topBar.backButton.title getWithDefaultValue:nil] : @""];
  28. }
  29. - (void)applyOptions:(RNNNavigationOptions *)options {
  30. [super applyOptions:options];
  31. UIViewController* viewController = self.boundViewController;
  32. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  33. [viewController setBackgroundImage:[withDefault.backgroundImage getWithDefaultValue:nil]];
  34. [viewController setNavigationItemTitle:[withDefault.topBar.title.text getWithDefaultValue:nil]];
  35. [viewController setTopBarPrefersLargeTitle:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
  36. [viewController setTabBarItemBadgeColor:[withDefault.bottomTab.badgeColor getWithDefaultValue:nil]];
  37. [viewController setStatusBarBlur:[withDefault.statusBar.blur getWithDefaultValue:NO]];
  38. [viewController setStatusBarStyle:[withDefault.statusBar.style getWithDefaultValue:@"default"] animated:[withDefault.statusBar.animate getWithDefaultValue:YES]];
  39. [viewController setBackButtonVisible:[withDefault.topBar.backButton.visible getWithDefaultValue:YES]];
  40. [viewController setInterceptTouchOutside:[withDefault.overlay.interceptTouchOutside getWithDefaultValue:YES]];
  41. if (withDefault.layout.backgroundColor.hasValue) {
  42. [viewController setBackgroundColor:withDefault.layout.backgroundColor.get];
  43. }
  44. if (withDefault.topBar.searchBar.hasValue) {
  45. BOOL hideNavBarOnFocusSearchBar = YES;
  46. if (withDefault.topBar.hideNavBarOnFocusSearchBar.hasValue) {
  47. hideNavBarOnFocusSearchBar = withDefault.topBar.hideNavBarOnFocusSearchBar.get;
  48. }
  49. [viewController setSearchBarWithPlaceholder:[withDefault.topBar.searchBarPlaceholder getWithDefaultValue:@""] hideNavBarOnFocusSearchBar:hideNavBarOnFocusSearchBar];
  50. }
  51. [self setTitleViewWithSubtitle:withDefault];
  52. }
  53. - (void)applyOptionsOnInit:(RNNNavigationOptions *)options {
  54. [super applyOptionsOnInit:options];
  55. UIViewController* viewController = self.boundViewController;
  56. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  57. [viewController setModalPresentationStyle:[RCTConvert UIModalPresentationStyle:[withDefault.modalPresentationStyle getWithDefaultValue:@"fullScreen"]]];
  58. [viewController setModalTransitionStyle:[RCTConvert UIModalTransitionStyle:[withDefault.modalTransitionStyle getWithDefaultValue:@"coverVertical"]]];
  59. [viewController setDrawBehindTopBar:[withDefault.topBar.drawBehind getWithDefaultValue:NO]];
  60. [viewController setDrawBehindTabBar:[withDefault.bottomTabs.drawBehind getWithDefaultValue:NO] || ![withDefault.bottomTabs.visible getWithDefaultValue:YES]];
  61. if ((withDefault.topBar.leftButtons || withDefault.topBar.rightButtons)) {
  62. [_navigationButtons applyLeftButtons:withDefault.topBar.leftButtons rightButtons:withDefault.topBar.rightButtons defaultLeftButtonStyle:withDefault.topBar.leftButtonStyle defaultRightButtonStyle:withDefault.topBar.rightButtonStyle];
  63. }
  64. }
  65. - (void)mergeOptions:(RNNNavigationOptions *)options resolvedOptions:(RNNNavigationOptions *)currentOptions {
  66. [super mergeOptions:options resolvedOptions:currentOptions];
  67. RNNNavigationOptions * withDefault = (RNNNavigationOptions *) [[currentOptions overrideOptions:options] withDefault:[self defaultOptions]];
  68. UIViewController* viewController = self.boundViewController;
  69. [self removeTitleComponentIfNeeded:options];
  70. if (options.backgroundImage.hasValue) {
  71. [viewController setBackgroundImage:options.backgroundImage.get];
  72. }
  73. if (options.modalPresentationStyle.hasValue) {
  74. [viewController setModalPresentationStyle:[RCTConvert UIModalPresentationStyle:options.modalPresentationStyle.get]];
  75. }
  76. if (options.modalTransitionStyle.hasValue) {
  77. [viewController setModalTransitionStyle:[RCTConvert UIModalTransitionStyle:options.modalTransitionStyle.get]];
  78. }
  79. if (options.topBar.searchBar.hasValue) {
  80. BOOL hideNavBarOnFocusSearchBar = YES;
  81. if (options.topBar.hideNavBarOnFocusSearchBar.hasValue) {
  82. hideNavBarOnFocusSearchBar = options.topBar.hideNavBarOnFocusSearchBar.get;
  83. }
  84. [viewController setSearchBarWithPlaceholder:[options.topBar.searchBarPlaceholder getWithDefaultValue:@""] hideNavBarOnFocusSearchBar:hideNavBarOnFocusSearchBar];
  85. }
  86. if (options.topBar.drawBehind.hasValue) {
  87. [viewController setDrawBehindTopBar:options.topBar.drawBehind.get];
  88. }
  89. if (options.topBar.title.text.hasValue) {
  90. [viewController setNavigationItemTitle:options.topBar.title.text.get];
  91. }
  92. if (options.topBar.largeTitle.visible.hasValue) {
  93. [viewController setTopBarPrefersLargeTitle:options.topBar.largeTitle.visible.get];
  94. }
  95. if (options.bottomTabs.drawBehind.hasValue) {
  96. [viewController setDrawBehindTabBar:options.bottomTabs.drawBehind.get];
  97. }
  98. if (options.bottomTab.badgeColor.hasValue) {
  99. [viewController setTabBarItemBadgeColor:options.bottomTab.badgeColor.get];
  100. }
  101. if (options.layout.backgroundColor.hasValue) {
  102. [viewController setBackgroundColor:options.layout.backgroundColor.get];
  103. }
  104. if (options.bottomTab.visible.hasValue) {
  105. [viewController.tabBarController setCurrentTabIndex:[viewController.tabBarController.viewControllers indexOfObject:viewController]];
  106. }
  107. if (options.statusBar.blur.hasValue) {
  108. [viewController setStatusBarBlur:options.statusBar.blur.get];
  109. }
  110. if (options.statusBar.style.hasValue) {
  111. [viewController setStatusBarStyle:options.statusBar.style.get animated:[withDefault.statusBar.animate getWithDefaultValue:YES]];
  112. }
  113. if (options.topBar.backButton.visible.hasValue) {
  114. [viewController setBackButtonVisible:options.topBar.backButton.visible.get];
  115. }
  116. if (options.topBar.leftButtons || options.topBar.rightButtons) {
  117. [_navigationButtons applyLeftButtons:options.topBar.leftButtons rightButtons:options.topBar.rightButtons defaultLeftButtonStyle:withDefault.topBar.leftButtonStyle defaultRightButtonStyle:withDefault.topBar.rightButtonStyle];
  118. }
  119. if (options.overlay.interceptTouchOutside.hasValue) {
  120. RCTRootView* rootView = (RCTRootView*)viewController.view;
  121. rootView.passThroughTouches = !options.overlay.interceptTouchOutside.get;
  122. }
  123. if (options.topBar.title.component.name.hasValue) {
  124. [self setCustomNavigationTitleView:options perform:nil];
  125. }
  126. [self setTitleViewWithSubtitle:withDefault];
  127. if (options.topBar.backButton.hasValue) {
  128. UIViewController *lastViewControllerInStack = viewController.navigationController.viewControllers.count > 1 ? viewController.navigationController.viewControllers[viewController.navigationController.viewControllers.count - 2] : viewController.navigationController.topViewController;
  129. RNNNavigationOptions * resolvedOptions = (RNNNavigationOptions *) [[currentOptions overrideOptions:options] withDefault:[self defaultOptions]];
  130. [lastViewControllerInStack applyBackButton:resolvedOptions.topBar.backButton];
  131. }
  132. }
  133. - (void)removeTitleComponentIfNeeded:(RNNNavigationOptions *)options {
  134. if (options.topBar.title.text.hasValue && !options.topBar.component.hasValue) {
  135. [_customTitleView removeFromSuperview];
  136. _customTitleView = nil;
  137. }
  138. }
  139. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  140. [self setCustomNavigationTitleView:options perform:readyBlock];
  141. }
  142. - (void)setCustomNavigationTitleView:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  143. UIViewController<RNNLayoutProtocol>* viewController = self.boundViewController;
  144. if (![options.topBar.title.component.waitForRender getWithDefaultValue:NO] && readyBlock) {
  145. readyBlock();
  146. readyBlock = nil;
  147. }
  148. if (options.topBar.title.component.name.hasValue) {
  149. _customTitleView = [_componentRegistry createComponentIfNotExists:options.topBar.title.component parentComponentId:viewController.layoutInfo.componentId reactViewReadyBlock:readyBlock];
  150. _customTitleView.backgroundColor = UIColor.clearColor;
  151. NSString* alignment = [options.topBar.title.component.alignment getWithDefaultValue:@""];
  152. [_customTitleView setAlignment:alignment inFrame:viewController.navigationController.navigationBar.frame];
  153. [_customTitleView layoutIfNeeded];
  154. viewController.navigationItem.titleView = nil;
  155. viewController.navigationItem.titleView = _customTitleView;
  156. } else {
  157. [_customTitleView removeFromSuperview];
  158. if (readyBlock) {
  159. readyBlock();
  160. }
  161. }
  162. }
  163. - (void)setTitleViewWithSubtitle:(RNNNavigationOptions *)options {
  164. if (!_customTitleView) {
  165. _titleViewHelper = [[RNNTitleViewHelper alloc] initWithTitleViewOptions:options.topBar.title subTitleOptions:options.topBar.subtitle viewController:self.boundViewController];
  166. if (options.topBar.title.text.hasValue) {
  167. [_titleViewHelper setTitleOptions:options.topBar.title];
  168. }
  169. if (options.topBar.subtitle.text.hasValue) {
  170. [_titleViewHelper setSubtitleOptions:options.topBar.subtitle];
  171. }
  172. [_titleViewHelper setup];
  173. }
  174. }
  175. - (void)dealloc {
  176. [_componentRegistry clearComponentsForParentId:self.boundComponentId];
  177. }
  178. @end