react-native-navigation的迁移库

RNNComponentPresenter.m 9.2KB

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