react-native-navigation的迁移库

RNNRootViewController.m 14KB

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