react-native-navigation的迁移库

RNNRootViewController.m 14KB

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