react-native-navigation的迁移库

RNNStackPresenter.m 8.2KB

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