react-native-navigation的迁移库

RNNNavigationControllerPresenter.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #import "RNNNavigationControllerPresenter.h"
  2. #import "UINavigationController+RNNOptions.h"
  3. #import "RNNNavigationController.h"
  4. #import "RNNCustomTitleView.h"
  5. @interface RNNNavigationControllerPresenter() {
  6. RNNReactComponentRegistry* _componentRegistry;
  7. UIView* _customTopBar;
  8. UIView* _customTopBarBackground;
  9. RNNReactView* _customTopBarBackgroundReactView;
  10. }
  11. @end
  12. @implementation RNNNavigationControllerPresenter
  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. RNNNavigationController* navigationController = self.boundViewController;
  21. RNNNavigationOptions * withDefault = [options withDefault:[self defaultOptions]];
  22. self.interactivePopGestureDelegate = [InteractivePopGestureDelegate new];
  23. self.interactivePopGestureDelegate.navigationController = navigationController;
  24. self.interactivePopGestureDelegate.originalDelegate = navigationController.interactivePopGestureRecognizer.delegate;
  25. navigationController.interactivePopGestureRecognizer.delegate = self.interactivePopGestureDelegate;
  26. [navigationController rnn_setInteractivePopGestureEnabled:[withDefault.popGesture getWithDefaultValue:YES]];
  27. [navigationController rnn_setRootBackgroundImage:[withDefault.rootBackgroundImage getWithDefaultValue:nil]];
  28. [navigationController rnn_setNavigationBarTestID:[withDefault.topBar.testID getWithDefaultValue:nil]];
  29. [navigationController rnn_setNavigationBarVisible:[withDefault.topBar.visible getWithDefaultValue:YES] animated:[withDefault.topBar.animate getWithDefaultValue:YES]];
  30. [navigationController rnn_hideBarsOnScroll:[withDefault.topBar.hideOnScroll getWithDefaultValue:NO]];
  31. [navigationController rnn_setNavigationBarNoBorder:[withDefault.topBar.noBorder getWithDefaultValue:NO]];
  32. [navigationController rnn_setBarStyle:[RCTConvert UIBarStyle:[withDefault.topBar.barStyle getWithDefaultValue:@"default"]]];
  33. [navigationController rnn_setNavigationBarTranslucent:[withDefault.topBar.background.translucent getWithDefaultValue:NO]];
  34. [navigationController rnn_setNavigationBarClipsToBounds:[withDefault.topBar.background.clipToBounds getWithDefaultValue:NO]];
  35. [navigationController rnn_setNavigationBarBlur:[withDefault.topBar.background.blur getWithDefaultValue:NO]];
  36. [navigationController setTopBarBackgroundColor:[withDefault.topBar.background.color getWithDefaultValue:nil]];
  37. [navigationController rnn_setNavigationBarLargeTitleVisible:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
  38. [navigationController rnn_setNavigationBarLargeTitleFontFamily:[withDefault.topBar.largeTitle.fontFamily getWithDefaultValue:nil] fontSize:[withDefault.topBar.largeTitle.fontSize getWithDefaultValue:nil] color:[withDefault.topBar.largeTitle.color getWithDefaultValue:nil]];
  39. [navigationController rnn_setNavigationBarFontFamily:[withDefault.topBar.title.fontFamily getWithDefaultValue:nil] fontSize:[withDefault.topBar.title.fontSize getWithDefaultValue:nil] color:[withDefault.topBar.title.color getWithDefaultValue:nil]];
  40. [navigationController rnn_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. RNNNavigationController* navigationController = self.boundViewController;
  51. [navigationController setTopBarBackgroundColor:[withDefault.topBar.background.color getWithDefaultValue:nil]];
  52. [navigationController rnn_setNavigationBarFontFamily:[withDefault.topBar.title.fontFamily getWithDefaultValue:nil] fontSize:[withDefault.topBar.title.fontSize getWithDefaultValue:nil] color:[withDefault.topBar.title.color getWithDefaultValue:[UIColor blackColor]]];
  53. [navigationController rnn_setNavigationBarLargeTitleVisible:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
  54. }
  55. - (void)mergeOptions:(RNNNavigationOptions *)newOptions currentOptions:(RNNNavigationOptions *)currentOptions {
  56. [super mergeOptions:newOptions currentOptions:currentOptions];
  57. RNNNavigationController* navigationController = self.boundViewController;
  58. if (newOptions.popGesture.hasValue) {
  59. [navigationController rnn_setInteractivePopGestureEnabled:newOptions.popGesture.get];
  60. }
  61. if (newOptions.rootBackgroundImage.hasValue) {
  62. [navigationController rnn_setRootBackgroundImage:newOptions.rootBackgroundImage.get];
  63. }
  64. if (newOptions.topBar.testID.hasValue) {
  65. [navigationController rnn_setNavigationBarTestID:newOptions.topBar.testID.get];
  66. }
  67. if (newOptions.topBar.visible.hasValue) {
  68. [navigationController rnn_setNavigationBarVisible:newOptions.topBar.visible.get animated:[newOptions.topBar.animate getWithDefaultValue:YES]];
  69. }
  70. if (newOptions.topBar.hideOnScroll.hasValue) {
  71. [navigationController rnn_hideBarsOnScroll:[newOptions.topBar.hideOnScroll get]];
  72. }
  73. if (newOptions.topBar.noBorder.hasValue) {
  74. [navigationController rnn_setNavigationBarNoBorder:[newOptions.topBar.noBorder get]];
  75. }
  76. if (newOptions.topBar.barStyle.hasValue) {
  77. [navigationController rnn_setBarStyle:[RCTConvert UIBarStyle:newOptions.topBar.barStyle.get]];
  78. }
  79. if (newOptions.topBar.background.translucent.hasValue) {
  80. [navigationController rnn_setNavigationBarTranslucent:[newOptions.topBar.background.translucent get]];
  81. }
  82. if (newOptions.topBar.background.clipToBounds.hasValue) {
  83. [navigationController rnn_setNavigationBarClipsToBounds:[newOptions.topBar.background.clipToBounds get]];
  84. }
  85. if (newOptions.topBar.background.blur.hasValue) {
  86. [navigationController rnn_setNavigationBarBlur:[newOptions.topBar.background.blur get]];
  87. }
  88. if (newOptions.topBar.background.color.hasValue) {
  89. [navigationController setTopBarBackgroundColor:newOptions.topBar.background.color.get];
  90. }
  91. if (newOptions.topBar.largeTitle.visible.hasValue) {
  92. [navigationController rnn_setNavigationBarLargeTitleVisible:newOptions.topBar.largeTitle.visible.get];
  93. }
  94. if (newOptions.topBar.backButton.color.hasValue) {
  95. [navigationController rnn_setBackButtonColor:newOptions.topBar.backButton.color.get];
  96. }
  97. RNNLargeTitleOptions *largeTitleOptions = newOptions.topBar.largeTitle;
  98. if (largeTitleOptions.color.hasValue || largeTitleOptions.fontSize.hasValue || largeTitleOptions.fontFamily.hasValue) {
  99. [navigationController rnn_setNavigationBarLargeTitleFontFamily:[newOptions.topBar.largeTitle.fontFamily getWithDefaultValue:nil] fontSize:[newOptions.topBar.largeTitle.fontSize getWithDefaultValue:nil] color:[newOptions.topBar.largeTitle.color getWithDefaultValue:nil]];
  100. }
  101. RNNNavigationOptions * withDefault = (RNNNavigationOptions *) [[newOptions mergeInOptions:currentOptions] withDefault:[self defaultOptions]];
  102. [navigationController rnn_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 (newOptions.topBar.component.name.hasValue) {
  106. [self setCustomNavigationBarView:newOptions perform:nil];
  107. }
  108. if (newOptions.topBar.background.component.name.hasValue) {
  109. [self setCustomNavigationComponentBackground:newOptions 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. RNNNavigationController* navigationController = 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 = [navigationController 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:navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  148. reactView.backgroundColor = UIColor.clearColor;
  149. _customTopBar.backgroundColor = UIColor.clearColor;
  150. [navigationController.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. RNNNavigationController* navigationController = 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 = [navigationController 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. RNNNavigationController* navigationController = self.boundViewController;
  179. if (_customTopBarBackground) {
  180. [_customTopBarBackground removeFromSuperview];
  181. }
  182. RNNCustomTitleView* customTopBarBackground = [[RNNCustomTitleView alloc] initWithFrame:navigationController.navigationBar.bounds subView:_customTopBarBackgroundReactView alignment:@"fill"];
  183. _customTopBarBackground = customTopBarBackground;
  184. [navigationController.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  185. }
  186. - (void)dealloc {
  187. [_componentRegistry removeComponent:self.boundComponentId];
  188. }
  189. @end