react-native-navigation的迁移库

RNNRootViewController.m 13KB

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