react-native-navigation的迁移库

RNNRootViewController.m 14KB

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