react-native-navigation的迁移库

RNNRootViewController.m 12KB

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