react-native-navigation的迁移库

RNNRootViewController.m 14KB

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