react-native-navigation的迁移库

RNNBasePresenter.m 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. UIApplication.sharedApplication.delegate.window.backgroundColor = [withDefault.window.backgroundColor getWithDefaultValue:nil];
  44. }
  45. - (void)applyOptionsOnViewDidLayoutSubviews:(RNNNavigationOptions *)options {
  46. }
  47. - (void)applyOptionsOnWillMoveToParentViewController:(RNNNavigationOptions *)options {
  48. UIViewController *viewController = self.boundViewController;
  49. RNNNavigationOptions * withDefault = [options withDefault:_defaultOptions];
  50. if (withDefault.bottomTab.text.hasValue) {
  51. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  52. viewController.tabBarItem = tabItem;
  53. }
  54. if (withDefault.bottomTab.icon.hasValue) {
  55. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  56. viewController.tabBarItem = tabItem;
  57. }
  58. if (withDefault.bottomTab.selectedIcon.hasValue) {
  59. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  60. viewController.tabBarItem = tabItem;
  61. }
  62. if (withDefault.bottomTab.badgeColor.hasValue) {
  63. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  64. viewController.tabBarItem = tabItem;
  65. }
  66. if (withDefault.bottomTab.textColor.hasValue) {
  67. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  68. viewController.tabBarItem = tabItem;
  69. }
  70. if (withDefault.bottomTab.iconColor.hasValue) {
  71. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  72. viewController.tabBarItem = tabItem;
  73. }
  74. if (withDefault.bottomTab.selectedTextColor.hasValue) {
  75. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  76. viewController.tabBarItem = tabItem;
  77. }
  78. if (withDefault.bottomTab.selectedIconColor.hasValue) {
  79. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  80. viewController.tabBarItem = tabItem;
  81. }
  82. }
  83. - (void)applyOptions:(RNNNavigationOptions *)options {
  84. UIViewController *viewController = self.boundViewController;
  85. RNNNavigationOptions * withDefault = [options withDefault:_defaultOptions];
  86. if (withDefault.bottomTab.badge.hasValue && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
  87. [viewController setTabBarItemBadge:withDefault.bottomTab.badge.get];
  88. }
  89. if (withDefault.bottomTab.badgeColor.hasValue && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
  90. [viewController setTabBarItemBadgeColor:withDefault.bottomTab.badgeColor.get];
  91. }
  92. }
  93. - (void)mergeOptions:(RNNNavigationOptions *)options resolvedOptions:(RNNNavigationOptions *)resolvedOptions {
  94. UIViewController* viewController = self.boundViewController;
  95. RNNNavigationOptions* withDefault = (RNNNavigationOptions *) [[resolvedOptions withDefault:_defaultOptions] overrideOptions:options];
  96. if (options.bottomTab.badge.hasValue) {
  97. [viewController setTabBarItemBadge:options.bottomTab.badge.get];
  98. }
  99. if (options.bottomTab.badgeColor.hasValue && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
  100. [viewController setTabBarItemBadgeColor:options.bottomTab.badgeColor.get];
  101. }
  102. if ([options.bottomTab.dotIndicator hasValue] && [viewController.parentViewController isKindOfClass:[UITabBarController class]]) {
  103. [[self dotIndicatorPresenter] apply:viewController:options.bottomTab.dotIndicator];
  104. }
  105. if (options.bottomTab.text.hasValue) {
  106. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  107. viewController.tabBarItem = tabItem;
  108. }
  109. if (options.bottomTab.icon.hasValue) {
  110. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  111. viewController.tabBarItem = tabItem;
  112. }
  113. if (options.bottomTab.selectedIcon.hasValue) {
  114. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  115. viewController.tabBarItem = tabItem;
  116. }
  117. if (options.bottomTab.textColor.hasValue) {
  118. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  119. viewController.tabBarItem = tabItem;
  120. }
  121. if (options.bottomTab.selectedTextColor.hasValue) {
  122. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  123. viewController.tabBarItem = tabItem;
  124. }
  125. if (options.bottomTab.iconColor.hasValue) {
  126. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  127. viewController.tabBarItem = tabItem;
  128. }
  129. if (options.bottomTab.selectedIconColor.hasValue) {
  130. UITabBarItem *tabItem = [RNNTabBarItemCreator updateTabBarItem:viewController.tabBarItem bottomTabOptions:withDefault.bottomTab];
  131. viewController.tabBarItem = tabItem;
  132. }
  133. if (options.window.backgroundColor.hasValue) {
  134. UIApplication.sharedApplication.delegate.window.backgroundColor = withDefault.window.backgroundColor.get;
  135. }
  136. }
  137. - (void)renderComponents:(RNNNavigationOptions *)options perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  138. if (readyBlock) {
  139. readyBlock();
  140. readyBlock = nil;
  141. }
  142. }
  143. - (void)viewDidLayoutSubviews {
  144. }
  145. - (void)applyDotIndicator:(UIViewController *)child {
  146. [[self dotIndicatorPresenter] apply:child:[child resolveOptions].bottomTab.dotIndicator];
  147. }
  148. - (UIStatusBarStyle)getStatusBarStyle:(RNNNavigationOptions *)resolvedOptions {
  149. RNNNavigationOptions *withDefault = [resolvedOptions withDefault:[self defaultOptions]];
  150. NSString* statusBarStyle = [withDefault.statusBar.style getWithDefaultValue:@"default"];
  151. if ([statusBarStyle isEqualToString:@"light"]) {
  152. return UIStatusBarStyleLightContent;
  153. } else if (@available(iOS 13.0, *)) {
  154. if ([statusBarStyle isEqualToString:@"dark"]) {
  155. return UIStatusBarStyleDarkContent;
  156. } else {
  157. return UIStatusBarStyleDefault;
  158. }
  159. } else {
  160. return UIStatusBarStyleDefault;
  161. }
  162. }
  163. - (UIInterfaceOrientationMask)getOrientation:(RNNNavigationOptions *)options {
  164. return [options withDefault:[self defaultOptions]].layout.supportedOrientations;
  165. }
  166. - (BOOL)isStatusBarVisibility:(UINavigationController *)stack resolvedOptions:(RNNNavigationOptions *)resolvedOptions {
  167. RNNNavigationOptions *withDefault = [resolvedOptions withDefault:[self defaultOptions]];
  168. if (withDefault.statusBar.visible.hasValue) {
  169. return ![withDefault.statusBar.visible get];
  170. } else if ([withDefault.statusBar.hideWithTopBar getWithDefaultValue:NO]) {
  171. return stack.isNavigationBarHidden;
  172. }
  173. return NO;
  174. }
  175. @end