react-native-navigation的迁移库

RNNNavigationControllerPresenter.m 11KB

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