react-native-navigation的迁移库

RNNRootViewController.m 14KB

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