react-native-navigation的迁移库

RNNViewControllerPresenter.m 9.9KB

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