react-native-navigation的迁移库

RNNComponentPresenter.m 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 setDrawBehindTopBar:[withDefault.topBar.drawBehind getWithDefaultValue:NO]];
  55. [viewController setDrawBehindTabBar:[withDefault.bottomTabs.drawBehind getWithDefaultValue:NO] || ![withDefault.bottomTabs.visible getWithDefaultValue:YES]];
  56. if ((withDefault.topBar.leftButtons || withDefault.topBar.rightButtons)) {
  57. [_navigationButtons applyLeftButtons:withDefault.topBar.leftButtons rightButtons:withDefault.topBar.rightButtons defaultLeftButtonStyle:withDefault.topBar.leftButtonStyle defaultRightButtonStyle:withDefault.topBar.rightButtonStyle];
  58. }
  59. }
  60. - (void)mergeOptions:(RNNNavigationOptions *)options resolvedOptions:(RNNNavigationOptions *)currentOptions {
  61. [super mergeOptions:options resolvedOptions:currentOptions];
  62. RNNNavigationOptions * withDefault = (RNNNavigationOptions *) [[currentOptions overrideOptions:options] withDefault:[self defaultOptions]];
  63. UIViewController* viewController = self.boundViewController;
  64. [self removeTitleComponentIfNeeded:options];
  65. if (options.backgroundImage.hasValue) {
  66. [viewController setBackgroundImage:options.backgroundImage.get];
  67. }
  68. if (options.modalPresentationStyle.hasValue) {
  69. [viewController setModalPresentationStyle:[RCTConvert UIModalPresentationStyle:options.modalPresentationStyle.get]];
  70. }
  71. if (options.modalTransitionStyle.hasValue) {
  72. [viewController setModalTransitionStyle:[RCTConvert UIModalTransitionStyle:options.modalTransitionStyle.get]];
  73. }
  74. if (options.topBar.searchBar.hasValue) {
  75. BOOL hideNavBarOnFocusSearchBar = YES;
  76. if (options.topBar.hideNavBarOnFocusSearchBar.hasValue) {
  77. hideNavBarOnFocusSearchBar = options.topBar.hideNavBarOnFocusSearchBar.get;
  78. }
  79. [viewController setSearchBarWithPlaceholder:[options.topBar.searchBarPlaceholder getWithDefaultValue:@""] hideNavBarOnFocusSearchBar:hideNavBarOnFocusSearchBar];
  80. }
  81. if (options.topBar.drawBehind.hasValue) {
  82. [viewController setDrawBehindTopBar:options.topBar.drawBehind.get];
  83. }
  84. if (options.topBar.title.text.hasValue) {
  85. [viewController setNavigationItemTitle:options.topBar.title.text.get];
  86. }
  87. if (options.topBar.largeTitle.visible.hasValue) {
  88. [viewController setTopBarPrefersLargeTitle:options.topBar.largeTitle.visible.get];
  89. }
  90. if (options.bottomTabs.drawBehind.hasValue) {
  91. [viewController setDrawBehindTabBar:options.bottomTabs.drawBehind.get];
  92. }
  93. if (options.bottomTab.badgeColor.hasValue) {
  94. [viewController setTabBarItemBadgeColor:options.bottomTab.badgeColor.get];
  95. }
  96. if (options.layout.backgroundColor.hasValue) {
  97. [viewController setBackgroundColor:options.layout.backgroundColor.get];
  98. }
  99. if (options.bottomTab.visible.hasValue) {
  100. [viewController.tabBarController setCurrentTabIndex:[viewController.tabBarController.viewControllers indexOfObject:viewController]];
  101. }
  102. if (options.statusBar.blur.hasValue) {
  103. [viewController setStatusBarBlur:options.statusBar.blur.get];
  104. }
  105. if (options.statusBar.style.hasValue) {
  106. [viewController setStatusBarStyle:options.statusBar.style.get animated:[withDefault.statusBar.animate getWithDefaultValue:YES]];
  107. }
  108. if (options.topBar.backButton.visible.hasValue) {
  109. [viewController setBackButtonVisible:options.topBar.backButton.visible.get];
  110. }
  111. if (options.topBar.leftButtons || options.topBar.rightButtons) {
  112. [_navigationButtons applyLeftButtons:options.topBar.leftButtons rightButtons:options.topBar.rightButtons defaultLeftButtonStyle:withDefault.topBar.leftButtonStyle defaultRightButtonStyle:withDefault.topBar.rightButtonStyle];
  113. }
  114. if (options.overlay.interceptTouchOutside.hasValue) {
  115. RCTRootView* rootView = (RCTRootView*)viewController.view;
  116. rootView.passThroughTouches = !options.overlay.interceptTouchOutside.get;
  117. }
  118. if (options.topBar.title.component.name.hasValue) {
  119. [self setCustomNavigationTitleView:options perform:nil];
  120. }
  121. [self setTitleViewWithSubtitle:withDefault];
  122. }
  123. - (void)removeTitleComponentIfNeeded:(RNNNavigationOptions *)options {
  124. if (options.topBar.title.text.hasValue && !options.topBar.component.hasValue) {
  125. [_customTitleView removeFromSuperview];
  126. _customTitleView = nil;
  127. }
  128. }
  129. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  130. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  131. [self setCustomNavigationTitleView:withDefault perform:readyBlock];
  132. }
  133. - (void)setCustomNavigationTitleView:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  134. UIViewController<RNNLayoutProtocol>* viewController = self.boundViewController;
  135. if (![options.topBar.title.component.waitForRender getWithDefaultValue:NO] && readyBlock) {
  136. readyBlock();
  137. readyBlock = nil;
  138. }
  139. if (options.topBar.title.component.name.hasValue) {
  140. _customTitleView = [_componentRegistry createComponentIfNotExists:options.topBar.title.component parentComponentId:viewController.layoutInfo.componentId reactViewReadyBlock:readyBlock];
  141. _customTitleView.backgroundColor = UIColor.clearColor;
  142. NSString* alignment = [options.topBar.title.component.alignment getWithDefaultValue:@""];
  143. [_customTitleView setAlignment:alignment inFrame:viewController.navigationController.navigationBar.frame];
  144. [_customTitleView layoutIfNeeded];
  145. viewController.navigationItem.titleView = nil;
  146. viewController.navigationItem.titleView = _customTitleView;
  147. } else {
  148. [_customTitleView removeFromSuperview];
  149. if (readyBlock) {
  150. readyBlock();
  151. }
  152. }
  153. }
  154. - (void)setTitleViewWithSubtitle:(RNNNavigationOptions *)options {
  155. if (!_customTitleView && ![options.topBar.largeTitle.visible getWithDefaultValue:NO]) {
  156. _titleViewHelper = [[RNNTitleViewHelper alloc] initWithTitleViewOptions:options.topBar.title subTitleOptions:options.topBar.subtitle viewController:self.boundViewController];
  157. if (options.topBar.title.text.hasValue) {
  158. [_titleViewHelper setTitleOptions:options.topBar.title];
  159. }
  160. if (options.topBar.subtitle.text.hasValue) {
  161. [_titleViewHelper setSubtitleOptions:options.topBar.subtitle];
  162. }
  163. [_titleViewHelper setup];
  164. }
  165. }
  166. - (void)dealloc {
  167. [_componentRegistry clearComponentsForParentId:self.boundComponentId];
  168. }
  169. @end