VLCPlaybackInfoTVViewController.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCPlaybackInfoTVViewController.h"
  12. #import "VLCPlaybackInfoPlaybackTVViewController.h"
  13. #import "VLCPlaybackInfoMediaInfoTVViewController.h"
  14. #import "VLCPlaybackInfoTVAnimators.h"
  15. #import "VLCPlaybackInfoTracksTVViewController.h"
  16. #import "VLCPlaybackInfoChaptersTVViewController.h"
  17. // just for appearance reasons
  18. @interface VLCPlaybackInfoTVTabBarController : UITabBarController
  19. @end
  20. @implementation VLCPlaybackInfoTVTabBarController
  21. @end
  22. @interface VLCPlaybackInfoTVViewController ()
  23. {
  24. NSArray<UIViewController<VLCPlaybackInfoPanelTVViewController> *> *_allTabViewControllers;
  25. }
  26. @end
  27. @implementation VLCPlaybackInfoTVViewController
  28. - (NSArray<UIViewController*>*)tabViewControllers
  29. {
  30. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  31. return [_allTabViewControllers filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id<VLCPlaybackInfoPanelTVViewController> _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  32. return [[evaluatedObject class] shouldBeVisibleForPlaybackController:vpc];
  33. }]];
  34. }
  35. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  36. {
  37. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  38. if (self) {
  39. self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
  40. }
  41. return self;
  42. }
  43. - (void)viewDidLoad
  44. {
  45. [super viewDidLoad];
  46. [self setupTabBarItemAppearance];
  47. _allTabViewControllers = @[[[VLCPlaybackInfoChaptersTVViewController alloc] initWithNibName:nil bundle:nil],
  48. [[VLCPlaybackInfoTracksTVViewController alloc] initWithNibName:nil bundle:nil],
  49. [[VLCPlaybackInfoPlaybackTVViewController alloc] initWithNibName:nil bundle:nil],
  50. [[VLCPlaybackInfoMediaInfoTVViewController alloc] initWithNibName:nil bundle:nil],
  51. ];
  52. UITabBarController *controller = [[VLCPlaybackInfoTVTabBarController alloc] init];
  53. controller.delegate = self;
  54. self.tabBarController = controller;
  55. [self addChildViewController:controller];
  56. controller.view.frame = self.containerView.bounds;
  57. controller.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  58. [self.containerView addSubview:controller.view];
  59. [controller didMoveToParentViewController:self];
  60. UISwipeGestureRecognizer *swipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeUpRecognized:)];
  61. swipeUpRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
  62. swipeUpRecognizer.delegate = self;
  63. [self.view addGestureRecognizer:swipeUpRecognizer];
  64. }
  65. - (void)viewWillAppear:(BOOL)animated
  66. {
  67. UITabBarController *tabBarController = self.tabBarController;
  68. UIViewController *oldSelectedVC = tabBarController.selectedViewController;
  69. tabBarController.viewControllers = [self tabViewControllers];
  70. NSUInteger newIndex = [tabBarController.viewControllers indexOfObject:oldSelectedVC];
  71. if (newIndex == NSNotFound) {
  72. newIndex = 0;
  73. }
  74. tabBarController.selectedIndex = newIndex;
  75. [super viewWillAppear:animated];
  76. }
  77. - (BOOL)shouldAutomaticallyForwardAppearanceMethods
  78. {
  79. return YES;
  80. }
  81. - (void)updateViewConstraints
  82. {
  83. [super updateViewConstraints];
  84. UIViewController *viewController = self.tabBarController.selectedViewController;
  85. CGFloat tabBarHeight = CGRectGetHeight(self.tabBarController.tabBar.bounds);
  86. self.tabBarRegiomHeightConstraint.constant = tabBarHeight;
  87. CGFloat controllerHeight = viewController.preferredContentSize.height;
  88. self.containerHeightConstraint.constant = controllerHeight;
  89. }
  90. - (void)setupTabBarItemAppearance
  91. {
  92. UITabBarItem *tabBarItemApprearance = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[[VLCPlaybackInfoTVTabBarController class]]];
  93. NSDictionary *attributesSelected = @{NSForegroundColorAttributeName : [UIColor colorWithWhite:0.75 alpha:1.0]};
  94. [tabBarItemApprearance setTitleTextAttributes:attributesSelected forState:UIControlStateSelected];
  95. NSDictionary *attributesFocused = @{NSForegroundColorAttributeName : [UIColor colorWithWhite:1.0 alpha:1.0]};
  96. [tabBarItemApprearance setTitleTextAttributes:attributesFocused forState:UIControlStateFocused];
  97. }
  98. - (void)swipeUpRecognized:(UISwipeGestureRecognizer *)recognizer
  99. {
  100. [self dismissViewControllerAnimated:YES completion:nil];
  101. }
  102. #pragma mark - GestureRecognizerDelegate
  103. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
  104. {
  105. // FIXME: is there any other way to figure out if the tab bar item is currenlty focused?
  106. UIView *view = [[UIScreen mainScreen] focusedView];
  107. while (view) {
  108. if ([view isKindOfClass:[UITabBar class]]) {
  109. return YES;
  110. }
  111. view = view.superview;
  112. }
  113. return NO;
  114. }
  115. #pragma mark - TabBarControllerDelegate
  116. - (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
  117. animationControllerForTransitionFromViewController:(UIViewController *)fromVC
  118. toViewController:(UIViewController *)toVC
  119. {
  120. VLCPlaybackInfoTabBarTVTransitioningAnimator* animator = [[VLCPlaybackInfoTabBarTVTransitioningAnimator alloc] init];
  121. animator.infoContainerViewController = self;
  122. return animator;
  123. }
  124. @end