react-native-navigation的迁移库

RNNRootViewController.m 12KB

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