react-native-navigation的迁移库

RNNStackPresenter.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 setTopBarBackgroundColor:[withDefault.topBar.background.color getWithDefaultValue:nil]];
  27. [stack setInteractivePopGestureEnabled:[withDefault.popGesture getWithDefaultValue:YES]];
  28. [stack setRootBackgroundImage:[withDefault.rootBackgroundImage getWithDefaultValue:nil]];
  29. [stack setNavigationBarTestId:[withDefault.topBar.testID getWithDefaultValue:nil]];
  30. [stack setNavigationBarVisible:[withDefault.topBar.visible getWithDefaultValue:YES] animated:[withDefault.topBar.animate getWithDefaultValue:YES]];
  31. [stack hideBarsOnScroll:[withDefault.topBar.hideOnScroll getWithDefaultValue:NO]];
  32. [stack setNavigationBarNoBorder:[withDefault.topBar.noBorder getWithDefaultValue:NO]];
  33. [stack setBarStyle:[RCTConvert UIBarStyle:[withDefault.topBar.barStyle getWithDefaultValue:@"default"]]];
  34. [stack setNavigationBarTranslucent:[withDefault.topBar.background.translucent 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. RNNStackController* stack = self.boundViewController;
  167. if (![options.topBar.background.component.waitForRender getWithDefaultValue:NO] && readyBlock) {
  168. readyBlock();
  169. readyBlock = nil;
  170. }
  171. if (options.topBar.background.component.name.hasValue) {
  172. NSString* currentChildComponentId = [stack getCurrentChild].layoutInfo.componentId;
  173. RNNReactView *reactView = [_componentRegistry createComponentIfNotExists:options.topBar.background.component parentComponentId:currentChildComponentId reactViewReadyBlock:readyBlock];
  174. _customTopBarBackgroundReactView = reactView;
  175. } else {
  176. [_customTopBarBackground removeFromSuperview];
  177. _customTopBarBackground = nil;
  178. if (readyBlock) {
  179. readyBlock();
  180. }
  181. }
  182. }
  183. - (void)presentBackgroundComponent {
  184. RNNStackController* stack = self.boundViewController;
  185. if (_customTopBarBackground) {
  186. [_customTopBarBackground removeFromSuperview];
  187. }
  188. RNNCustomTitleView* customTopBarBackground = [[RNNCustomTitleView alloc] initWithFrame:stack.navigationBar.bounds subView:_customTopBarBackgroundReactView alignment:@"fill"];
  189. _customTopBarBackground = customTopBarBackground;
  190. [stack.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  191. }
  192. - (void)dealloc {
  193. [_componentRegistry removeComponent:self.boundComponentId];
  194. }
  195. @end