react-native-navigation的迁移库

RNNRootViewController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. - (void)onReactViewReady:(RNNReactViewReadyCompletionBlock)readyBlock {
  106. if (self.isExternalViewController) {
  107. readyBlock();
  108. } else {
  109. self.reactViewReadyBlock = readyBlock;
  110. }
  111. }
  112. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  113. [self.eventEmitter sendOnSearchBarUpdated:self.layoutInfo.componentId
  114. text:searchController.searchBar.text
  115. isFocused:searchController.searchBar.isFirstResponder];
  116. }
  117. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  118. [self.eventEmitter sendOnSearchBarCancelPressed:self.layoutInfo.componentId];
  119. }
  120. - (void)initCustomViews {
  121. [self setCustomNavigationTitleView];
  122. [self setCustomNavigationBarView];
  123. [self setCustomNavigationComponentBackground];
  124. if (!_customTitleView) {
  125. [self setTitleViewWithSubtitle];
  126. }
  127. }
  128. - (void)setTitleViewWithSubtitle {
  129. if (self.resolveOptions.topBar.subtitle.text.hasValue) {
  130. RNNTitleViewHelper* titleViewHelper = [[RNNTitleViewHelper alloc] initWithTitleViewOptions:self.resolveOptions.topBar.title subTitleOptions:self.resolveOptions.topBar.subtitle viewController:self];
  131. [titleViewHelper setup];
  132. }
  133. }
  134. - (void)setCustomNavigationTitleView {
  135. if (!_customTitleView && _isBeingPresented) {
  136. if (self.resolveOptions.topBar.title.component.name.hasValue) {
  137. _customTitleView = (RNNReactView*)[_creator createRootViewFromComponentOptions:self.resolveOptions.topBar.title.component];
  138. _customTitleView.backgroundColor = UIColor.clearColor;
  139. NSString* alignment = [self.resolveOptions.topBar.title.component.alignment getWithDefaultValue:@""];
  140. [_customTitleView setAlignment:alignment];
  141. BOOL isCenter = [alignment isEqualToString:@"center"];
  142. __weak RNNReactView *weakTitleView = _customTitleView;
  143. CGRect frame = self.navigationController.navigationBar.bounds;
  144. [_customTitleView setFrame:frame];
  145. [_customTitleView setRootViewDidChangeIntrinsicSize:^(CGSize intrinsicContentSize) {
  146. if (isCenter) {
  147. [weakTitleView setFrame:CGRectMake(0, 0, intrinsicContentSize.width, intrinsicContentSize.height)];
  148. } else {
  149. [weakTitleView setFrame:frame];
  150. }
  151. }];
  152. self.navigationItem.titleView = _customTitleView;
  153. }
  154. } else if (_customTitleView && _customTitleView.superview == nil) {
  155. if ([self.navigationItem.title isKindOfClass:[RNNCustomTitleView class]] && !_customTitleView) {
  156. self.navigationItem.title = nil;
  157. }
  158. self.navigationItem.titleView = _customTitleView;
  159. }
  160. }
  161. - (void)setCustomNavigationBarView {
  162. if (!_customTopBar) {
  163. if (self.resolveOptions.topBar.component.name.hasValue) {
  164. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.resolveOptions.topBar.component];
  165. _customTopBar = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  166. reactView.backgroundColor = UIColor.clearColor;
  167. _customTopBar.backgroundColor = UIColor.clearColor;
  168. [self.navigationController.navigationBar addSubview:_customTopBar];
  169. } else if ([[self.navigationController.navigationBar.subviews lastObject] isKindOfClass:[RNNCustomTitleView class]] && !_customTopBar) {
  170. [[self.navigationController.navigationBar.subviews lastObject] removeFromSuperview];
  171. }
  172. } else if (_customTopBar && _customTopBar.superview == nil) {
  173. if ([[self.navigationController.navigationBar.subviews lastObject] isKindOfClass:[RNNCustomTitleView class]] && !_customTopBar) {
  174. [[self.navigationController.navigationBar.subviews lastObject] removeFromSuperview];
  175. }
  176. [self.navigationController.navigationBar addSubview:_customTopBar];
  177. }
  178. }
  179. - (void)setCustomNavigationComponentBackground {
  180. if (!_customTopBarBackground) {
  181. if (self.resolveOptions.topBar.background.component.name.hasValue) {
  182. RCTRootView *reactView = (RCTRootView*)[_creator createRootViewFromComponentOptions:self.resolveOptions.topBar.background.component];
  183. _customTopBarBackground = [[RNNCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:@"fill"];
  184. [self.navigationController.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  185. } else if (self.navigationController.navigationBar.subviews.count > 1 && [[self.navigationController.navigationBar.subviews objectAtIndex:1] isKindOfClass:[RNNCustomTitleView class]]) {
  186. [[self.navigationController.navigationBar.subviews objectAtIndex:1] removeFromSuperview];
  187. }
  188. } if (_customTopBarBackground && _customTopBarBackground.superview == nil) {
  189. if (self.navigationController.navigationBar.subviews.count && [[self.navigationController.navigationBar.subviews objectAtIndex:1] isKindOfClass:[RNNCustomTitleView class]]) {
  190. [[self.navigationController.navigationBar.subviews objectAtIndex:1] removeFromSuperview];
  191. }
  192. [self.navigationController.navigationBar insertSubview:_customTopBarBackground atIndex:1];
  193. self.navigationController.navigationBar.clipsToBounds = YES;
  194. }
  195. }
  196. -(BOOL)isCustomTransitioned {
  197. return self.resolveOptions.customTransition.animations != nil;
  198. }
  199. - (BOOL)isExternalViewController {
  200. return !self.creator;
  201. }
  202. - (BOOL)prefersStatusBarHidden {
  203. if (self.resolveOptions.statusBar.visible.hasValue) {
  204. return ![self.resolveOptions.statusBar.visible get];
  205. } else if ([self.resolveOptions.statusBar.hideWithTopBar getWithDefaultValue:NO]) {
  206. return self.navigationController.isNavigationBarHidden;
  207. }
  208. return NO;
  209. }
  210. - (UIStatusBarStyle)preferredStatusBarStyle {
  211. if ([[self.resolveOptions.statusBar.style getWithDefaultValue:@"default"] isEqualToString:@"light"]) {
  212. return UIStatusBarStyleLightContent;
  213. } else {
  214. return UIStatusBarStyleDefault;
  215. }
  216. }
  217. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  218. return self.resolveOptions.layout.supportedOrientations;
  219. }
  220. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
  221. RNNRootViewController* vc = (RNNRootViewController*)viewController;
  222. if (![[vc.self.resolveOptions.topBar.backButton.transition getWithDefaultValue:@""] isEqualToString:@"custom"]){
  223. navigationController.delegate = nil;
  224. }
  225. }
  226. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
  227. animationControllerForOperation:(UINavigationControllerOperation)operation
  228. fromViewController:(UIViewController*)fromVC
  229. toViewController:(UIViewController*)toVC {
  230. if (self.animator) {
  231. return self.animator;
  232. } else if (operation == UINavigationControllerOperationPush && self.resolveOptions.animations.push.hasCustomAnimation) {
  233. return [[RNNPushAnimation alloc] initWithScreenTransition:self.resolveOptions.animations.push];
  234. } else if (operation == UINavigationControllerOperationPop && self.resolveOptions.animations.pop.hasCustomAnimation) {
  235. return [[RNNPushAnimation alloc] initWithScreenTransition:self.resolveOptions.animations.pop];
  236. } else {
  237. return nil;
  238. }
  239. return nil;
  240. }
  241. - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
  242. return [[RNNModalAnimation alloc] initWithScreenTransition:self.resolveOptions.animations.showModal isDismiss:NO];
  243. }
  244. - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
  245. return [[RNNModalAnimation alloc] initWithScreenTransition:self.resolveOptions.animations.dismissModal isDismiss:YES];
  246. }
  247. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
  248. return self.previewController;
  249. }
  250. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
  251. if (self.previewCallback) {
  252. self.previewCallback(self);
  253. }
  254. }
  255. - (void)onActionPress:(NSString *)id {
  256. [_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
  257. }
  258. - (UIPreviewAction *) convertAction:(NSDictionary *)action {
  259. NSString *actionId = action[@"id"];
  260. NSString *actionTitle = action[@"title"];
  261. UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
  262. if ([action[@"style"] isEqualToString:@"selected"]) {
  263. actionStyle = UIPreviewActionStyleSelected;
  264. } else if ([action[@"style"] isEqualToString:@"destructive"]) {
  265. actionStyle = UIPreviewActionStyleDestructive;
  266. }
  267. return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  268. [self onActionPress:actionId];
  269. }];
  270. }
  271. - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
  272. NSMutableArray *actions = [[NSMutableArray alloc] init];
  273. for (NSDictionary *previewAction in self.resolveOptions.preview.actions) {
  274. UIPreviewAction *action = [self convertAction:previewAction];
  275. NSDictionary *actionActions = previewAction[@"actions"];
  276. if (actionActions.count > 0) {
  277. NSMutableArray *group = [[NSMutableArray alloc] init];
  278. for (NSDictionary *previewGroupAction in actionActions) {
  279. [group addObject:[self convertAction:previewGroupAction]];
  280. }
  281. UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
  282. [actions addObject:actionGroup];
  283. } else {
  284. [actions addObject:action];
  285. }
  286. }
  287. return actions;
  288. }
  289. -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
  290. [self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
  291. }
  292. /**
  293. * fix for #877, #878
  294. */
  295. -(void)onJsReload {
  296. [self cleanReactLeftovers];
  297. }
  298. /**
  299. * fix for #880
  300. */
  301. -(void)dealloc {
  302. [self cleanReactLeftovers];
  303. }
  304. -(void)cleanReactLeftovers {
  305. [[NSNotificationCenter defaultCenter] removeObserver:self];
  306. [[NSNotificationCenter defaultCenter] removeObserver:self.view];
  307. self.view = nil;
  308. self.navigationItem.titleView = nil;
  309. self.navigationItem.rightBarButtonItems = nil;
  310. self.navigationItem.leftBarButtonItems = nil;
  311. _customTopBar = nil;
  312. _customTitleView = nil;
  313. _customTopBarBackground = nil;
  314. }
  315. @end