react-native-navigation的迁移库

RNNComponentPresenter.m 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #import "RNNComponentPresenter.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. #import "RNNReactTitleView.h"
  8. @interface RNNComponentPresenter() {
  9. RNNReactTitleView* _customTitleView;
  10. RNNTitleViewHelper* _titleViewHelper;
  11. RNNReactComponentRegistry* _componentRegistry;
  12. }
  13. @end
  14. @implementation RNNComponentPresenter
  15. - (instancetype)initWithComponentRegistry:(RNNReactComponentRegistry *)componentRegistry:(RNNNavigationOptions *)defaultOptions {
  16. self = [self initWithDefaultOptions:defaultOptions];
  17. _componentRegistry = componentRegistry;
  18. return self;
  19. }
  20. - (void)bindViewController:(id)boundViewController {
  21. [super bindViewController:boundViewController];
  22. _navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:self.boundViewController componentRegistry:_componentRegistry];
  23. }
  24. - (void)componentDidAppear {
  25. RNNReactView* component = (RNNReactView *)self.boundViewController.view;
  26. if ([component respondsToSelector:@selector(componentDidAppear)]) {
  27. [component componentDidAppear];
  28. }
  29. [_customTitleView componentDidAppear];
  30. [_navigationButtons componentDidAppear];
  31. }
  32. - (void)componentDidDisappear {
  33. RNNReactView* component = (RNNReactView *)self.boundViewController.view;
  34. if ([component respondsToSelector:@selector(componentDidDisappear)]) {
  35. [component componentDidDisappear];
  36. }
  37. [_customTitleView componentDidDisappear];
  38. [_navigationButtons componentDidDisappear];
  39. }
  40. - (void)applyOptionsOnWillMoveToParentViewController:(RNNNavigationOptions *)options {
  41. [super applyOptionsOnWillMoveToParentViewController:options];
  42. }
  43. - (void)applyOptions:(RNNNavigationOptions *)options {
  44. [super applyOptions:options];
  45. UIViewController* viewController = self.boundViewController;
  46. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  47. [viewController setBackgroundImage:[withDefault.backgroundImage getWithDefaultValue:nil]];
  48. [viewController setNavigationItemTitle:[withDefault.topBar.title.text getWithDefaultValue:nil]];
  49. [viewController setTopBarPrefersLargeTitle:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
  50. [viewController setTabBarItemBadgeColor:[withDefault.bottomTab.badgeColor getWithDefaultValue:nil]];
  51. [viewController setStatusBarBlur:[withDefault.statusBar.blur getWithDefaultValue:NO]];
  52. [viewController setStatusBarStyle:[withDefault.statusBar.style getWithDefaultValue:@"default"] animated:[withDefault.statusBar.animate getWithDefaultValue:YES]];
  53. [viewController setBackButtonVisible:[withDefault.topBar.backButton.visible getWithDefaultValue:YES]];
  54. [viewController setInterceptTouchOutside:[withDefault.overlay.interceptTouchOutside getWithDefaultValue:YES]];
  55. if (withDefault.layout.backgroundColor.hasValue) {
  56. [viewController setBackgroundColor:withDefault.layout.backgroundColor.get];
  57. }
  58. if (withDefault.topBar.searchBar.hasValue) {
  59. BOOL hideNavBarOnFocusSearchBar = YES;
  60. if (withDefault.topBar.hideNavBarOnFocusSearchBar.hasValue) {
  61. hideNavBarOnFocusSearchBar = withDefault.topBar.hideNavBarOnFocusSearchBar.get;
  62. }
  63. [viewController setSearchBarWithPlaceholder:[withDefault.topBar.searchBarPlaceholder getWithDefaultValue:@""] hideNavBarOnFocusSearchBar:hideNavBarOnFocusSearchBar];
  64. }
  65. [self setTitleViewWithSubtitle:withDefault];
  66. }
  67. - (void)applyOptionsOnInit:(RNNNavigationOptions *)options {
  68. [super applyOptionsOnInit:options];
  69. UIViewController* viewController = self.boundViewController;
  70. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  71. [viewController setDrawBehindTopBar:[withDefault.topBar.drawBehind getWithDefaultValue:NO]];
  72. [viewController setDrawBehindTabBar:[withDefault.bottomTabs.drawBehind getWithDefaultValue:NO] || ![withDefault.bottomTabs.visible getWithDefaultValue:YES]];
  73. if ((withDefault.topBar.leftButtons || withDefault.topBar.rightButtons)) {
  74. [_navigationButtons applyLeftButtons:withDefault.topBar.leftButtons rightButtons:withDefault.topBar.rightButtons defaultLeftButtonStyle:withDefault.topBar.leftButtonStyle defaultRightButtonStyle:withDefault.topBar.rightButtonStyle];
  75. }
  76. }
  77. - (void)mergeOptions:(RNNNavigationOptions *)options resolvedOptions:(RNNNavigationOptions *)currentOptions {
  78. [super mergeOptions:options resolvedOptions:currentOptions];
  79. RNNNavigationOptions * withDefault = (RNNNavigationOptions *) [[currentOptions overrideOptions:options] withDefault:[self defaultOptions]];
  80. UIViewController* viewController = self.boundViewController;
  81. [self removeTitleComponentIfNeeded:options];
  82. if (options.backgroundImage.hasValue) {
  83. [viewController setBackgroundImage:options.backgroundImage.get];
  84. }
  85. if (options.modalPresentationStyle.hasValue) {
  86. [viewController setModalPresentationStyle:[RCTConvert UIModalPresentationStyle:options.modalPresentationStyle.get]];
  87. }
  88. if (options.modalTransitionStyle.hasValue) {
  89. [viewController setModalTransitionStyle:[RCTConvert UIModalTransitionStyle:options.modalTransitionStyle.get]];
  90. }
  91. if (options.topBar.searchBar.hasValue) {
  92. BOOL hideNavBarOnFocusSearchBar = YES;
  93. if (options.topBar.hideNavBarOnFocusSearchBar.hasValue) {
  94. hideNavBarOnFocusSearchBar = options.topBar.hideNavBarOnFocusSearchBar.get;
  95. }
  96. [viewController setSearchBarWithPlaceholder:[options.topBar.searchBarPlaceholder getWithDefaultValue:@""] hideNavBarOnFocusSearchBar:hideNavBarOnFocusSearchBar];
  97. }
  98. if (options.topBar.drawBehind.hasValue) {
  99. [viewController setDrawBehindTopBar:options.topBar.drawBehind.get];
  100. }
  101. if (options.topBar.title.text.hasValue) {
  102. [viewController setNavigationItemTitle:options.topBar.title.text.get];
  103. }
  104. if (options.topBar.largeTitle.visible.hasValue) {
  105. [viewController setTopBarPrefersLargeTitle:options.topBar.largeTitle.visible.get];
  106. }
  107. if (options.bottomTabs.drawBehind.hasValue) {
  108. [viewController setDrawBehindTabBar:options.bottomTabs.drawBehind.get];
  109. }
  110. if (options.bottomTab.badgeColor.hasValue) {
  111. [viewController setTabBarItemBadgeColor:options.bottomTab.badgeColor.get];
  112. }
  113. if (options.layout.backgroundColor.hasValue) {
  114. [viewController setBackgroundColor:options.layout.backgroundColor.get];
  115. }
  116. if (options.bottomTab.visible.hasValue) {
  117. [viewController.tabBarController setCurrentTabIndex:[viewController.tabBarController.viewControllers indexOfObject:viewController]];
  118. }
  119. if (options.statusBar.blur.hasValue) {
  120. [viewController setStatusBarBlur:options.statusBar.blur.get];
  121. }
  122. if (options.statusBar.style.hasValue) {
  123. [viewController setStatusBarStyle:options.statusBar.style.get animated:[withDefault.statusBar.animate getWithDefaultValue:YES]];
  124. }
  125. if (options.topBar.backButton.visible.hasValue) {
  126. [viewController setBackButtonVisible:options.topBar.backButton.visible.get];
  127. }
  128. if (options.topBar.leftButtons || options.topBar.rightButtons) {
  129. [_navigationButtons applyLeftButtons:options.topBar.leftButtons rightButtons:options.topBar.rightButtons defaultLeftButtonStyle:withDefault.topBar.leftButtonStyle defaultRightButtonStyle:withDefault.topBar.rightButtonStyle];
  130. }
  131. if (options.overlay.interceptTouchOutside.hasValue) {
  132. RCTRootView* rootView = (RCTRootView*)viewController.view;
  133. rootView.passThroughTouches = !options.overlay.interceptTouchOutside.get;
  134. }
  135. if (options.topBar.title.component.name.hasValue) {
  136. [self setCustomNavigationTitleView:options perform:nil];
  137. }
  138. [self setTitleViewWithSubtitle:withDefault];
  139. }
  140. - (void)removeTitleComponentIfNeeded:(RNNNavigationOptions *)options {
  141. if (options.topBar.title.text.hasValue) {
  142. [_customTitleView componentDidDisappear];
  143. [_customTitleView removeFromSuperview];
  144. _customTitleView = nil;
  145. }
  146. }
  147. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  148. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  149. [self setCustomNavigationTitleView:withDefault perform:readyBlock];
  150. }
  151. - (void)setCustomNavigationTitleView:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  152. UIViewController<RNNLayoutProtocol>* viewController = self.boundViewController;
  153. if (![options.topBar.title.component.waitForRender getWithDefaultValue:NO] && readyBlock) {
  154. readyBlock();
  155. readyBlock = nil;
  156. }
  157. if (options.topBar.title.component.name.hasValue) {
  158. _customTitleView = (RNNReactTitleView *)[_componentRegistry createComponentIfNotExists:options.topBar.title.component parentComponentId:viewController.layoutInfo.componentId componentType:RNNComponentTypeTopBarTitle reactViewReadyBlock:readyBlock];
  159. _customTitleView.backgroundColor = UIColor.clearColor;
  160. NSString* alignment = [options.topBar.title.component.alignment getWithDefaultValue:@""];
  161. [_customTitleView setAlignment:alignment inFrame:viewController.navigationController.navigationBar.frame];
  162. [_customTitleView layoutIfNeeded];
  163. viewController.navigationItem.titleView = nil;
  164. viewController.navigationItem.titleView = _customTitleView;
  165. [_customTitleView componentDidAppear];
  166. } else {
  167. [_customTitleView removeFromSuperview];
  168. if (readyBlock) {
  169. readyBlock();
  170. }
  171. }
  172. }
  173. - (void)setTitleViewWithSubtitle:(RNNNavigationOptions *)options {
  174. if (!_customTitleView && ![options.topBar.largeTitle.visible getWithDefaultValue:NO]) {
  175. _titleViewHelper = [[RNNTitleViewHelper alloc] initWithTitleViewOptions:options.topBar.title subTitleOptions:options.topBar.subtitle viewController:self.boundViewController];
  176. if (options.topBar.title.text.hasValue) {
  177. [_titleViewHelper setTitleOptions:options.topBar.title];
  178. }
  179. if (options.topBar.subtitle.text.hasValue) {
  180. [_titleViewHelper setSubtitleOptions:options.topBar.subtitle];
  181. }
  182. [_titleViewHelper setup];
  183. }
  184. }
  185. - (void)dealloc {
  186. [_componentRegistry clearComponentsForParentId:self.boundComponentId];
  187. }
  188. @end