react-native-navigation的迁移库

RNNRootViewController.m 8.9KB

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