react-native-navigation的迁移库

RNNRootViewController.m 14KB

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