react-native-navigation的迁移库

RNNRootViewController.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #import "RNNRootViewController.h"
  2. #import <React/RCTConvert.h>
  3. #import "RNNAnimator.h"
  4. #import "RNNCustomTitleView.h"
  5. #import "RNNPushAnimation.h"
  6. #import "RNNReactView.h"
  7. #import "RNNParentProtocol.h"
  8. @interface RNNRootViewController() {
  9. RNNReactView* _customTitleView;
  10. UIView* _customTopBar;
  11. UIView* _customTopBarBackground;
  12. BOOL _isBeingPresented;
  13. }
  14. @property (nonatomic, copy) RNNReactViewReadyCompletionBlock reactViewReadyBlock;
  15. @end
  16. @implementation RNNRootViewController
  17. @synthesize previewCallback;
  18. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo rootViewCreator:(id<RNNRootViewCreator>)creator eventEmitter:(RNNEventEmitter *)eventEmitter presenter:(RNNViewControllerPresenter *)presenter options:(RNNNavigationOptions *)options {
  19. self = [super init];
  20. self.eventEmitter = eventEmitter;
  21. self.creator = creator;
  22. self.layoutInfo = layoutInfo;
  23. self.presenter = presenter;
  24. self.options = options;
  25. self.animator = [[RNNAnimator alloc] initWithTransitionOptions:self.options.customTransition];
  26. if (self.creator) {
  27. self.view = [creator createRootView:self.layoutInfo.name rootViewId:self.layoutInfo.componentId];
  28. [[NSNotificationCenter defaultCenter] addObserver:self
  29. selector:@selector(reactViewReady)
  30. name: @"RCTContentDidAppearNotification"
  31. object:nil];
  32. }
  33. [[NSNotificationCenter defaultCenter] addObserver:self
  34. selector:@selector(onJsReload)
  35. name:RCTJavaScriptWillStartLoadingNotification
  36. object:nil];
  37. self.navigationController.delegate = self;
  38. return self;
  39. }
  40. - (instancetype)initExternalComponentWithLayoutInfo:(RNNLayoutInfo *)layoutInfo eventEmitter:(RNNEventEmitter *)eventEmitter presenter:(RNNViewControllerPresenter *)presenter options:(RNNNavigationOptions *)options {
  41. self = [self initWithLayoutInfo:layoutInfo rootViewCreator:nil eventEmitter:eventEmitter presenter:presenter options:options];
  42. return self;
  43. }
  44. - (void)bindViewController:(UIViewController *)viewController {
  45. [self addChildViewController:viewController];
  46. [self.view addSubview:viewController.view];
  47. [viewController didMoveToParentViewController:self];
  48. }
  49. -(void)viewWillAppear:(BOOL)animated{
  50. [super viewWillAppear:animated];
  51. _isBeingPresented = YES;
  52. [_presenter present:self.options onViewControllerWillAppear:self];
  53. [self initReactCustomViews];
  54. }
  55. -(void)viewDidAppear:(BOOL)animated {
  56. [super viewDidAppear:animated];
  57. [self.eventEmitter sendComponentDidAppear:self.layoutInfo.componentId componentName:self.layoutInfo.name];
  58. }
  59. - (void)viewWillDisappear:(BOOL)animated {
  60. [super viewWillDisappear:animated];
  61. _isBeingPresented = NO;
  62. }
  63. -(void)viewDidDisappear:(BOOL)animated {
  64. [super viewDidDisappear:animated];
  65. [self.eventEmitter sendComponentDidDisappear:self.layoutInfo.componentId componentName:self.layoutInfo.name];
  66. }
  67. - (void)willMoveToParentViewController:(UIViewController *)parent {
  68. [_presenter present:self.options onViewControllerDidLoad:self];
  69. }
  70. - (void)mergeOptions:(RNNNavigationOptions *)options {
  71. [self.options mergeOptions:options overrideOptions:YES];
  72. [self.presenter present:self.options onViewControllerWillAppear:self];
  73. [self initReactCustomViews];
  74. }
  75. - (void)reactViewReady {
  76. if (_reactViewReadyBlock) {
  77. _reactViewReadyBlock();
  78. _reactViewReadyBlock = nil;
  79. }
  80. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RCTContentDidAppearNotification" object:nil];
  81. }
  82. - (void)waitForReactViewRender:(BOOL)wait perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  83. if (wait && !self.isExternalViewController) {
  84. [self onReactViewReady:readyBlock];
  85. } else {
  86. readyBlock();
  87. }
  88. }
  89. - (UIViewController *)getLeafViewController {
  90. return self;
  91. }
  92. - (void)onReactViewReady:(RNNReactViewReadyCompletionBlock)readyBlock {
  93. if (self.isExternalViewController) {
  94. readyBlock();
  95. } else {
  96. self.reactViewReadyBlock = readyBlock;
  97. }
  98. }
  99. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  100. [self.eventEmitter sendOnSearchBarUpdated:self.layoutInfo.componentId
  101. text:searchController.searchBar.text
  102. isFocused:searchController.searchBar.isFirstResponder];
  103. }
  104. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  105. [self.eventEmitter sendOnSearchBarCancelPressed:self.layoutInfo.componentId];
  106. }
  107. - (void)initReactCustomViews {
  108. [self setCustomNavigationTitleView];
  109. [self setCustomNavigationBarView];
  110. [self setCustomNavigationComponentBackground];
  111. }
  112. - (void)setCustomNavigationTitleView {
  113. if (!_customTitleView && _isBeingPresented) {
  114. if (self.options.topBar.title.component.name) {
  115. _customTitleView = (RNNReactView*)[_creator createRootViewFromComponentOptions:self.options.topBar.title.component];
  116. _customTitleView.backgroundColor = UIColor.clearColor;
  117. [_customTitleView setAlignment:self.options.topBar.title.component.alignment];
  118. BOOL isCenter = [self.options.topBar.title.component.alignment isEqualToString:@"center"];
  119. __weak RNNReactView *weakTitleView = _customTitleView;
  120. CGRect frame = self.navigationController.navigationBar.bounds;
  121. [_customTitleView setFrame:frame];
  122. [_customTitleView setRootViewDidChangeIntrinsicSize:^(CGSize intrinsicContentSize) {
  123. if (isCenter) {
  124. [weakTitleView setFrame:CGRectMake(0, 0, intrinsicContentSize.width, intrinsicContentSize.height)];
  125. } else {
  126. [weakTitleView setFrame:frame];
  127. }
  128. }];
  129. self.navigationItem.titleView = _customTitleView;
  130. }
  131. } else if (_customTitleView && _customTitleView.superview == nil) {
  132. if ([self.navigationItem.title isKindOfClass:[RNNCustomTitleView class]] && !_customTitleView) {
  133. self.navigationItem.title = nil;
  134. }
  135. self.navigationItem.titleView = _customTitleView;
  136. }
  137. }
  138. - (void)setCustomNavigationBarView {
  139. if (!_customTopBar) {
  140. if (self.options.topBar.component.name) {
  141. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.options.topBar.component];
  142. _customTopBar = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  143. reactView.backgroundColor = UIColor.clearColor;
  144. _customTopBar.backgroundColor = UIColor.clearColor;
  145. [self.navigationController.navigationBar addSubview:_customTopBar];
  146. } else if ([[self.navigationController.navigationBar.subviews lastObject] isKindOfClass:[RNNCustomTitleView class]] && !_customTopBar) {
  147. [[self.navigationController.navigationBar.subviews lastObject] removeFromSuperview];
  148. }
  149. } else if (_customTopBar && _customTopBar.superview == nil) {
  150. if ([[self.navigationController.navigationBar.subviews lastObject] isKindOfClass:[RNNCustomTitleView class]] && !_customTopBar) {
  151. [[self.navigationController.navigationBar.subviews lastObject] removeFromSuperview];
  152. }
  153. [self.navigationController.navigationBar addSubview:_customTopBar];
  154. }
  155. }
  156. - (void)setCustomNavigationComponentBackground {
  157. if (!_customTopBarBackground) {
  158. if (self.options.topBar.background.component.name) {
  159. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.options.topBar.background.component];
  160. _customTopBarBackground = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  161. [self.navigationController.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  162. } else if (self.navigationController.navigationBar.subviews.count && [[self.navigationController.navigationBar.subviews objectAtIndex:1] isKindOfClass:[RNNCustomTitleView class]]) {
  163. [[self.navigationController.navigationBar.subviews objectAtIndex:1] removeFromSuperview];
  164. }
  165. } if (_customTopBarBackground && _customTopBarBackground.superview == nil) {
  166. if (self.navigationController.navigationBar.subviews.count && [[self.navigationController.navigationBar.subviews objectAtIndex:1] isKindOfClass:[RNNCustomTitleView class]]) {
  167. [[self.navigationController.navigationBar.subviews objectAtIndex:1] removeFromSuperview];
  168. }
  169. [self.navigationController.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  170. self.navigationController.navigationBar.clipsToBounds = YES;
  171. }
  172. }
  173. -(BOOL)isCustomTransitioned {
  174. return self.options.customTransition.animations != nil;
  175. }
  176. - (BOOL)isExternalViewController {
  177. return !self.creator;
  178. }
  179. - (BOOL)prefersStatusBarHidden {
  180. if (self.options.statusBar.visible) {
  181. return ![self.options.statusBar.visible boolValue];
  182. } else if ([self.options.statusBar.hideWithTopBar boolValue]) {
  183. return self.navigationController.isNavigationBarHidden;
  184. }
  185. return NO;
  186. }
  187. - (UIStatusBarStyle)preferredStatusBarStyle {
  188. if (self.options.statusBar.style && [self.options.statusBar.style isEqualToString:@"light"]) {
  189. return UIStatusBarStyleLightContent;
  190. } else {
  191. return UIStatusBarStyleDefault;
  192. }
  193. }
  194. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  195. return self.options.layout.supportedOrientations;
  196. }
  197. - (BOOL)hidesBottomBarWhenPushed
  198. {
  199. if (self.options.bottomTabs && self.options.bottomTabs.visible) {
  200. return ![self.options.bottomTabs.visible boolValue];
  201. }
  202. return NO;
  203. }
  204. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  205. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  206. if (![vc.options.topBar.backButton.transition isEqualToString:@"custom"]){
  207. navigationController.delegate = nil;
  208. }
  209. }
  210. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  211. animationControllerForOperation:(UINavigationControllerOperation)operation
  212. fromViewController:(UIViewController*)fromVC
  213. toViewController:(UIViewController*)toVC {
  214. if (self.animator) {
  215. return self.animator;
  216. } else if (operation == UINavigationControllerOperationPush && self.options.animations.push.hasCustomAnimation) {
  217. return [[RNNPushAnimation alloc] initWithScreenTransition:self.options.animations.push];
  218. } else if (operation == UINavigationControllerOperationPop && self.options.animations.pop.hasCustomAnimation) {
  219. return [[RNNPushAnimation alloc] initWithScreenTransition:self.options.animations.pop];
  220. } else {
  221. return nil;
  222. }
  223. return nil;
  224. }
  225. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
  226. return [[RNNModalAnimation alloc] initWithScreenTransition:self.options.animations.showModal isDismiss:NO];
  227. }
  228. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  229. return [[RNNModalAnimation alloc] initWithScreenTransition:self.options.animations.dismissModal isDismiss:YES];
  230. }
  231. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  232. return self.previewController;
  233. }
  234. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  235. if (self.previewCallback) {
  236. self.previewCallback(self);
  237. }
  238. }
  239. - (void)onActionPress:(NSString *)id {
  240. [_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
  241. }
  242. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  243. NSString *actionId = action[@"id"];
  244. NSString *actionTitle = action[@"title"];
  245. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  246. if ([action[@"style"] isEqualToString:@"selected"]) {
  247. actionStyle = UIPreviewActionStyleSelected;
  248. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  249. actionStyle = UIPreviewActionStyleDestructive;
  250. }
  251. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  252. [self onActionPress:actionId];
  253. }];
  254. }
  255. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  256. NSMutableArray *actions = [[NSMutableArray alloc] init];
  257. for (NSDictionary *previewAction in self.options.preview.actions) {
  258. UIPreviewAction *action = [self convertAction:previewAction];
  259. NSDictionary *actionActions = previewAction[@"actions"];
  260. if (actionActions.count > 0) {
  261. NSMutableArray *group = [[NSMutableArray alloc] init];
  262. for (NSDictionary *previewGroupAction in actionActions) {
  263. [group addObject:[self convertAction:previewGroupAction]];
  264. }
  265. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  266. [actions addObject:actionGroup];
  267. } else {
  268. [actions addObject:action];
  269. }
  270. }
  271. return actions;
  272. }
  273. -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
  274. [self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
  275. }
  276. /**
  277. * fix for #877, #878
  278. */
  279. -(void)onJsReload {
  280. [self cleanReactLeftovers];
  281. }
  282. /**
  283. * fix for #880
  284. */
  285. -(void)dealloc {
  286. [self cleanReactLeftovers];
  287. }
  288. -(void)cleanReactLeftovers {
  289. [[NSNotificationCenter defaultCenter] removeObserver:self];
  290. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  291. self.view = nil;
  292. self.navigationItem.titleView = nil;
  293. self.navigationItem.rightBarButtonItems = nil;
  294. self.navigationItem.leftBarButtonItems = nil;
  295. _customTopBar = nil;
  296. _customTitleView = nil;
  297. _customTopBarBackground = nil;
  298. }
  299. @end