react-native-navigation的迁移库

RNNBasePresenter.m 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. RNNNavigationOptions* withDefault = (RNNNavigationOptions *) [[resolvedOptions withDefault:_defaultOptions] overrideOptions:options];
  95. if (options.bottomTab.badge.hasValue) {
  96. [viewController setTabBarItemBadge:options.bottomTab.badge.get];
  97. }
  98. if (options.bottomTab.badgeColor.hasValue && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
  99. [viewController setTabBarItemBadgeColor:options.bottomTab.badgeColor.get];
  100. }
  101. if ([options.bottomTab.dotIndicator hasValue] && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
  102. [[self dotIndicatorPresenter] apply:viewController:options.bottomTab.dotIndicator];
  103. }
  104. if (options.bottomTab.text.hasValue) {
  105. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  106. viewController.tabBarItem = tabItem;
  107. }
  108. if (options.bottomTab.icon.hasValue) {
  109. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  110. viewController.tabBarItem = tabItem;
  111. }
  112. if (options.bottomTab.selectedIcon.hasValue) {
  113. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  114. viewController.tabBarItem = tabItem;
  115. }
  116. if (options.bottomTab.textColor.hasValue) {
  117. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  118. viewController.tabBarItem = tabItem;
  119. }
  120. if (options.bottomTab.selectedTextColor.hasValue) {
  121. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  122. viewController.tabBarItem = tabItem;
  123. }
  124. if (options.bottomTab.iconColor.hasValue) {
  125. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  126. viewController.tabBarItem = tabItem;
  127. }
  128. if (options.bottomTab.selectedIconColor.hasValue) {
  129. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  130. viewController.tabBarItem = tabItem;
  131. }
  132. }
  133. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  134. if (readyBlock) {
  135. readyBlock();
  136. readyBlock = nil;
  137. }
  138. }
  139. - (void)viewDidLayoutSubviews {
  140. }
  141. - (void)applyDotIndicator:(UIViewController *)child {
  142. [[self dotIndicatorPresenter] apply:child:[child resolveOptions].bottomTab.dotIndicator];
  143. }
  144. - (UIStatusBarStyle)getStatusBarStyle:(RNNNavigationOptions *)resolvedOptions {
  145. RNNNavigationOptions *withDefault = [resolvedOptions withDefault:[self defaultOptions]];
  146. NSString* statusBarStyle = [withDefault.statusBar.style getWithDefaultValue:@"default"];
  147. if ([statusBarStyle isEqualToString:@"light"]) {
  148. return UIStatusBarStyleLightContent;
  149. } else if (@available(iOS 13.0, *)) {
  150. if ([statusBarStyle isEqualToString:@"dark"]) {
  151. return UIStatusBarStyleDarkContent;
  152. } else {
  153. return UIStatusBarStyleDefault;
  154. }
  155. } else {
  156. return UIStatusBarStyleDefault;
  157. }
  158. }
  159. - (UIInterfaceOrientationMask)getOrientation:(RNNNavigationOptions *)options {
  160. return [options withDefault:[self defaultOptions]].layout.supportedOrientations;
  161. }
  162. - (BOOL)isStatusBarVisibility:(UINavigationController *)stack resolvedOptions:(RNNNavigationOptions *)resolvedOptions {
  163. RNNNavigationOptions *withDefault = [resolvedOptions withDefault:[self defaultOptions]];
  164. if (withDefault.statusBar.visible.hasValue) {
  165. return ![withDefault.statusBar.visible get];
  166. } else if ([withDefault.statusBar.hideWithTopBar getWithDefaultValue:NO]) {
  167. return stack.isNavigationBarHidden;
  168. }
  169. return NO;
  170. }
  171. @end