react-native-navigation的迁移库

RNNRootViewController.m 13KB

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