react-native-navigation的迁移库

RNNViewControllerPresenter.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. RNNNavigationOptions * withDefault = (RNNNavigationOptions *) [[currentOptions overrideOptions:newOptions] withDefault:[self defaultOptions]];
  68. UIViewController* viewController = self.boundViewController;
  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:[withDefault.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. [_navigationButtons applyLeftButtons:newOptions.topBar.leftButtons rightButtons:newOptions.topBar.rightButtons defaultLeftButtonStyle:withDefault.topBar.leftButtonStyle defaultRightButtonStyle:withDefault.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:withDefault];
  123. if (newOptions.topBar.title.component.name.hasValue) {
  124. [self setCustomNavigationTitleView:newOptions perform:nil];
  125. } else {
  126. [_customTitleView removeFromSuperview];
  127. _customTitleView = nil;
  128. }
  129. if (newOptions.topBar.backButton.icon.hasValue || newOptions.topBar.backButton.showTitle.hasValue || newOptions.topBar.backButton.color.hasValue || newOptions.topBar.backButton.title.hasValue) {
  130. [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] : @""];
  131. }
  132. }
  133. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  134. [self setCustomNavigationTitleView:options perform:readyBlock];
  135. }
  136. - (void)setCustomNavigationTitleView:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  137. UIViewController<RNNLayoutProtocol>* viewController = self.boundViewController;
  138. if (![options.topBar.title.component.waitForRender getWithDefaultValue:NO] && readyBlock) {
  139. readyBlock();
  140. readyBlock = nil;
  141. }
  142. if (options.topBar.title.component.name.hasValue) {
  143. _customTitleView = [_componentRegistry createComponentIfNotExists:options.topBar.title.component parentComponentId:viewController.layoutInfo.componentId reactViewReadyBlock:readyBlock];
  144. _customTitleView.backgroundColor = UIColor.clearColor;
  145. NSString* alignment = [options.topBar.title.component.alignment getWithDefaultValue:@""];
  146. [_customTitleView setAlignment:alignment inFrame:viewController.navigationController.navigationBar.frame];
  147. viewController.navigationItem.titleView = nil;
  148. viewController.navigationItem.titleView = _customTitleView;
  149. } else {
  150. [_customTitleView removeFromSuperview];
  151. if (readyBlock) {
  152. readyBlock();
  153. }
  154. }
  155. }
  156. - (void)setTitleViewWithSubtitle:(RNNNavigationOptions *)options {
  157. if (!_customTitleView) {
  158. _titleViewHelper = [[RNNTitleViewHelper alloc] initWithTitleViewOptions:options.topBar.title subTitleOptions:options.topBar.subtitle viewController:self.boundViewController];
  159. if (options.topBar.title.text.hasValue) {
  160. [_titleViewHelper setTitleOptions:options.topBar.title];
  161. }
  162. if (options.topBar.subtitle.text.hasValue) {
  163. [_titleViewHelper setSubtitleOptions:options.topBar.subtitle];
  164. }
  165. [_titleViewHelper setup];
  166. } else {
  167. _titleViewHelper = [[RNNTitleViewHelper alloc] initWithTitleViewOptions:options.topBar.title subTitleOptions:options.topBar.subtitle viewController:self.boundViewController];
  168. if (options.topBar.title.text.hasValue) {
  169. [_titleViewHelper setTitleOptions:options.topBar.title];
  170. }
  171. if (options.topBar.subtitle.text.hasValue) {
  172. [_titleViewHelper setSubtitleOptions:options.topBar.subtitle];
  173. }
  174. [_titleViewHelper setup];
  175. }
  176. }
  177. - (void)dealloc {
  178. [_componentRegistry clearComponentsForParentId:self.boundComponentId];
  179. }
  180. @end