react-native-navigation的迁移库

RNNStackPresenter.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #import "RNNStackPresenter.h"
  2. #import "UINavigationController+RNNOptions.h"
  3. #import "RNNStackController.h"
  4. #import "RNNCustomTitleView.h"
  5. @interface RNNStackPresenter() {
  6. RNNReactComponentRegistry* _componentRegistry;
  7. UIView* _customTopBar;
  8. UIView* _customTopBarBackground;
  9. RNNReactView* _customTopBarBackgroundReactView;
  10. }
  11. @end
  12. @implementation RNNStackPresenter
  13. - (instancetype)initWithComponentRegistry:(RNNReactComponentRegistry *)componentRegistry:(RNNNavigationOptions *)defaultOptions {
  14. self = [super initWithDefaultOptions:defaultOptions];
  15. _componentRegistry = componentRegistry;
  16. return self;
  17. }
  18. - (void)applyOptions:(RNNNavigationOptions *)options {
  19. [super applyOptions:options];
  20. RNNStackController* stack = self.boundViewController;
  21. RNNNavigationOptions * withDefault = [options withDefault:[self defaultOptions]];
  22. self.interactivePopGestureDelegate = [InteractivePopGestureDelegate new];
  23. self.interactivePopGestureDelegate.navigationController = stack;
  24. self.interactivePopGestureDelegate.originalDelegate = stack.interactivePopGestureRecognizer.delegate;
  25. stack.interactivePopGestureRecognizer.delegate = self.interactivePopGestureDelegate;
  26. [stack setInteractivePopGestureEnabled:[withDefault.popGesture getWithDefaultValue:YES]];
  27. [stack setRootBackgroundImage:[withDefault.rootBackgroundImage getWithDefaultValue:nil]];
  28. [stack setNavigationBarTestId:[withDefault.topBar.testID getWithDefaultValue:nil]];
  29. [stack setNavigationBarVisible:[withDefault.topBar.visible getWithDefaultValue:YES] animated:[withDefault.topBar.animate getWithDefaultValue:YES]];
  30. [stack hideBarsOnScroll:[withDefault.topBar.hideOnScroll getWithDefaultValue:NO]];
  31. [stack setNavigationBarNoBorder:[withDefault.topBar.noBorder getWithDefaultValue:NO]];
  32. [stack setBarStyle:[RCTConvert UIBarStyle:[withDefault.topBar.barStyle getWithDefaultValue:@"default"]]];
  33. [stack setNavigationBarTranslucent:[withDefault.topBar.background.translucent getWithDefaultValue:NO]];
  34. [stack setNavigationBarClipsToBounds:[withDefault.topBar.background.clipToBounds getWithDefaultValue:NO]];
  35. [stack setNavigationBarBlur:[withDefault.topBar.background.blur getWithDefaultValue:NO]];
  36. [stack setTopBarBackgroundColor:[withDefault.topBar.background.color getWithDefaultValue:nil]];
  37. [stack setNavigationBarLargeTitleVisible:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
  38. [stack setNavigationBarLargeTitleFontFamily:[withDefault.topBar.largeTitle.fontFamily getWithDefaultValue:nil] fontSize:[withDefault.topBar.largeTitle.fontSize getWithDefaultValue:nil] fontWeight:[withDefault.topBar.largeTitle.fontWeight getWithDefaultValue:nil] color:[withDefault.topBar.largeTitle.color getWithDefaultValue:nil]];
  39. [stack setNavigationBarFontFamily:[withDefault.topBar.title.fontFamily getWithDefaultValue:nil] fontSize:[withDefault.topBar.title.fontSize getWithDefaultValue:nil] fontWeight:[withDefault.topBar.title.fontWeight getWithDefaultValue:nil] color:[withDefault.topBar.title.color getWithDefaultValue:nil]];
  40. [stack setBackButtonColor:[withDefault.topBar.backButton.color getWithDefaultValue:nil]];
  41. }
  42. - (void)applyOptionsOnViewDidLayoutSubviews:(RNNNavigationOptions *)options {
  43. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  44. if (withDefault.topBar.background.component.name.hasValue) {
  45. [self presentBackgroundComponent];
  46. }
  47. }
  48. - (void)applyOptionsBeforePopping:(RNNNavigationOptions *)options {
  49. RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
  50. RNNStackController* navigationController = self.boundViewController;
  51. [navigationController setTopBarBackgroundColor:[withDefault.topBar.background.color getWithDefaultValue:nil]];
  52. [navigationController setNavigationBarFontFamily:[withDefault.topBar.title.fontFamily getWithDefaultValue:nil] fontSize:[withDefault.topBar.title.fontSize getWithDefaultValue:nil] fontWeight:[withDefault.topBar.title.fontWeight getWithDefaultValue:nil] color:[withDefault.topBar.title.color getWithDefaultValue:[UIColor blackColor]]];
  53. [navigationController setNavigationBarLargeTitleVisible:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
  54. }
  55. - (void)mergeOptions:(RNNNavigationOptions *)options resolvedOptions:(RNNNavigationOptions *)resolvedOptions {
  56. [super mergeOptions:options resolvedOptions:resolvedOptions];
  57. RNNStackController* stack = self.boundViewController;
  58. if (options.popGesture.hasValue) {
  59. [stack setInteractivePopGestureEnabled:options.popGesture.get];
  60. }
  61. if (options.rootBackgroundImage.hasValue) {
  62. [stack setRootBackgroundImage:options.rootBackgroundImage.get];
  63. }
  64. if (options.topBar.testID.hasValue) {
  65. [stack setNavigationBarTestId:options.topBar.testID.get];
  66. }
  67. if (options.topBar.visible.hasValue) {
  68. [stack setNavigationBarVisible:options.topBar.visible.get animated:[options.topBar.animate getWithDefaultValue:YES]];
  69. }
  70. if (options.topBar.hideOnScroll.hasValue) {
  71. [stack hideBarsOnScroll:[options.topBar.hideOnScroll get]];
  72. }
  73. if (options.topBar.noBorder.hasValue) {
  74. [stack setNavigationBarNoBorder:[options.topBar.noBorder get]];
  75. }
  76. if (options.topBar.barStyle.hasValue) {
  77. [stack setBarStyle:[RCTConvert UIBarStyle:options.topBar.barStyle.get]];
  78. }
  79. if (options.topBar.background.translucent.hasValue) {
  80. [stack setNavigationBarTranslucent:[options.topBar.background.translucent get]];
  81. }
  82. if (options.topBar.background.clipToBounds.hasValue) {
  83. [stack setNavigationBarClipsToBounds:[options.topBar.background.clipToBounds get]];
  84. }
  85. if (options.topBar.background.blur.hasValue) {
  86. [stack setNavigationBarBlur:[options.topBar.background.blur get]];
  87. }
  88. if (options.topBar.background.color.hasValue) {
  89. [stack setTopBarBackgroundColor:options.topBar.background.color.get];
  90. }
  91. if (options.topBar.largeTitle.visible.hasValue) {
  92. [stack setNavigationBarLargeTitleVisible:options.topBar.largeTitle.visible.get];
  93. }
  94. if (options.topBar.backButton.color.hasValue) {
  95. [stack setBackButtonColor:options.topBar.backButton.color.get];
  96. }
  97. RNNLargeTitleOptions *largeTitleOptions = options.topBar.largeTitle;
  98. if (largeTitleOptions.color.hasValue || largeTitleOptions.fontSize.hasValue || largeTitleOptions.fontFamily.hasValue) {
  99. [stack setNavigationBarLargeTitleFontFamily:[options.topBar.largeTitle.fontFamily getWithDefaultValue:nil] fontSize:[options.topBar.largeTitle.fontSize getWithDefaultValue:nil] fontWeight:[options.topBar.largeTitle.fontWeight getWithDefaultValue:nil] color:[options.topBar.largeTitle.color getWithDefaultValue:nil]];
  100. }
  101. RNNNavigationOptions * withDefault = (RNNNavigationOptions *) [[options mergeInOptions:resolvedOptions] withDefault:[self defaultOptions]];
  102. [stack setNavigationBarFontFamily:[withDefault.topBar.title.fontFamily getWithDefaultValue:nil]
  103. fontSize:[withDefault.topBar.title.fontSize getWithDefaultValue:nil]
  104. fontWeight:[withDefault.topBar.title.fontWeight getWithDefaultValue:nil]
  105. color:[withDefault.topBar.title.color getWithDefaultValue:nil]];
  106. if (options.topBar.component.name.hasValue) {
  107. [self setCustomNavigationBarView:options perform:nil];
  108. }
  109. if (options.topBar.background.component.name.hasValue) {
  110. [self setCustomNavigationComponentBackground:options perform:nil];
  111. }
  112. }
  113. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  114. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  115. dispatch_group_t group = dispatch_group_create();
  116. dispatch_group_enter(group);
  117. dispatch_async(dispatch_get_main_queue(), ^{
  118. [self setCustomNavigationBarView:options perform:^{
  119. dispatch_group_leave(group);
  120. }];
  121. });
  122. dispatch_group_enter(group);
  123. dispatch_async(dispatch_get_main_queue(), ^{
  124. [self setCustomNavigationComponentBackground:options perform:^{
  125. dispatch_group_leave(group);
  126. }];
  127. });
  128. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  129. dispatch_async(dispatch_get_main_queue(), ^{
  130. if (readyBlock) {
  131. readyBlock();
  132. }
  133. });
  134. });
  135. }
  136. - (void)setCustomNavigationBarView:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  137. RNNStackController* stack = self.boundViewController;
  138. if (![options.topBar.component.waitForRender getWithDefaultValue:NO] && readyBlock) {
  139. readyBlock();
  140. readyBlock = nil;
  141. }
  142. if (options.topBar.component.name.hasValue) {
  143. NSString* currentChildComponentId = [stack getCurrentChild].layoutInfo.componentId;
  144. RCTRootView *reactView = [_componentRegistry createComponentIfNotExists:options.topBar.component parentComponentId:currentChildComponentId reactViewReadyBlock:readyBlock];
  145. if (_customTopBar) {
  146. [_customTopBar removeFromSuperview];
  147. }
  148. _customTopBar = [[RNNCustomTitleView alloc] initWithFrame:stack.navigationBar.bounds subView:reactView alignment:@"fill"];
  149. reactView.backgroundColor = UIColor.clearColor;
  150. _customTopBar.backgroundColor = UIColor.clearColor;
  151. [stack.navigationBar addSubview:_customTopBar];
  152. } else {
  153. [_customTopBar removeFromSuperview];
  154. _customTopBar = nil;
  155. if (readyBlock) {
  156. readyBlock();
  157. }
  158. }
  159. }
  160. - (void)setCustomNavigationComponentBackground:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  161. RNNStackController* stack = self.boundViewController;
  162. if (![options.topBar.background.component.waitForRender getWithDefaultValue:NO] && readyBlock) {
  163. readyBlock();
  164. readyBlock = nil;
  165. }
  166. if (options.topBar.background.component.name.hasValue) {
  167. NSString* currentChildComponentId = [stack getCurrentChild].layoutInfo.componentId;
  168. RNNReactView *reactView = [_componentRegistry createComponentIfNotExists:options.topBar.background.component parentComponentId:currentChildComponentId reactViewReadyBlock:readyBlock];
  169. _customTopBarBackgroundReactView = reactView;
  170. } else {
  171. [_customTopBarBackground removeFromSuperview];
  172. _customTopBarBackground = nil;
  173. if (readyBlock) {
  174. readyBlock();
  175. }
  176. }
  177. }
  178. - (void)presentBackgroundComponent {
  179. RNNStackController* stack = self.boundViewController;
  180. if (_customTopBarBackground) {
  181. [_customTopBarBackground removeFromSuperview];
  182. }
  183. RNNCustomTitleView* customTopBarBackground = [[RNNCustomTitleView alloc] initWithFrame:stack.navigationBar.bounds subView:_customTopBarBackgroundReactView alignment:@"fill"];
  184. _customTopBarBackground = customTopBarBackground;
  185. [stack.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  186. }
  187. - (void)dealloc {
  188. [_componentRegistry removeComponent:self.boundComponentId];
  189. }
  190. @end