react-native-navigation的迁移库

RNNNavigationControllerPresenter.m 11KB

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