react-native-navigation的迁移库

RNNTabBarController.m 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #import "RNNTabBarController.h"
  2. @implementation RNNTabBarController {
  3. NSUInteger _currentTabIndex;
  4. }
  5. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
  6. childViewControllers:(NSArray *)childViewControllers
  7. options:(RNNNavigationOptions *)options
  8. defaultOptions:(RNNNavigationOptions *)defaultOptions
  9. presenter:(RNNTabBarPresenter *)presenter
  10. eventEmitter:(RNNEventEmitter *)eventEmitter {
  11. self = [self initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options defaultOptions:defaultOptions presenter:presenter];
  12. _eventEmitter = eventEmitter;
  13. return self;
  14. }
  15. - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
  16. childViewControllers:(NSArray *)childViewControllers
  17. options:(RNNNavigationOptions *)options
  18. defaultOptions:(RNNNavigationOptions *)defaultOptions
  19. presenter:(RNNTabBarPresenter *)presenter {
  20. self = [super init];
  21. self.delegate = self;
  22. self.options = options;
  23. self.defaultOptions = defaultOptions;
  24. self.layoutInfo = layoutInfo;
  25. self.presenter = presenter;
  26. [self.presenter bindViewController:self];
  27. [self setViewControllers:childViewControllers];
  28. [self.presenter applyOptionsOnInit:self.options];
  29. return self;
  30. }
  31. - (void)willMoveToParentViewController:(UIViewController *)parent {
  32. if (parent) {
  33. [_presenter applyOptionsOnWillMoveToParentViewController:self.resolveOptions];
  34. }
  35. }
  36. - (void)onChildWillAppear {
  37. [_presenter applyOptions:self.resolveOptions];
  38. [((UIViewController<RNNParentProtocol> *)self.parentViewController) onChildWillAppear];
  39. }
  40. - (RNNNavigationOptions *)resolveOptions {
  41. return [(RNNNavigationOptions *)[self.options mergeInOptions:self.getCurrentChild.resolveOptions.copy] withDefault:self.defaultOptions];
  42. }
  43. - (void)mergeOptions:(RNNNavigationOptions *)options {
  44. [_presenter mergeOptions:options currentOptions:self.options defaultOptions:self.defaultOptions];
  45. [((UIViewController<RNNLayoutProtocol> *)self.parentViewController) mergeOptions:options];
  46. }
  47. - (void)overrideOptions:(RNNNavigationOptions *)options {
  48. [self.options overrideOptions:options];
  49. }
  50. - (void)renderTreeAndWait:(BOOL)wait perform:(RNNReactViewReadyCompletionBlock)readyBlock {
  51. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  52. dispatch_group_t group = dispatch_group_create();
  53. for (UIViewController<RNNLayoutProtocol>* childViewController in self.childViewControllers) {
  54. dispatch_group_enter(group);
  55. dispatch_async(dispatch_get_main_queue(), ^{
  56. [childViewController renderTreeAndWait:wait perform:^{
  57. dispatch_group_leave(group);
  58. }];
  59. });
  60. }
  61. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  62. dispatch_async(dispatch_get_main_queue(), ^{
  63. readyBlock();
  64. });
  65. });
  66. }
  67. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  68. return self.selectedViewController.supportedInterfaceOrientations;
  69. }
  70. - (void)setSelectedIndexByComponentID:(NSString *)componentID {
  71. for (id child in self.childViewControllers) {
  72. UIViewController<RNNLayoutProtocol>* vc = child;
  73. if ([vc conformsToProtocol:@protocol(RNNLayoutProtocol)] && [vc.layoutInfo.componentId isEqualToString:componentID]) {
  74. [self setSelectedIndex:[self.childViewControllers indexOfObject:child]];
  75. }
  76. }
  77. }
  78. - (void)setSelectedIndex:(NSUInteger)selectedIndex {
  79. _currentTabIndex = selectedIndex;
  80. [super setSelectedIndex:selectedIndex];
  81. }
  82. - (UIViewController *)getCurrentChild {
  83. return self.selectedViewController;
  84. }
  85. - (UIStatusBarStyle)preferredStatusBarStyle {
  86. return ((UIViewController<RNNParentProtocol>*)self.selectedViewController).preferredStatusBarStyle;
  87. }
  88. #pragma mark UITabBarControllerDelegate
  89. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
  90. [_eventEmitter sendBottomTabSelected:@(tabBarController.selectedIndex) unselected:@(_currentTabIndex)];
  91. _currentTabIndex = tabBarController.selectedIndex;
  92. }
  93. @end