react-native-navigation的迁移库

RNNRootViewController.m 14KB

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