react-native-navigation的迁移库

RNNStackPresenter.m 8.0KB

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