react-native-navigation的迁移库

RNNRootViewController.m 14KB

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