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