react-native-navigation的迁移库

RNNRootViewController.m 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #import "RNNRootViewController.h"
  2. #import <React/RCTConvert.h>
  3. #import "RNNAnimator.h"
  4. #import "RNNPushAnimation.h"
  5. #import "RNNReactView.h"
  6. #import "RNNAnimationsTransitionDelegate.h"
  7. #import "UIViewController+LayoutProtocol.h"
  8. @implementation RNNRootViewController
  9. @synthesize previewCallback;
  10. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo rootViewCreator:(id<RNNRootViewCreator>)creator eventEmitter:(RNNEventEmitter *)eventEmitter presenter:(RNNViewControllerPresenter *)presenter options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions {
  11. self = [super initWithLayoutInfo:layoutInfo creator:creator options:options defaultOptions:defaultOptions presenter:presenter eventEmitter:eventEmitter];
  12. self.animator = [[RNNAnimator alloc] initWithTransitionOptions:self.resolveOptions.customTransition];
  13. [[NSNotificationCenter defaultCenter] addObserver:self
  14. selector:@selector(onJsReload)
  15. name:RCTJavaScriptWillStartLoadingNotification
  16. object:nil];
  17. self.navigationController.delegate = self;
  18. return self;
  19. }
  20. - (instancetype)initExternalComponentWithLayoutInfo:(RNNLayoutInfo *)layoutInfo eventEmitter:(RNNEventEmitter *)eventEmitter presenter:(RNNViewControllerPresenter *)presenter options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions {
  21. self = [self initWithLayoutInfo:layoutInfo rootViewCreator:nil eventEmitter:eventEmitter presenter:presenter options:options defaultOptions:defaultOptions];
  22. return self;
  23. }
  24. - (void)bindViewController:(UIViewController *)viewController {
  25. [self addChildViewController:viewController];
  26. [self.view addSubview:viewController.view];
  27. [viewController didMoveToParentViewController:self];
  28. }
  29. - (void)mergeOptions:(RNNNavigationOptions *)options {
  30. [_presenter mergeOptions:options currentOptions:self.options defaultOptions:self.defaultOptions];
  31. [((UIViewController<RNNLayoutProtocol> *)self.parentViewController) mergeOptions:options];
  32. }
  33. - (void)overrideOptions:(RNNNavigationOptions *)options {
  34. [self.options overrideOptions:options];
  35. }
  36. - (void)viewWillAppear:(BOOL)animated{
  37. [super viewWillAppear:animated];
  38. [_presenter applyOptions:self.resolveOptions];
  39. [_presenter renderComponents:self.resolveOptions perform:nil];
  40. [((UIViewController *)self.parentViewController) onChildWillAppear];
  41. }
  42. -(void)viewDidAppear:(BOOL)animated {
  43. [super viewDidAppear:animated];
  44. [self.eventEmitter sendComponentDidAppear:self.layoutInfo.componentId componentName:self.layoutInfo.name];
  45. }
  46. - (void)viewWillDisappear:(BOOL)animated {
  47. [super viewWillDisappear:animated];
  48. }
  49. - (void)viewDidDisappear:(BOOL)animated {
  50. [super viewDidDisappear:animated];
  51. [self.eventEmitter sendComponentDidDisappear:self.layoutInfo.componentId componentName:self.layoutInfo.name];
  52. }
  53. - (void)renderTreeAndWait:(BOOL)wait perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  54. if (self.isExternalViewController) {
  55. if (readyBlock) {
  56. readyBlock();
  57. }
  58. return;
  59. }
  60. __block RNNReactViewReadyCompletionBlock readyBlockCopy = readyBlock;
  61. UIView* reactView = [self.creator createRootView:self.layoutInfo.name rootViewId:self.layoutInfo.componentId availableSize:[UIScreen mainScreen].bounds.size reactViewReadyBlock:^{
  62. [_presenter renderComponents:self.resolveOptions perform:^{
  63. if (readyBlockCopy) {
  64. readyBlockCopy();
  65. readyBlockCopy = nil;
  66. }
  67. }];
  68. }];
  69. self.view = reactView;
  70. if (!wait && readyBlock) {
  71. readyBlockCopy();
  72. readyBlockCopy = nil;
  73. }
  74. }
  75. - (UIViewController *)getCurrentChild {
  76. return nil;
  77. }
  78. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  79. [self.eventEmitter sendOnSearchBarUpdated:self.layoutInfo.componentId
  80. text:searchController.searchBar.text
  81. isFocused:searchController.searchBar.isFirstResponder];
  82. }
  83. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  84. [self.eventEmitter sendOnSearchBarCancelPressed:self.layoutInfo.componentId];
  85. }
  86. -(BOOL)isCustomTransitioned {
  87. return self.resolveOptions.customTransition.animations != nil;
  88. }
  89. - (BOOL)isExternalViewController {
  90. return !self.creator;
  91. }
  92. - (BOOL)prefersStatusBarHidden {
  93. if (self.resolveOptions.statusBar.visible.hasValue) {
  94. return ![self.resolveOptions.statusBar.visible get];
  95. } else if ([self.resolveOptions.statusBar.hideWithTopBar getWithDefaultValue:NO]) {
  96. return self.navigationController.isNavigationBarHidden;
  97. }
  98. return NO;
  99. }
  100. - (UIStatusBarStyle)preferredStatusBarStyle {
  101. if ([[self.resolveOptions.statusBar.style getWithDefaultValue:@"default"] isEqualToString:@"light"]) {
  102. return UIStatusBarStyleLightContent;
  103. } else {
  104. return UIStatusBarStyleDefault;
  105. }
  106. }
  107. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  108. return self.resolveOptions.layout.supportedOrientations;
  109. }
  110. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  111. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  112. if (![[vc.self.resolveOptions.topBar.backButton.transition getWithDefaultValue:@""] isEqualToString:@"custom"]){
  113. navigationController.delegate = nil;
  114. }
  115. }
  116. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  117. animationControllerForOperation:(UINavigationControllerOperation)operation
  118. fromViewController:(UIViewController*)fromVC
  119. toViewController:(UIViewController*)toVC {
  120. if (self.animator) {
  121. return self.animator;
  122. } else if (operation == UINavigationControllerOperationPush && self.resolveOptions.animations.push.hasCustomAnimation) {
  123. return [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:self.resolveOptions.animations.push isDismiss:NO];
  124. } else if (operation == UINavigationControllerOperationPop && self.resolveOptions.animations.pop.hasCustomAnimation) {
  125. return [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:self.resolveOptions.animations.pop isDismiss:YES];
  126. } else {
  127. return nil;
  128. }
  129. return nil;
  130. }
  131. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  132. return self.previewController;
  133. }
  134. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  135. if (self.previewCallback) {
  136. self.previewCallback(self);
  137. }
  138. }
  139. - (void)onActionPress:(NSString *)id {
  140. [_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
  141. }
  142. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  143. NSString *actionId = action[@"id"];
  144. NSString *actionTitle = action[@"title"];
  145. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  146. if ([action[@"style"] isEqualToString:@"selected"]) {
  147. actionStyle = UIPreviewActionStyleSelected;
  148. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  149. actionStyle = UIPreviewActionStyleDestructive;
  150. }
  151. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  152. [self onActionPress:actionId];
  153. }];
  154. }
  155. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  156. NSMutableArray *actions = [[NSMutableArray alloc] init];
  157. for (NSDictionary *previewAction in self.resolveOptions.preview.actions) {
  158. UIPreviewAction *action = [self convertAction:previewAction];
  159. NSDictionary *actionActions = previewAction[@"actions"];
  160. if (actionActions.count > 0) {
  161. NSMutableArray *group = [[NSMutableArray alloc] init];
  162. for (NSDictionary *previewGroupAction in actionActions) {
  163. [group addObject:[self convertAction:previewGroupAction]];
  164. }
  165. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  166. [actions addObject:actionGroup];
  167. } else {
  168. [actions addObject:action];
  169. }
  170. }
  171. return actions;
  172. }
  173. -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
  174. [self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
  175. }
  176. /**
  177. * fix for #877, #878
  178. */
  179. -(void)onJsReload {
  180. [self cleanReactLeftovers];
  181. }
  182. /**
  183. * fix for #880
  184. */
  185. -(void)dealloc {
  186. [self cleanReactLeftovers];
  187. }
  188. -(void)cleanReactLeftovers {
  189. [[NSNotificationCenter defaultCenter] removeObserver:self];
  190. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  191. self.view = nil;
  192. self.navigationItem.titleView = nil;
  193. self.navigationItem.rightBarButtonItems = nil;
  194. self.navigationItem.leftBarButtonItems = nil;
  195. }
  196. @end