react-native-navigation的迁移库

RNNStackPresenter.m 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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] color:[withDefault.topBar.largeTitle.color getWithDefaultValue:nil]];
  39. [stack setNavigationBarFontFamily:[withDefault.topBar.title.fontFamily getWithDefaultValue:nil] fontSize:[withDefault.topBar.title.fontSize 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] 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] 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. color:[withDefault.topBar.title.color getWithDefaultValue:nil]];
  105. if (options.topBar.component.name.hasValue) {
  106. [self setCustomNavigationBarView:options perform:nil];
  107. }
  108. if (options.topBar.background.component.name.hasValue) {
  109. [self setCustomNavigationComponentBackground:options perform:nil];
  110. }
  111. }
  112. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  113. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  114. dispatch_group_t group = dispatch_group_create();
  115. dispatch_group_enter(group);
  116. dispatch_async(dispatch_get_main_queue(), ^{
  117. [self setCustomNavigationBarView:options perform:^{
  118. dispatch_group_leave(group);
  119. }];
  120. });
  121. dispatch_group_enter(group);
  122. dispatch_async(dispatch_get_main_queue(), ^{
  123. [self setCustomNavigationComponentBackground:options perform:^{
  124. dispatch_group_leave(group);
  125. }];
  126. });
  127. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  128. dispatch_async(dispatch_get_main_queue(), ^{
  129. if (readyBlock) {
  130. readyBlock();
  131. }
  132. });
  133. });
  134. }
  135. - (void)setCustomNavigationBarView:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  136. RNNStackController* stack = self.boundViewController;
  137. if (![options.topBar.component.waitForRender getWithDefaultValue:NO] && readyBlock) {
  138. readyBlock();
  139. readyBlock = nil;
  140. }
  141. if (options.topBar.component.name.hasValue) {
  142. NSString* currentChildComponentId = [stack getCurrentChild].layoutInfo.componentId;
  143. RCTRootView *reactView = [_componentRegistry createComponentIfNotExists:options.topBar.component parentComponentId:currentChildComponentId reactViewReadyBlock:readyBlock];
  144. if (_customTopBar) {
  145. [_customTopBar removeFromSuperview];
  146. }
  147. _customTopBar = [[RNNCustomTitleView alloc] initWithFrame:stack.navigationBar.bounds subView:reactView alignment:@"fill"];
  148. reactView.backgroundColor = UIColor.clearColor;
  149. _customTopBar.backgroundColor = UIColor.clearColor;
  150. [stack.navigationBar addSubview:_customTopBar];
  151. } else {
  152. [_customTopBar removeFromSuperview];
  153. _customTopBar = nil;
  154. if (readyBlock) {
  155. readyBlock();
  156. }
  157. }
  158. }
  159. - (void)setCustomNavigationComponentBackground:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  160. RNNStackController* stack = self.boundViewController;
  161. if (![options.topBar.background.component.waitForRender getWithDefaultValue:NO] && readyBlock) {
  162. readyBlock();
  163. readyBlock = nil;
  164. }
  165. if (options.topBar.background.component.name.hasValue) {
  166. NSString* currentChildComponentId = [stack getCurrentChild].layoutInfo.componentId;
  167. RNNReactView *reactView = [_componentRegistry createComponentIfNotExists:options.topBar.background.component parentComponentId:currentChildComponentId reactViewReadyBlock:readyBlock];
  168. _customTopBarBackgroundReactView = reactView;
  169. } else {
  170. [_customTopBarBackground removeFromSuperview];
  171. _customTopBarBackground = nil;
  172. if (readyBlock) {
  173. readyBlock();
  174. }
  175. }
  176. }
  177. - (void)presentBackgroundComponent {
  178. RNNStackController* stack = self.boundViewController;
  179. if (_customTopBarBackground) {
  180. [_customTopBarBackground removeFromSuperview];
  181. }
  182. RNNCustomTitleView* customTopBarBackground = [[RNNCustomTitleView alloc] initWithFrame:stack.navigationBar.bounds subView:_customTopBarBackgroundReactView alignment:@"fill"];
  183. _customTopBarBackground = customTopBarBackground;
  184. [stack.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  185. }
  186. - (void)dealloc {
  187. [_componentRegistry removeComponent:self.boundComponentId];
  188. }
  189. @end