react-native-navigation的迁移库

RNNRootViewController.m 9.4KB

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