react-native-navigation的迁移库

RNNBasePresenter.m 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #import "RNNBasePresenter.h"
  2. #import "UIViewController+RNNOptions.h"
  3. #import "RNNTabBarItemCreator.h"
  4. #import "RNNReactComponentRegistry.h"
  5. #import "UIViewController+LayoutProtocol.h"
  6. #import "DotIndicatorOptions.h"
  7. #import "RNNDotIndicatorPresenter.h"
  8. #import "RCTConvert+Modal.h"
  9. @interface RNNBasePresenter ()
  10. @property(nonatomic, strong) RNNDotIndicatorPresenter* dotIndicatorPresenter;
  11. @end
  12. @implementation RNNBasePresenter
  13. - (instancetype)initWithDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  14. self = [super init];
  15. _defaultOptions = defaultOptions;
  16. self.dotIndicatorPresenter = [[RNNDotIndicatorPresenter alloc] initWithDefaultOptions:_defaultOptions];
  17. return self;
  18. }
  19. - (instancetype)initWithComponentRegistry:(RNNReactComponentRegistry *)componentRegistry defaultOptions:(RNNNavigationOptions *)defaultOptions {
  20. self = [self initWithDefaultOptions:defaultOptions];
  21. _componentRegistry = componentRegistry;
  22. return self;
  23. }
  24. - (void)bindViewController:(UIViewController *)boundViewController {
  25. self.boundComponentId = boundViewController.layoutInfo.componentId;
  26. _boundViewController = boundViewController;
  27. }
  28. - (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
  29. _defaultOptions = defaultOptions;
  30. }
  31. - (void)componentDidAppear {
  32. }
  33. - (void)componentDidDisappear {
  34. }
  35. - (void)applyOptionsOnInit:(RNNNavigationOptions *)initialOptions {
  36. UIViewController* viewController = self.boundViewController;
  37. RNNNavigationOptions *withDefault = [initialOptions withDefault:[self defaultOptions]];
  38. [viewController setModalPresentationStyle:[RCTConvert UIModalPresentationStyle:[withDefault.modalPresentationStyle getWithDefaultValue:@"default"]]];
  39. [viewController setModalTransitionStyle:[RCTConvert UIModalTransitionStyle:[withDefault.modalTransitionStyle getWithDefaultValue:@"coverVertical"]]];
  40. if (@available(iOS 13.0, *)) {
  41. viewController.modalInPresentation = ![withDefault.modal.swipeToDismiss getWithDefaultValue:YES];
  42. }
  43. }
  44. - (void)applyOptionsOnViewDidLayoutSubviews:(RNNNavigationOptions *)options {
  45. }
  46. - (void)applyOptionsOnWillMoveToParentViewController:(RNNNavigationOptions *)options {
  47. UIViewController *viewController = self.boundViewController;
  48. RNNNavigationOptions * withDefault = [options withDefault:_defaultOptions];
  49. if (withDefault.bottomTab.text.hasValue) {
  50. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  51. viewController.tabBarItem = tabItem;
  52. }
  53. if (withDefault.bottomTab.icon.hasValue) {
  54. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  55. viewController.tabBarItem = tabItem;
  56. }
  57. if (withDefault.bottomTab.selectedIcon.hasValue) {
  58. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  59. viewController.tabBarItem = tabItem;
  60. }
  61. if (withDefault.bottomTab.badgeColor.hasValue) {
  62. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  63. viewController.tabBarItem = tabItem;
  64. }
  65. if (withDefault.bottomTab.textColor.hasValue) {
  66. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  67. viewController.tabBarItem = tabItem;
  68. }
  69. if (withDefault.bottomTab.iconColor.hasValue) {
  70. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  71. viewController.tabBarItem = tabItem;
  72. }
  73. if (withDefault.bottomTab.selectedTextColor.hasValue) {
  74. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  75. viewController.tabBarItem = tabItem;
  76. }
  77. if (withDefault.bottomTab.selectedIconColor.hasValue) {
  78. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  79. viewController.tabBarItem = tabItem;
  80. }
  81. }
  82. - (void)applyOptions:(RNNNavigationOptions *)options {
  83. UIViewController *viewController = self.boundViewController;
  84. RNNNavigationOptions * withDefault = [options withDefault:_defaultOptions];
  85. if (withDefault.bottomTab.badge.hasValue && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
  86. [viewController setTabBarItemBadge:withDefault.bottomTab.badge.get];
  87. }
  88. if (withDefault.bottomTab.badgeColor.hasValue && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
  89. [viewController setTabBarItemBadgeColor:withDefault.bottomTab.badgeColor.get];
  90. }
  91. }
  92. - (void)mergeOptions:(RNNNavigationOptions *)options resolvedOptions:(RNNNavigationOptions *)resolvedOptions {
  93. UIViewController *viewController = self.boundViewController;
  94. if (options.bottomTab.badge.hasValue && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
  95. [viewController setTabBarItemBadge:options.bottomTab.badge.get];
  96. }
  97. if (options.bottomTab.badgeColor.hasValue && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
  98. [viewController setTabBarItemBadgeColor:options.bottomTab.badgeColor.get];
  99. }
  100. if ([options.bottomTab.dotIndicator hasValue] && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
  101. [[self dotIndicatorPresenter] apply:viewController:options.bottomTab.dotIndicator];
  102. }
  103. if (options.bottomTab.text.hasValue) {
  104. RNNNavigationOptions *buttonsResolvedOptions = (RNNNavigationOptions *) [resolvedOptions overrideOptions:options];
  105. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:buttonsResolvedOptions.bottomTab];
  106. viewController.tabBarItem = tabItem;
  107. }
  108. if (options.bottomTab.icon.hasValue) {
  109. RNNNavigationOptions *buttonsResolvedOptions = (RNNNavigationOptions *) [resolvedOptions overrideOptions:options];
  110. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:buttonsResolvedOptions.bottomTab];
  111. viewController.tabBarItem = tabItem;
  112. }
  113. if (options.bottomTab.selectedIcon.hasValue) {
  114. RNNNavigationOptions *buttonsResolvedOptions = (RNNNavigationOptions *) [resolvedOptions overrideOptions:options];
  115. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:buttonsResolvedOptions.bottomTab];
  116. viewController.tabBarItem = tabItem;
  117. }
  118. if (options.bottomTab.textColor.hasValue) {
  119. RNNNavigationOptions *buttonsResolvedOptions = (RNNNavigationOptions *) [resolvedOptions overrideOptions:options];
  120. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:buttonsResolvedOptions.bottomTab];
  121. viewController.tabBarItem = tabItem;
  122. }
  123. if (options.bottomTab.selectedTextColor.hasValue) {
  124. RNNNavigationOptions *buttonsResolvedOptions = (RNNNavigationOptions *) [resolvedOptions overrideOptions:options];
  125. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:buttonsResolvedOptions.bottomTab];
  126. viewController.tabBarItem = tabItem;
  127. }
  128. if (options.bottomTab.iconColor.hasValue) {
  129. RNNNavigationOptions *buttonsResolvedOptions = (RNNNavigationOptions *) [resolvedOptions overrideOptions:options];
  130. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:buttonsResolvedOptions.bottomTab];
  131. viewController.tabBarItem = tabItem;
  132. }
  133. if (options.bottomTab.selectedIconColor.hasValue) {
  134. RNNNavigationOptions *buttonsResolvedOptions = (RNNNavigationOptions *) [resolvedOptions overrideOptions:options];
  135. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:buttonsResolvedOptions.bottomTab];
  136. viewController.tabBarItem = tabItem;
  137. }
  138. }
  139. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  140. if (readyBlock) {
  141. readyBlock();
  142. readyBlock = nil;
  143. }
  144. }
  145. - (void)viewDidLayoutSubviews {
  146. }
  147. - (void)applyDotIndicator:(UIViewController *)child {
  148. [[self dotIndicatorPresenter] apply:child:[child resolveOptions].bottomTab.dotIndicator];
  149. }
  150. - (UIStatusBarStyle)getStatusBarStyle:(RNNNavigationOptions *)resolvedOptions {
  151. RNNNavigationOptions *withDefault = [resolvedOptions withDefault:[self defaultOptions]];
  152. if ([[withDefault.statusBar.style getWithDefaultValue:@"default"] isEqualToString:@"light"]) {
  153. return UIStatusBarStyleLightContent;
  154. } else {
  155. return UIStatusBarStyleDefault;
  156. }
  157. }
  158. - (UIInterfaceOrientationMask)getOrientation:(RNNNavigationOptions *)options {
  159. return [options withDefault:[self defaultOptions]].layout.supportedOrientations;
  160. }
  161. - (BOOL)isStatusBarVisibility:(UINavigationController *)stack resolvedOptions:(RNNNavigationOptions *)resolvedOptions {
  162. RNNNavigationOptions *withDefault = [resolvedOptions withDefault:[self defaultOptions]];
  163. if (withDefault.statusBar.visible.hasValue) {
  164. return ![withDefault.statusBar.visible get];
  165. } else if ([withDefault.statusBar.hideWithTopBar getWithDefaultValue:NO]) {
  166. return stack.isNavigationBarHidden;
  167. }
  168. return NO;
  169. }
  170. @end