react-native-navigation的迁移库

RNNViewControllerPresenter.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #import "RNNViewControllerPresenter.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 RNNViewControllerPresenter() {
  8. RNNReactView* _customTitleView;
  9. RNNTitleViewHelper* _titleViewHelper;
  10. RNNReactComponentRegistry* _componentRegistry;
  11. }
  12. @end
  13. @implementation RNNViewControllerPresenter
  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 rnn_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 rnn_setBackgroundImage:[withDefault.backgroundImage getWithDefaultValue:nil]];
  34. [viewController rnn_setNavigationItemTitle:[withDefault.topBar.title.text getWithDefaultValue:nil]];
  35. [viewController rnn_setTopBarPrefersLargeTitle:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
  36. [viewController rnn_setTabBarItemBadgeColor:[withDefault.bottomTab.badgeColor getWithDefaultValue:nil]];
  37. [viewController rnn_setStatusBarBlur:[withDefault.statusBar.blur getWithDefaultValue:NO]];
  38. [viewController rnn_setStatusBarStyle:[withDefault.statusBar.style getWithDefaultValue:@"default"] animated:[withDefault.statusBar.animate getWithDefaultValue:YES]];
  39. [viewController rnn_setBackButtonVisible:[withDefault.topBar.backButton.visible getWithDefaultValue:YES]];
  40. [viewController rnn_setInterceptTouchOutside:[withDefault.overlay.interceptTouchOutside getWithDefaultValue:YES]];
  41. if (withDefault.layout.backgroundColor.hasValue) {
  42. [viewController rnn_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 rnn_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 rnn_setModalPresentationStyle:[RCTConvert UIModalPresentationStyle:[withDefault.modalPresentationStyle getWithDefaultValue:@"fullScreen"]]];
  58. [viewController rnn_setModalTransitionStyle:[RCTConvert UIModalTransitionStyle:[withDefault.modalTransitionStyle getWithDefaultValue:@"coverVertical"]]];
  59. [viewController rnn_setDrawBehindTopBar:[withDefault.topBar.drawBehind getWithDefaultValue:NO]];
  60. [viewController rnn_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 *)newOptions currentOptions:(RNNNavigationOptions *)currentOptions {
  66. [super mergeOptions:newOptions currentOptions:currentOptions];
  67. UIViewController* viewController = self.boundViewController;
  68. if (newOptions.backgroundImage.hasValue) {
  69. [viewController rnn_setBackgroundImage:newOptions.backgroundImage.get];
  70. }
  71. if (newOptions.modalPresentationStyle.hasValue) {
  72. [viewController rnn_setModalPresentationStyle:[RCTConvert UIModalPresentationStyle:newOptions.modalPresentationStyle.get]];
  73. }
  74. if (newOptions.modalTransitionStyle.hasValue) {
  75. [viewController rnn_setModalTransitionStyle:[RCTConvert UIModalTransitionStyle:newOptions.modalTransitionStyle.get]];
  76. }
  77. if (newOptions.topBar.searchBar.hasValue) {
  78. BOOL hideNavBarOnFocusSearchBar = YES;
  79. if (newOptions.topBar.hideNavBarOnFocusSearchBar.hasValue) {
  80. hideNavBarOnFocusSearchBar = newOptions.topBar.hideNavBarOnFocusSearchBar.get;
  81. }
  82. [viewController rnn_setSearchBarWithPlaceholder:[newOptions.topBar.searchBarPlaceholder getWithDefaultValue:@""] hideNavBarOnFocusSearchBar:hideNavBarOnFocusSearchBar];
  83. }
  84. if (newOptions.topBar.drawBehind.hasValue) {
  85. [viewController rnn_setDrawBehindTopBar:newOptions.topBar.drawBehind.get];
  86. }
  87. if (newOptions.topBar.title.text.hasValue) {
  88. [viewController rnn_setNavigationItemTitle:newOptions.topBar.title.text.get];
  89. }
  90. if (newOptions.topBar.largeTitle.visible.hasValue) {
  91. [viewController rnn_setTopBarPrefersLargeTitle:newOptions.topBar.largeTitle.visible.get];
  92. }
  93. if (newOptions.bottomTabs.drawBehind.hasValue) {
  94. [viewController rnn_setDrawBehindTabBar:newOptions.bottomTabs.drawBehind.get];
  95. }
  96. if (newOptions.bottomTab.badgeColor.hasValue) {
  97. [viewController rnn_setTabBarItemBadgeColor:newOptions.bottomTab.badgeColor.get];
  98. }
  99. if (newOptions.layout.backgroundColor.hasValue) {
  100. [viewController rnn_setBackgroundColor:newOptions.layout.backgroundColor.get];
  101. }
  102. if (newOptions.bottomTab.visible.hasValue) {
  103. [viewController.tabBarController rnn_setCurrentTabIndex:[viewController.tabBarController.viewControllers indexOfObject:viewController]];
  104. }
  105. if (newOptions.statusBar.blur.hasValue) {
  106. [viewController rnn_setStatusBarBlur:newOptions.statusBar.blur.get];
  107. }
  108. if (newOptions.statusBar.style.hasValue) {
  109. [viewController rnn_setStatusBarStyle:newOptions.statusBar.style.get animated:[newOptions.statusBar.animate getWithDefaultValue:YES]];
  110. }
  111. if (newOptions.topBar.backButton.visible.hasValue) {
  112. [viewController rnn_setBackButtonVisible:newOptions.topBar.backButton.visible.get];
  113. }
  114. if (newOptions.topBar.leftButtons || newOptions.topBar.rightButtons) {
  115. RNNNavigationOptions* buttonsResolvedOptions = (RNNNavigationOptions *)[currentOptions overrideOptions:newOptions];
  116. [_navigationButtons applyLeftButtons:newOptions.topBar.leftButtons rightButtons:newOptions.topBar.rightButtons defaultLeftButtonStyle:buttonsResolvedOptions.topBar.leftButtonStyle defaultRightButtonStyle:buttonsResolvedOptions.topBar.rightButtonStyle];
  117. }
  118. if (newOptions.overlay.interceptTouchOutside.hasValue) {
  119. RCTRootView* rootView = (RCTRootView*)viewController.view;
  120. rootView.passThroughTouches = !newOptions.overlay.interceptTouchOutside.get;
  121. }
  122. [self setTitleViewWithSubtitle:(RNNNavigationOptions *)[currentOptions overrideOptions:newOptions]];
  123. if (newOptions.topBar.title.component.name.hasValue) {
  124. [self setCustomNavigationTitleView:newOptions perform:nil];
  125. }
  126. if (newOptions.topBar.backButton.icon.hasValue || newOptions.topBar.backButton.showTitle.hasValue || newOptions.topBar.backButton.color.hasValue || newOptions.topBar.backButton.title.hasValue) {
  127. [viewController rnn_setBackButtonIcon:[newOptions.topBar.backButton.icon getWithDefaultValue:nil] withColor:[newOptions.topBar.backButton.color getWithDefaultValue:nil] title:[newOptions.topBar.backButton.showTitle getWithDefaultValue:YES] ? [newOptions.topBar.backButton.title getWithDefaultValue:nil] : @""];
  128. }
  129. }
  130. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  131. [self setCustomNavigationTitleView:options perform:readyBlock];
  132. }
  133. - (void)setCustomNavigationTitleView:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  134. UIViewController<RNNLayoutProtocol>* viewController = self.boundViewController;
  135. if (![options.topBar.title.component.waitForRender getWithDefaultValue:NO] && readyBlock) {
  136. readyBlock();
  137. readyBlock = nil;
  138. }
  139. if (options.topBar.title.component.name.hasValue) {
  140. _customTitleView = [_componentRegistry createComponentIfNotExists:options.topBar.title.component parentComponentId:viewController.layoutInfo.componentId reactViewReadyBlock:readyBlock];
  141. _customTitleView.backgroundColor = UIColor.clearColor;
  142. NSString* alignment = [options.topBar.title.component.alignment getWithDefaultValue:@""];
  143. [_customTitleView setAlignment:alignment inFrame:viewController.navigationController.navigationBar.frame];
  144. viewController.navigationItem.titleView = nil;
  145. viewController.navigationItem.titleView = _customTitleView;
  146. } else {
  147. [_customTitleView removeFromSuperview];
  148. if (readyBlock) {
  149. readyBlock();
  150. }
  151. }
  152. }
  153. - (void)setTitleViewWithSubtitle:(RNNNavigationOptions *)options {
  154. if (!_customTitleView && options.topBar.subtitle.text.hasValue) {
  155. _titleViewHelper = [[RNNTitleViewHelper alloc] initWithTitleViewOptions:options.topBar.title subTitleOptions:options.topBar.subtitle viewController:self.boundViewController];
  156. [_titleViewHelper setup];
  157. } else if (_titleViewHelper) {
  158. if (options.topBar.title.text.hasValue) {
  159. [_titleViewHelper setTitleOptions:options.topBar.title];
  160. }
  161. if (options.topBar.subtitle.text.hasValue) {
  162. [_titleViewHelper setSubtitleOptions:options.topBar.subtitle];
  163. }
  164. [_titleViewHelper setup];
  165. }
  166. }
  167. - (void)dealloc {
  168. [_componentRegistry clearComponentsForParentId:self.boundComponentId];
  169. }
  170. @end