123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #import "RNNBottomTabsController.h"
- #import "UITabBarController+RNNUtils.h"
-
- @interface RNNBottomTabsController ()
- @property (nonatomic, strong) BottomTabPresenter* bottomTabPresenter;
- @property (nonatomic, strong) RNNDotIndicatorPresenter* dotIndicatorPresenter;
- @end
-
- @implementation RNNBottomTabsController {
- NSUInteger _currentTabIndex;
- BottomTabsBaseAttacher* _bottomTabsAttacher;
-
- }
-
- - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo
- creator:(id<RNNComponentViewCreator>)creator
- options:(RNNNavigationOptions *)options
- defaultOptions:(RNNNavigationOptions *)defaultOptions
- presenter:(RNNBasePresenter *)presenter
- bottomTabPresenter:(BottomTabPresenter *)bottomTabPresenter
- dotIndicatorPresenter:(RNNDotIndicatorPresenter *)dotIndicatorPresenter
- eventEmitter:(RNNEventEmitter *)eventEmitter
- childViewControllers:(NSArray *)childViewControllers
- bottomTabsAttacher:(BottomTabsBaseAttacher *)bottomTabsAttacher {
- _bottomTabsAttacher = bottomTabsAttacher;
- _bottomTabPresenter = bottomTabPresenter;
- _dotIndicatorPresenter = dotIndicatorPresenter;
- self = [super initWithLayoutInfo:layoutInfo creator:creator options:options defaultOptions:defaultOptions presenter:presenter eventEmitter:eventEmitter childViewControllers:childViewControllers];
- if (@available(iOS 13.0, *)) {
- self.tabBar.standardAppearance = [UITabBarAppearance new];
- }
- return self;
- }
-
- - (void)onChildAddToParent:(UIViewController *)child options:(RNNNavigationOptions *)options {
- [_bottomTabPresenter applyOptionsOnWillMoveToParentViewController:options child:child];
- }
-
- - (void)mergeChildOptions:(RNNNavigationOptions *)options child:(UIViewController *)child {
- [super mergeChildOptions:options child:child];
- [_bottomTabPresenter mergeOptions:options resolvedOptions:child.resolveOptions child:[self findViewController:child]];
- [_dotIndicatorPresenter mergeOptions:options resolvedOptions:child.resolveOptions child:[self findViewController:child]];
- }
-
- - (id<UITabBarControllerDelegate>)delegate {
- return self;
- }
-
- - (void)render {
- [_bottomTabsAttacher attach:self];
- }
-
- - (void)viewDidLayoutSubviews {
- [self.presenter viewDidLayoutSubviews];
-
- for (UIView *view in [[self tabBar] subviews]) {
- UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleLongPress:)];
- if ([NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"]) {
- [view addGestureRecognizer: longPressGesture];
- }
- }
-
- [_dotIndicatorPresenter bottomTabsDidLayoutSubviews:self];
- }
-
- - (UIViewController *)getCurrentChild {
- return self.selectedViewController;
- }
-
- - (CGFloat)getBottomTabsHeight {
- return self.tabBar.frame.size.height;
- }
-
- - (void)setSelectedIndexByComponentID:(NSString *)componentID {
- for (id child in self.childViewControllers) {
- UIViewController<RNNLayoutProtocol>* vc = child;
-
- if ([vc conformsToProtocol:@protocol(RNNLayoutProtocol)] && [vc.layoutInfo.componentId isEqualToString:componentID]) {
- [self setSelectedIndex:[self.childViewControllers indexOfObject:child]];
- }
- }
- }
-
- - (void)setSelectedIndex:(NSUInteger)selectedIndex {
- _currentTabIndex = selectedIndex;
- [super setSelectedIndex:selectedIndex];
- }
-
- - (UIStatusBarStyle)preferredStatusBarStyle {
- return [[self presenter] getStatusBarStyle:self.resolveOptions];
- }
-
- #pragma mark UITabBarControllerDelegate
-
- - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
- [self.eventEmitter sendBottomTabSelected:@(tabBarController.selectedIndex) unselected:@(_currentTabIndex)];
- _currentTabIndex = tabBarController.selectedIndex;
- }
-
- - (void)handleLongPress:(UILongPressGestureRecognizer *) recognizer {
- if (recognizer.state == UIGestureRecognizerStateBegan) {
- NSUInteger _index = [self.tabBar.subviews indexOfObject:(UIView *)recognizer.view];
- [self.eventEmitter sendBottomTabLongPressed:@(_index)];
- }
- }
-
- - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
- {
- NSUInteger _index = [tabBarController.viewControllers indexOfObject:viewController];
- [self.eventEmitter sendBottomTabPressed:@(_index)];
-
- if([[viewController resolveOptions].bottomTab.selectTabOnPress getWithDefaultValue:YES]){
- return YES;
- }
-
- return NO;
- }
-
- @end
|