react-native-navigation的迁移库

RNNRootViewController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. @interface RNNRootViewController() {
  8. RNNReactView* _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 = (RNNReactView*)[_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 RNNReactView *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. } else 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. if (self.options.topBar.background.clipToBounds) {
  171. self.navigationController.navigationBar.clipsToBounds = YES;
  172. } else {
  173. self.navigationController.navigationBar.clipsToBounds = NO;
  174. }
  175. } if (_customTopBarBackground && _customTopBarBackground.superview == nil) {
  176. if (self.navigationController.navigationBar.subviews.count && [[self.navigationController.navigationBar.subviews objectAtIndex:1] isKindOfClass:[RNNCustomTitleView class]]) {
  177. [[self.navigationController.navigationBar.subviews objectAtIndex:1] removeFromSuperview];
  178. }
  179. [self.navigationController.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  180. self.navigationController.navigationBar.clipsToBounds = YES;
  181. }
  182. }
  183. -(BOOL)isCustomTransitioned {
  184. return self.options.customTransition.animations != nil;
  185. }
  186. - (BOOL)isCustomViewController {
  187. return self.isExternalComponent;
  188. }
  189. - (BOOL)prefersStatusBarHidden {
  190. if (self.options.statusBar.visible) {
  191. return ![self.options.statusBar.visible boolValue];
  192. } else if ([self.options.statusBar.hideWithTopBar boolValue]) {
  193. return self.navigationController.isNavigationBarHidden;
  194. }
  195. return NO;
  196. }
  197. - (UIStatusBarStyle)preferredStatusBarStyle {
  198. if (self.options.statusBar.style && [self.options.statusBar.style isEqualToString:@"light"]) {
  199. return UIStatusBarStyleLightContent;
  200. } else {
  201. return UIStatusBarStyleDefault;
  202. }
  203. }
  204. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  205. return self.options.layout.supportedOrientations;
  206. }
  207. - (BOOL)hidesBottomBarWhenPushed
  208. {
  209. if (self.options.bottomTabs && self.options.bottomTabs.visible) {
  210. return ![self.options.bottomTabs.visible boolValue];
  211. }
  212. return NO;
  213. }
  214. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  215. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  216. if (![vc.options.topBar.backButton.transition isEqualToString:@"custom"]){
  217. navigationController.delegate = nil;
  218. }
  219. }
  220. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  221. animationControllerForOperation:(UINavigationControllerOperation)operation
  222. fromViewController:(UIViewController*)fromVC
  223. toViewController:(UIViewController*)toVC {
  224. {
  225. if (self.animator) {
  226. return self.animator;
  227. } else if (operation == UINavigationControllerOperationPush && self.options.animations.push.hasCustomAnimation) {
  228. return [[RNNPushAnimation alloc] initWithScreenTransition:self.options.animations.push];
  229. } else if (operation == UINavigationControllerOperationPop && self.options.animations.pop.hasCustomAnimation) {
  230. return [[RNNPushAnimation alloc] initWithScreenTransition:self.options.animations.pop];
  231. } else {
  232. return nil;
  233. }
  234. }
  235. return nil;
  236. }
  237. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
  238. return [[RNNModalAnimation alloc] initWithScreenTransition:self.options.animations.showModal isDismiss:NO];
  239. }
  240. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  241. return [[RNNModalAnimation alloc] initWithScreenTransition:self.options.animations.dismissModal isDismiss:YES];
  242. }
  243. -(void)applyTabBarItem {
  244. [self.options.bottomTab mergeOptions:((RNNNavigationOptions *)self.options.defaultOptions).bottomTab overrideOptions:NO];
  245. [self.options.bottomTab applyOn:self];
  246. }
  247. -(void)applyTopTabsOptions {
  248. [self.options.topTab applyOn:self];
  249. }
  250. - (void)performOnRotation:(void (^)(void))block {
  251. _rotationBlock = block;
  252. }
  253. - (void)orientationDidChange:(NSNotification*)notification {
  254. if (_rotationBlock) {
  255. _rotationBlock();
  256. }
  257. }
  258. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  259. if (self.previewController) {
  260. // RNNRootViewController * vc = (RNNRootViewController*) self.previewController;
  261. // [_eventEmitter sendOnNavigationEvent:@"previewContext" params:@{
  262. // @"previewComponentId": vc.componentId,
  263. // @"componentId": self.componentId
  264. // }];
  265. }
  266. return self.previewController;
  267. }
  268. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  269. RNNRootViewController * vc = (RNNRootViewController*) self.previewController;
  270. // NSDictionary * params = @{
  271. // @"previewComponentId": vc.componentId,
  272. // @"componentId": self.componentId
  273. // };
  274. if (vc.options.preview.commit) {
  275. // [_eventEmitter sendOnNavigationEvent:@"previewCommit" params:params];
  276. [self.navigationController pushViewController:vc animated:false];
  277. } else {
  278. // [_eventEmitter sendOnNavigationEvent:@"previewDismissed" params:params];
  279. }
  280. }
  281. - (void)onActionPress:(NSString *)id {
  282. [_eventEmitter sendOnNavigationButtonPressed:self.componentId buttonId:id];
  283. }
  284. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  285. NSString *actionId = action[@"id"];
  286. NSString *actionTitle = action[@"title"];
  287. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  288. if ([action[@"style"] isEqualToString:@"selected"]) {
  289. actionStyle = UIPreviewActionStyleSelected;
  290. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  291. actionStyle = UIPreviewActionStyleDestructive;
  292. }
  293. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  294. [self onActionPress:actionId];
  295. }];
  296. }
  297. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  298. NSMutableArray *actions = [[NSMutableArray alloc] init];
  299. for (NSDictionary *previewAction in self.options.preview.actions) {
  300. UIPreviewAction *action = [self convertAction:previewAction];
  301. NSDictionary *actionActions = previewAction[@"actions"];
  302. if (actionActions.count > 0) {
  303. NSMutableArray *group = [[NSMutableArray alloc] init];
  304. for (NSDictionary *previewGroupAction in actionActions) {
  305. [group addObject:[self convertAction:previewGroupAction]];
  306. }
  307. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  308. [actions addObject:actionGroup];
  309. } else {
  310. [actions addObject:action];
  311. }
  312. }
  313. return actions;
  314. }
  315. -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
  316. [self.eventEmitter sendOnNavigationButtonPressed:self.componentId buttonId:barButtonItem.buttonId];
  317. }
  318. /**
  319. * fix for #877, #878
  320. */
  321. -(void)onJsReload {
  322. [self cleanReactLeftovers];
  323. }
  324. /**
  325. * fix for #880
  326. */
  327. -(void)dealloc {
  328. [self cleanReactLeftovers];
  329. }
  330. -(void)cleanReactLeftovers {
  331. [[NSNotificationCenter defaultCenter] removeObserver:self];
  332. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  333. self.view = nil;
  334. self.navigationItem.titleView = nil;
  335. self.navigationItem.rightBarButtonItems = nil;
  336. self.navigationItem.leftBarButtonItems = nil;
  337. _customTopBar = nil;
  338. _customTitleView = nil;
  339. _customTopBarBackground = nil;
  340. }
  341. @end