react-native-navigation的迁移库

RNNStackPresenter.m 7.5KB

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