react-native-navigation的迁移库

RNNComponentPresenter.m 7.6KB

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