react-native-navigation的迁移库

RNNRootViewController.m 13KB

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