react-native-navigation的迁移库

RNNRootViewController.m 14KB

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