react-native-navigation的迁移库

RNNStackPresenter.m 11KB

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