react-native-navigation的迁移库

RNNNavigationControllerPresenter.m 11KB

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