react-native-navigation的迁移库

RNNRootViewController.m 12KB

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