react-native-navigation的迁移库

RNNStackPresenter.m 7.7KB

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