react-native-navigation的迁移库

RNNStackPresenter.m 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #import "RNNStackPresenter.h"
  2. #import "UINavigationController+RNNOptions.h"
  3. #import "RNNStackController.h"
  4. #import "RNNCustomTitleView.h"
  5. #import "TopBarPresenterCreator.h"
  6. #import "RNNReactBackgroundView.h"
  7. @interface RNNStackPresenter() {
  8. RNNReactComponentRegistry* _componentRegistry;
  9. UIView* _customTopBarBackground;
  10. RNNReactView* _topBarBackgroundReactView;
  11. TopBarPresenter* _topBarPresenter;
  12. }
  13. @property (nonatomic, weak) RNNStackController* stackController;
  14. @end
  15. @implementation RNNStackPresenter
  16. - (instancetype)initWithComponentRegistry:(RNNReactComponentRegistry *)componentRegistry defaultOptions:(RNNNavigationOptions *)defaultOptions {
  17. self = [super initWithDefaultOptions:defaultOptions];
  18. _componentRegistry = componentRegistry;
  19. return self;
  20. }
  21. - (void)bindViewController:(UIViewController *)boundViewController {
  22. [super bindViewController:boundViewController];
  23. _topBarPresenter = [TopBarPresenterCreator createWithBoundedNavigationController:self.stackController];
  24. }
  25. - (void)componentDidAppear {
  26. [_topBarBackgroundReactView componentDidAppear];
  27. }
  28. - (void)componentDidDisappear {
  29. [_topBarBackgroundReactView componentDidDisappear];
  30. }
  31. - (RNNStackController *)stackController {
  32. return (RNNStackController *)self.boundViewController;
  33. }
  34. - (void)applyOptions:(RNNNavigationOptions *)options {
  35. [super applyOptions:options];
  36. RNNStackController* stack = self.stackController;
  37. RNNNavigationOptions * withDefault = [options withDefault:[self defaultOptions]];
  38. self.interactivePopGestureDelegate = [InteractivePopGestureDelegate new];
  39. self.interactivePopGestureDelegate.navigationController = stack;
  40. self.interactivePopGestureDelegate.originalDelegate = stack.interactivePopGestureRecognizer.delegate;
  41. stack.interactivePopGestureRecognizer.delegate = self.interactivePopGestureDelegate;
  42. [stack setBarStyle:[RCTConvert UIBarStyle:[withDefault.topBar.barStyle getWithDefaultValue:@"default"]]];
  43. [stack setInteractivePopGestureEnabled:[withDefault.popGesture getWithDefaultValue:YES]];
  44. [stack setRootBackgroundImage:[withDefault.rootBackgroundImage getWithDefaultValue:nil]];
  45. [stack setNavigationBarTestId:[withDefault.topBar.testID getWithDefaultValue:nil]];
  46. [stack setNavigationBarVisible:[withDefault.topBar.visible getWithDefaultValue:YES] animated:[withDefault.topBar.animate getWithDefaultValue:YES]];
  47. [stack hideBarsOnScroll:[withDefault.topBar.hideOnScroll getWithDefaultValue:NO]];
  48. [_topBarPresenter applyOptions:withDefault.topBar];
  49. [stack setNavigationBarBlur:[withDefault.topBar.background.blur getWithDefaultValue:NO]];
  50. [stack setNavigationBarLargeTitleVisible:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
  51. [stack setNavigationBarClipsToBounds:[withDefault.topBar.background.clipToBounds getWithDefaultValue:NO]];
  52. [stack setBackButtonColor:[withDefault.topBar.backButton.color getWithDefaultValue:nil]];
  53. }
  54. - (void)applyOptionsOnViewDidLayoutSubviews:(RNNNavigationOptions *)options {
  55. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  56. if (withDefault.topBar.background.component.name.hasValue) {
  57. [self presentBackgroundComponent];
  58. }
  59. }
  60. - (void)applyOptionsBeforePopping:(RNNNavigationOptions *)options {
  61. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  62. RNNStackController* navigationController = self.stackController;
  63. [navigationController setNavigationBarLargeTitleVisible:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
  64. [_topBarPresenter applyOptionsBeforePopping:options.topBar];
  65. }
  66. - (void)mergeOptions:(RNNNavigationOptions *)options resolvedOptions:(RNNNavigationOptions *)resolvedOptions {
  67. [super mergeOptions:options resolvedOptions:resolvedOptions];
  68. RNNStackController* stack = self.stackController;
  69. if (options.popGesture.hasValue) {
  70. [stack setInteractivePopGestureEnabled:options.popGesture.get];
  71. }
  72. if (options.rootBackgroundImage.hasValue) {
  73. [stack setRootBackgroundImage:options.rootBackgroundImage.get];
  74. }
  75. if (options.topBar.testID.hasValue) {
  76. [stack setNavigationBarTestId:options.topBar.testID.get];
  77. }
  78. if (options.topBar.visible.hasValue) {
  79. [stack setNavigationBarVisible:options.topBar.visible.get animated:[options.topBar.animate getWithDefaultValue:YES]];
  80. }
  81. if (options.topBar.hideOnScroll.hasValue) {
  82. [stack hideBarsOnScroll:[options.topBar.hideOnScroll get]];
  83. }
  84. if (options.topBar.barStyle.hasValue) {
  85. [stack setBarStyle:[RCTConvert UIBarStyle:options.topBar.barStyle.get]];
  86. }
  87. if (options.topBar.background.clipToBounds.hasValue) {
  88. [stack setNavigationBarClipsToBounds:[options.topBar.background.clipToBounds get]];
  89. }
  90. if (options.topBar.background.blur.hasValue) {
  91. [stack setNavigationBarBlur:[options.topBar.background.blur get]];
  92. }
  93. if (options.topBar.largeTitle.visible.hasValue) {
  94. [stack setNavigationBarLargeTitleVisible:options.topBar.largeTitle.visible.get];
  95. }
  96. if (options.topBar.backButton.color.hasValue) {
  97. [stack setBackButtonColor:options.topBar.backButton.color.get];
  98. }
  99. if (options.topBar.background.component.name.hasValue) {
  100. [self setCustomNavigationComponentBackground:options perform:nil];
  101. }
  102. RNNNavigationOptions * withDefault = (RNNNavigationOptions *) [[options mergeInOptions:resolvedOptions] withDefault:[self defaultOptions]];
  103. [_topBarPresenter mergeOptions:options.topBar defaultOptions:withDefault.topBar];
  104. }
  105. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  106. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  107. dispatch_group_t group = dispatch_group_create();
  108. dispatch_group_enter(group);
  109. dispatch_async(dispatch_get_main_queue(), ^{
  110. [self setCustomNavigationComponentBackground:options perform:^{
  111. dispatch_group_leave(group);
  112. }];
  113. });
  114. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  115. dispatch_async(dispatch_get_main_queue(), ^{
  116. if (readyBlock) {
  117. readyBlock();
  118. }
  119. });
  120. });
  121. }
  122. - (void)setCustomNavigationComponentBackground:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  123. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  124. RNNStackController* stack = self.stackController;
  125. if (![withDefault.topBar.background.component.waitForRender getWithDefaultValue:NO] && readyBlock) {
  126. readyBlock();
  127. readyBlock = nil;
  128. }
  129. if (withDefault.topBar.background.component.name.hasValue) {
  130. NSString* currentChildComponentId = [stack getCurrentChild].layoutInfo.componentId;
  131. _topBarBackgroundReactView = [_componentRegistry createComponentIfNotExists:withDefault.topBar.background.component parentComponentId:currentChildComponentId componentType:RNNComponentTypeTopBarBackground reactViewReadyBlock:readyBlock];
  132. } else {
  133. [_topBarBackgroundReactView componentDidDisappear];
  134. [_customTopBarBackground removeFromSuperview];
  135. _customTopBarBackground = nil;
  136. if (readyBlock) {
  137. readyBlock();
  138. }
  139. }
  140. }
  141. - (void)presentBackgroundComponent {
  142. RNNStackController* stack = self.stackController;
  143. if (_customTopBarBackground) {
  144. [_customTopBarBackground removeFromSuperview];
  145. }
  146. _customTopBarBackground = [[RNNCustomTitleView alloc] initWithFrame:stack.navigationBar.bounds subView:_topBarBackgroundReactView alignment:@"fill"];
  147. [stack.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  148. [_topBarBackgroundReactView componentDidAppear];
  149. }
  150. - (void)dealloc {
  151. [_componentRegistry removeComponent:self.boundComponentId];
  152. }
  153. @end