react-native-navigation的迁移库

RNNRootViewController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. [_customTitleView setRootViewDidChangeIntrinsicSize:^(CGSize intrinsicContentSize) {
  123. if (isCenter) {
  124. [weakTitleView setFrame:CGRectMake(0, 0, intrinsicContentSize.width, intrinsicContentSize.height)];
  125. }
  126. }];
  127. [_customTitleView setFrame:self.navigationController.navigationBar.frame];
  128. self.navigationItem.titleView = _customTitleView;
  129. }
  130. } else if (_customTitleView && _customTitleView.superview == nil) {
  131. if ([self.navigationItem.title isKindOfClass:[RNNCustomTitleView class]] && !_customTitleView) {
  132. self.navigationItem.title = nil;
  133. }
  134. self.navigationItem.titleView = _customTitleView;
  135. }
  136. }
  137. - (void)setCustomNavigationBarView {
  138. if (!_customTopBar) {
  139. if (self.options.topBar.component.name) {
  140. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.options.topBar.component];
  141. _customTopBar = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  142. reactView.backgroundColor = UIColor.clearColor;
  143. _customTopBar.backgroundColor = UIColor.clearColor;
  144. [self.navigationController.navigationBar addSubview:_customTopBar];
  145. } else if ([[self.navigationController.navigationBar.subviews lastObject] isKindOfClass:[RNNCustomTitleView class]] && !_customTopBar) {
  146. [[self.navigationController.navigationBar.subviews lastObject] removeFromSuperview];
  147. }
  148. } else if (_customTopBar && _customTopBar.superview == nil) {
  149. if ([[self.navigationController.navigationBar.subviews lastObject] isKindOfClass:[RNNCustomTitleView class]] && !_customTopBar) {
  150. [[self.navigationController.navigationBar.subviews lastObject] removeFromSuperview];
  151. }
  152. [self.navigationController.navigationBar addSubview:_customTopBar];
  153. }
  154. }
  155. - (void)setCustomNavigationComponentBackground {
  156. if (!_customTopBarBackground) {
  157. if (self.options.topBar.background.component.name) {
  158. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.options.topBar.background.component];
  159. _customTopBarBackground = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  160. [self.navigationController.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  161. self.navigationController.navigationBar.clipsToBounds = YES;
  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. 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.options.customTransition.animations != nil;
  176. }
  177. - (BOOL)isCustomViewController {
  178. return self.isExternalComponent;
  179. }
  180. - (BOOL)prefersStatusBarHidden {
  181. if (self.options.statusBar.visible) {
  182. return ![self.options.statusBar.visible boolValue];
  183. } else if ([self.options.statusBar.hideWithTopBar boolValue]) {
  184. return self.navigationController.isNavigationBarHidden;
  185. }
  186. return NO;
  187. }
  188. - (UIStatusBarStyle)preferredStatusBarStyle {
  189. if (self.options.statusBar.style && [self.options.statusBar.style isEqualToString:@"light"]) {
  190. return UIStatusBarStyleLightContent;
  191. } else {
  192. return UIStatusBarStyleDefault;
  193. }
  194. }
  195. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  196. return self.options.layout.supportedOrientations;
  197. }
  198. - (BOOL)hidesBottomBarWhenPushed
  199. {
  200. if (self.options.bottomTabs && self.options.bottomTabs.visible) {
  201. return ![self.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.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.options.animations.push.hasCustomAnimation) {
  219. return [[RNNPushAnimation alloc] initWithScreenTransition:self.options.animations.push];
  220. } else if (operation == UINavigationControllerOperationPop && self.options.animations.pop.hasCustomAnimation) {
  221. return [[RNNPushAnimation alloc] initWithScreenTransition:self.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.options.animations.showModal isDismiss:NO];
  230. }
  231. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  232. return [[RNNModalAnimation alloc] initWithScreenTransition:self.options.animations.dismissModal isDismiss:YES];
  233. }
  234. -(void)applyTabBarItem {
  235. [self.options.bottomTab mergeOptions:((RNNNavigationOptions *)self.options.defaultOptions).bottomTab overrideOptions:NO];
  236. [self.options.bottomTab applyOn:self];
  237. }
  238. -(void)applyTopTabsOptions {
  239. [self.options.topTab applyOn:self];
  240. }
  241. - (void)performOnRotation:(void (^)(void))block {
  242. _rotationBlock = block;
  243. }
  244. - (void)orientationDidChange:(NSNotification*)notification {
  245. if (_rotationBlock) {
  246. _rotationBlock();
  247. }
  248. }
  249. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  250. if (self.previewController) {
  251. // RNNRootViewController * vc = (RNNRootViewController*) self.previewController;
  252. // [_eventEmitter sendOnNavigationEvent:@"previewContext" params:@{
  253. // @"previewComponentId": vc.componentId,
  254. // @"componentId": self.componentId
  255. // }];
  256. }
  257. return self.previewController;
  258. }
  259. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  260. RNNRootViewController * vc = (RNNRootViewController*) self.previewController;
  261. // NSDictionary * params = @{
  262. // @"previewComponentId": vc.componentId,
  263. // @"componentId": self.componentId
  264. // };
  265. if (vc.options.preview.commit) {
  266. // [_eventEmitter sendOnNavigationEvent:@"previewCommit" params:params];
  267. [self.navigationController pushViewController:vc animated:false];
  268. } else {
  269. // [_eventEmitter sendOnNavigationEvent:@"previewDismissed" params:params];
  270. }
  271. }
  272. - (void)onActionPress:(NSString *)id {
  273. [_eventEmitter sendOnNavigationButtonPressed:self.componentId buttonId:id];
  274. }
  275. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  276. NSString *actionId = action[@"id"];
  277. NSString *actionTitle = action[@"title"];
  278. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  279. if ([action[@"style"] isEqualToString:@"selected"]) {
  280. actionStyle = UIPreviewActionStyleSelected;
  281. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  282. actionStyle = UIPreviewActionStyleDestructive;
  283. }
  284. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  285. [self onActionPress:actionId];
  286. }];
  287. }
  288. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  289. NSMutableArray *actions = [[NSMutableArray alloc] init];
  290. for (NSDictionary *previewAction in self.options.preview.actions) {
  291. UIPreviewAction *action = [self convertAction:previewAction];
  292. NSDictionary *actionActions = previewAction[@"actions"];
  293. if (actionActions.count > 0) {
  294. NSMutableArray *group = [[NSMutableArray alloc] init];
  295. for (NSDictionary *previewGroupAction in actionActions) {
  296. [group addObject:[self convertAction:previewGroupAction]];
  297. }
  298. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  299. [actions addObject:actionGroup];
  300. } else {
  301. [actions addObject:action];
  302. }
  303. }
  304. return actions;
  305. }
  306. /**
  307. * fix for #877, #878
  308. */
  309. -(void)onJsReload {
  310. [self cleanReactLeftovers];
  311. }
  312. /**
  313. * fix for #880
  314. */
  315. -(void)dealloc {
  316. [self cleanReactLeftovers];
  317. }
  318. -(void)cleanReactLeftovers {
  319. [[NSNotificationCenter defaultCenter] removeObserver:self];
  320. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  321. self.view = nil;
  322. self.navigationItem.titleView = nil;
  323. self.navigationItem.rightBarButtonItems = nil;
  324. self.navigationItem.leftBarButtonItems = nil;
  325. _customTopBar = nil;
  326. _customTitleView = nil;
  327. _customTopBarBackground = nil;
  328. }
  329. @end