react-native-navigation的迁移库

RNNViewControllerPresenter.m 8.9KB

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