react-native-navigation的迁移库

RNNViewControllerPresenter.m 9.5KB

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