react-native-navigation的迁移库

RNNComponentPresenter.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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)bindViewController:(UIViewController<RNNLayoutProtocol> *)bindedViewController {
  20. [super bindViewController:bindedViewController];
  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. if (options.backgroundImage.hasValue) {
  70. [viewController setBackgroundImage:options.backgroundImage.get];
  71. }
  72. if (options.modalPresentationStyle.hasValue) {
  73. [viewController setModalPresentationStyle:[RCTConvert UIModalPresentationStyle:options.modalPresentationStyle.get]];
  74. }
  75. if (options.modalTransitionStyle.hasValue) {
  76. [viewController setModalTransitionStyle:[RCTConvert UIModalTransitionStyle:options.modalTransitionStyle.get]];
  77. }
  78. if (options.topBar.searchBar.hasValue) {
  79. BOOL hideNavBarOnFocusSearchBar = YES;
  80. if (options.topBar.hideNavBarOnFocusSearchBar.hasValue) {
  81. hideNavBarOnFocusSearchBar = options.topBar.hideNavBarOnFocusSearchBar.get;
  82. }
  83. [viewController setSearchBarWithPlaceholder:[options.topBar.searchBarPlaceholder getWithDefaultValue:@""] hideNavBarOnFocusSearchBar:hideNavBarOnFocusSearchBar];
  84. }
  85. if (options.topBar.drawBehind.hasValue) {
  86. [viewController setDrawBehindTopBar:options.topBar.drawBehind.get];
  87. }
  88. if (options.topBar.title.text.hasValue) {
  89. [viewController setNavigationItemTitle:options.topBar.title.text.get];
  90. }
  91. if (options.topBar.largeTitle.visible.hasValue) {
  92. [viewController setTopBarPrefersLargeTitle:options.topBar.largeTitle.visible.get];
  93. }
  94. if (options.bottomTabs.drawBehind.hasValue) {
  95. [viewController setDrawBehindTabBar:options.bottomTabs.drawBehind.get];
  96. }
  97. if (options.bottomTab.badgeColor.hasValue) {
  98. [viewController setTabBarItemBadgeColor:options.bottomTab.badgeColor.get];
  99. }
  100. if (options.layout.backgroundColor.hasValue) {
  101. [viewController setBackgroundColor:options.layout.backgroundColor.get];
  102. }
  103. if (options.bottomTab.visible.hasValue) {
  104. [viewController.tabBarController setCurrentTabIndex:[viewController.tabBarController.viewControllers indexOfObject:viewController]];
  105. }
  106. if (options.statusBar.blur.hasValue) {
  107. [viewController setStatusBarBlur:options.statusBar.blur.get];
  108. }
  109. if (options.statusBar.style.hasValue) {
  110. [viewController setStatusBarStyle:options.statusBar.style.get animated:[withDefault.statusBar.animate getWithDefaultValue:YES]];
  111. }
  112. if (options.topBar.backButton.visible.hasValue) {
  113. [viewController setBackButtonVisible:options.topBar.backButton.visible.get];
  114. }
  115. if (options.topBar.leftButtons || options.topBar.rightButtons) {
  116. [_navigationButtons applyLeftButtons:options.topBar.leftButtons rightButtons:options.topBar.rightButtons defaultLeftButtonStyle:withDefault.topBar.leftButtonStyle defaultRightButtonStyle:withDefault.topBar.rightButtonStyle];
  117. }
  118. if (options.overlay.interceptTouchOutside.hasValue) {
  119. RCTRootView* rootView = (RCTRootView*)viewController.view;
  120. rootView.passThroughTouches = !options.overlay.interceptTouchOutside.get;
  121. }
  122. [self setTitleViewWithSubtitle:withDefault];
  123. if (options.topBar.title.component.name.hasValue) {
  124. [self setCustomNavigationTitleView:options perform:nil];
  125. } else {
  126. [_customTitleView removeFromSuperview];
  127. _customTitleView = nil;
  128. }
  129. if (options.topBar.backButton.hasValue) {
  130. UIViewController *lastViewControllerInStack = viewController.navigationController.viewControllers.count > 1 ? viewController.navigationController.viewControllers[viewController.navigationController.viewControllers.count - 2] : viewController.navigationController.topViewController;
  131. RNNNavigationOptions * resolvedOptions = (RNNNavigationOptions *) [[currentOptions overrideOptions:options] withDefault:[self defaultOptions]];
  132. [lastViewControllerInStack applyBackButton:resolvedOptions.topBar.backButton];
  133. }
  134. }
  135. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  136. [self setCustomNavigationTitleView:options perform:readyBlock];
  137. }
  138. - (void)setCustomNavigationTitleView:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  139. UIViewController<RNNLayoutProtocol>* viewController = self.boundViewController;
  140. if (![options.topBar.title.component.waitForRender getWithDefaultValue:NO] && readyBlock) {
  141. readyBlock();
  142. readyBlock = nil;
  143. }
  144. if (options.topBar.title.component.name.hasValue) {
  145. _customTitleView = [_componentRegistry createComponentIfNotExists:options.topBar.title.component parentComponentId:viewController.layoutInfo.componentId reactViewReadyBlock:readyBlock];
  146. _customTitleView.backgroundColor = UIColor.clearColor;
  147. NSString* alignment = [options.topBar.title.component.alignment getWithDefaultValue:@""];
  148. [_customTitleView setAlignment:alignment inFrame:viewController.navigationController.navigationBar.frame];
  149. [_customTitleView layoutIfNeeded];
  150. viewController.navigationItem.titleView = nil;
  151. viewController.navigationItem.titleView = _customTitleView;
  152. } else {
  153. [_customTitleView removeFromSuperview];
  154. if (readyBlock) {
  155. readyBlock();
  156. }
  157. }
  158. }
  159. - (void)setTitleViewWithSubtitle:(RNNNavigationOptions *)options {
  160. if (!_customTitleView) {
  161. _titleViewHelper = [[RNNTitleViewHelper alloc] initWithTitleViewOptions:options.topBar.title subTitleOptions:options.topBar.subtitle viewController:self.boundViewController];
  162. if (options.topBar.title.text.hasValue) {
  163. [_titleViewHelper setTitleOptions:options.topBar.title];
  164. }
  165. if (options.topBar.subtitle.text.hasValue) {
  166. [_titleViewHelper setSubtitleOptions:options.topBar.subtitle];
  167. }
  168. [_titleViewHelper setup];
  169. } else {
  170. _titleViewHelper = [[RNNTitleViewHelper alloc] initWithTitleViewOptions:options.topBar.title subTitleOptions:options.topBar.subtitle viewController:self.boundViewController];
  171. if (options.topBar.title.text.hasValue) {
  172. [_titleViewHelper setTitleOptions:options.topBar.title];
  173. }
  174. if (options.topBar.subtitle.text.hasValue) {
  175. [_titleViewHelper setSubtitleOptions:options.topBar.subtitle];
  176. }
  177. [_titleViewHelper setup];
  178. }
  179. }
  180. - (void)dealloc {
  181. [_componentRegistry clearComponentsForParentId:self.boundComponentId];
  182. }
  183. @end