react-native-navigation的迁移库

RNNStackPresenter.m 7.8KB

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 setNavigationBarClipsToBounds:[withDefault.topBar.background.clipToBounds getWithDefaultValue:NO]];
  53. [stack setBackButtonColor:[withDefault.topBar.backButton.color getWithDefaultValue:nil]];
  54. [stack.view setBackgroundColor:[withDefault.layout.backgroundColor 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. [_topBarPresenter applyOptionsBeforePopping:options.topBar];
  66. }
  67. - (void)mergeOptions:(RNNNavigationOptions *)options resolvedOptions:(RNNNavigationOptions *)resolvedOptions {
  68. [super mergeOptions:options resolvedOptions:resolvedOptions];
  69. RNNStackController* stack = self.stackController;
  70. if (options.popGesture.hasValue) {
  71. [_interactivePopGestureDelegate setEnabled:options.popGesture.get];
  72. }
  73. if (options.rootBackgroundImage.hasValue) {
  74. [stack setRootBackgroundImage:options.rootBackgroundImage.get];
  75. }
  76. if (options.topBar.testID.hasValue) {
  77. [stack setNavigationBarTestId:options.topBar.testID.get];
  78. }
  79. if (options.topBar.visible.hasValue) {
  80. [stack setNavigationBarVisible:options.topBar.visible.get animated:[options.topBar.animate getWithDefaultValue:YES]];
  81. }
  82. if (options.topBar.hideOnScroll.hasValue) {
  83. [stack hideBarsOnScroll:[options.topBar.hideOnScroll get]];
  84. }
  85. if (options.topBar.barStyle.hasValue) {
  86. [stack setBarStyle:[RCTConvert UIBarStyle:options.topBar.barStyle.get]];
  87. }
  88. if (options.topBar.background.clipToBounds.hasValue) {
  89. [stack setNavigationBarClipsToBounds:[options.topBar.background.clipToBounds get]];
  90. }
  91. if (options.topBar.background.blur.hasValue) {
  92. [stack setNavigationBarBlur:[options.topBar.background.blur get]];
  93. }
  94. if (options.topBar.backButton.color.hasValue) {
  95. [stack setBackButtonColor:options.topBar.backButton.color.get];
  96. }
  97. if (options.topBar.background.component.name.hasValue) {
  98. [self setCustomNavigationComponentBackground:options perform:nil];
  99. }
  100. if (options.layout.backgroundColor.hasValue) {
  101. [stack.view setBackgroundColor:options.layout.backgroundColor.get];
  102. }
  103. RNNNavigationOptions * withDefault = (RNNNavigationOptions *) [[options mergeInOptions:resolvedOptions] withDefault:[self defaultOptions]];
  104. [_topBarPresenter mergeOptions:options.topBar withDefault:withDefault.topBar];
  105. }
  106. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  107. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  108. dispatch_group_t group = dispatch_group_create();
  109. dispatch_group_enter(group);
  110. dispatch_async(dispatch_get_main_queue(), ^{
  111. [self setCustomNavigationComponentBackground:options perform:^{
  112. dispatch_group_leave(group);
  113. }];
  114. });
  115. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  116. dispatch_async(dispatch_get_main_queue(), ^{
  117. if (readyBlock) {
  118. readyBlock();
  119. }
  120. });
  121. });
  122. }
  123. - (void)setCustomNavigationComponentBackground:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  124. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  125. RNNStackController* stack = self.stackController;
  126. if (![withDefault.topBar.background.component.waitForRender getWithDefaultValue:NO] && readyBlock) {
  127. readyBlock();
  128. readyBlock = nil;
  129. }
  130. if (withDefault.topBar.background.component.name.hasValue) {
  131. NSString* currentChildComponentId = [stack getCurrentChild].layoutInfo.componentId;
  132. _topBarBackgroundReactView = [_componentRegistry createComponentIfNotExists:withDefault.topBar.background.component parentComponentId:currentChildComponentId componentType:RNNComponentTypeTopBarBackground reactViewReadyBlock:readyBlock];
  133. } else {
  134. [_topBarBackgroundReactView componentDidDisappear];
  135. [_customTopBarBackground removeFromSuperview];
  136. _customTopBarBackground = nil;
  137. if (readyBlock) {
  138. readyBlock();
  139. }
  140. }
  141. }
  142. - (void)presentBackgroundComponent {
  143. RNNStackController* stack = self.stackController;
  144. if (_customTopBarBackground) {
  145. [_customTopBarBackground removeFromSuperview];
  146. }
  147. _customTopBarBackground = [[RNNCustomTitleView alloc] initWithFrame:stack.navigationBar.bounds subView:_topBarBackgroundReactView alignment:@"fill"];
  148. [stack.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  149. [_topBarBackgroundReactView componentDidAppear];
  150. }
  151. - (void)dealloc {
  152. [_componentRegistry removeComponent:self.boundComponentId];
  153. }
  154. @end