react-native-navigation的迁移库

RNNRootViewController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. [self optionsUpdated];
  55. }
  56. -(void)viewDidAppear:(BOOL)animated {
  57. [super viewDidAppear:animated];
  58. [self.eventEmitter sendComponentDidAppear:self.componentId componentName:self.componentName];
  59. if (@available(iOS 11.0, *)) {
  60. if (self.navigationItem.searchController && [self.options.topBar.searchBarHiddenWhenScrolling boolValue]) {
  61. self.navigationItem.hidesSearchBarWhenScrolling = YES;
  62. }
  63. }
  64. }
  65. - (void)viewWillDisappear:(BOOL)animated {
  66. [super viewWillDisappear:animated];
  67. }
  68. -(void)viewDidDisappear:(BOOL)animated {
  69. [super viewDidDisappear:animated];
  70. [self.eventEmitter sendComponentDidDisappear:self.componentId componentName:self.componentName];
  71. }
  72. - (void)reactViewReady {
  73. if (_reactViewReadyBlock) {
  74. _reactViewReadyBlock();
  75. _reactViewReadyBlock = nil;
  76. }
  77. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RCTContentDidAppearNotification" object:nil];
  78. }
  79. - (void)waitForReactViewRender:(BOOL)wait perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  80. if (wait) {
  81. [self onReactViewReady:readyBlock];
  82. } else {
  83. readyBlock();
  84. }
  85. }
  86. - (UIViewController *)getLeafViewController {
  87. return self;
  88. }
  89. - (void)onReactViewReady:(RNNReactViewReadyCompletionBlock)readyBlock {
  90. if (self.isCustomViewController) {
  91. readyBlock();
  92. } else {
  93. self.reactViewReadyBlock = readyBlock;
  94. }
  95. }
  96. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  97. [self.eventEmitter sendOnSearchBarUpdated:self.componentId
  98. text:searchController.searchBar.text
  99. isFocused:searchController.searchBar.isFirstResponder];
  100. }
  101. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  102. [self.eventEmitter sendOnSearchBarCancelPressed:self.componentId];
  103. }
  104. - (void)viewDidLoad {
  105. [super viewDidLoad];
  106. }
  107. - (void)optionsUpdated {
  108. [self setCustomNavigationTitleView];
  109. [self setCustomNavigationBarView];
  110. [self setCustomNavigationComponentBackground];
  111. }
  112. - (void)applyModalOptions {
  113. [self.options applyOn:self];
  114. [self.options applyModalOptions:self];
  115. }
  116. - (void)mergeOptions:(RNNOptions *)options {
  117. [self.options mergeOptions:options overrideOptions:NO];
  118. }
  119. - (void)setCustomNavigationTitleView {
  120. if (!_customTitleView) {
  121. if (self.options.topBar.title.component.name) {
  122. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.options.topBar.title.component];
  123. _customTitleView = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:self.options.topBar.title.component.alignment];
  124. reactView.backgroundColor = UIColor.clearColor;
  125. _customTitleView.backgroundColor = UIColor.clearColor;
  126. self.navigationItem.titleView = _customTitleView;
  127. } if ([self.navigationItem.title isKindOfClass:[RNNCustomTitleView class]] && !_customTitleView) {
  128. self.navigationItem.title = nil;
  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. }
  323. @end