react-native-navigation的迁移库

RNNRootViewController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 resolvedOptions:self.resolveOptions];
  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. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  219. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  220. if (![[vc.optionsWithDefault.topBar.backButton.transition getWithDefaultValue:@""] isEqualToString:@"custom"]){
  221. navigationController.delegate = nil;
  222. }
  223. }
  224. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  225. animationControllerForOperation:(UINavigationControllerOperation)operation
  226. fromViewController:(UIViewController*)fromVC
  227. toViewController:(UIViewController*)toVC {
  228. if (self.animator) {
  229. return self.animator;
  230. } else if (operation == UINavigationControllerOperationPush && self.optionsWithDefault.animations.push.hasCustomAnimation) {
  231. return [[RNNPushAnimation alloc] initWithScreenTransition:self.optionsWithDefault.animations.push];
  232. } else if (operation == UINavigationControllerOperationPop && self.optionsWithDefault.animations.pop.hasCustomAnimation) {
  233. return [[RNNPushAnimation alloc] initWithScreenTransition:self.optionsWithDefault.animations.pop];
  234. } else {
  235. return nil;
  236. }
  237. return nil;
  238. }
  239. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
  240. return [[RNNModalAnimation alloc] initWithScreenTransition:self.optionsWithDefault.animations.showModal isDismiss:NO];
  241. }
  242. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  243. return [[RNNModalAnimation alloc] initWithScreenTransition:self.optionsWithDefault.animations.dismissModal isDismiss:YES];
  244. }
  245. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  246. return self.previewController;
  247. }
  248. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  249. if (self.previewCallback) {
  250. self.previewCallback(self);
  251. }
  252. }
  253. - (void)onActionPress:(NSString *)id {
  254. [_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
  255. }
  256. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  257. NSString *actionId = action[@"id"];
  258. NSString *actionTitle = action[@"title"];
  259. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  260. if ([action[@"style"] isEqualToString:@"selected"]) {
  261. actionStyle = UIPreviewActionStyleSelected;
  262. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  263. actionStyle = UIPreviewActionStyleDestructive;
  264. }
  265. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  266. [self onActionPress:actionId];
  267. }];
  268. }
  269. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  270. NSMutableArray *actions = [[NSMutableArray alloc] init];
  271. for (NSDictionary *previewAction in self.options.preview.actions) {
  272. UIPreviewAction *action = [self convertAction:previewAction];
  273. NSDictionary *actionActions = previewAction[@"actions"];
  274. if (actionActions.count > 0) {
  275. NSMutableArray *group = [[NSMutableArray alloc] init];
  276. for (NSDictionary *previewGroupAction in actionActions) {
  277. [group addObject:[self convertAction:previewGroupAction]];
  278. }
  279. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  280. [actions addObject:actionGroup];
  281. } else {
  282. [actions addObject:action];
  283. }
  284. }
  285. return actions;
  286. }
  287. -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
  288. [self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
  289. }
  290. /**
  291. * fix for #877, #878
  292. */
  293. -(void)onJsReload {
  294. [self cleanReactLeftovers];
  295. }
  296. /**
  297. * fix for #880
  298. */
  299. -(void)dealloc {
  300. [self cleanReactLeftovers];
  301. }
  302. -(void)cleanReactLeftovers {
  303. [[NSNotificationCenter defaultCenter] removeObserver:self];
  304. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  305. self.view = nil;
  306. self.navigationItem.titleView = nil;
  307. self.navigationItem.rightBarButtonItems = nil;
  308. self.navigationItem.leftBarButtonItems = nil;
  309. _customTopBar = nil;
  310. _customTitleView = nil;
  311. _customTopBarBackground = nil;
  312. }
  313. @end