VLCPlaybackInfoTVViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. VLCPlaybackService *vpc = [VLCPlaybackService 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. if ([UIScreen mainScreen].traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  68. self.visualEffectView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
  69. }
  70. UITabBarController *tabBarController = self.tabBarController;
  71. UIViewController *oldSelectedVC = tabBarController.selectedViewController;
  72. tabBarController.viewControllers = [self tabViewControllers];
  73. NSUInteger newIndex = [tabBarController.viewControllers indexOfObject:oldSelectedVC];
  74. if (newIndex == NSNotFound) {
  75. newIndex = 0;
  76. }
  77. tabBarController.selectedIndex = newIndex;
  78. [super viewWillAppear:animated];
  79. }
  80. - (BOOL)shouldAutomaticallyForwardAppearanceMethods
  81. {
  82. return YES;
  83. }
  84. - (void)updateViewConstraints
  85. {
  86. [super updateViewConstraints];
  87. UIViewController *viewController = self.tabBarController.selectedViewController;
  88. CGFloat tabBarHeight = CGRectGetHeight(self.tabBarController.tabBar.bounds);
  89. self.tabBarRegiomHeightConstraint.constant = tabBarHeight;
  90. CGFloat controllerHeight = viewController.preferredContentSize.height;
  91. self.containerHeightConstraint.constant = controllerHeight;
  92. }
  93. - (void)setupTabBarItemAppearance
  94. {
  95. UITabBarItem *tabBarItemApprearance = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[[VLCPlaybackInfoTVTabBarController class]]];
  96. NSDictionary *attributesSelected = @{NSForegroundColorAttributeName : [UIColor colorWithWhite:0.75 alpha:1.0]};
  97. [tabBarItemApprearance setTitleTextAttributes:attributesSelected forState:UIControlStateSelected];
  98. NSDictionary *attributesFocused = @{NSForegroundColorAttributeName : [UIColor colorWithWhite:1.0 alpha:1.0]};
  99. [tabBarItemApprearance setTitleTextAttributes:attributesFocused forState:UIControlStateFocused];
  100. }
  101. - (void)swipeUpRecognized:(UISwipeGestureRecognizer *)recognizer
  102. {
  103. [self dismissViewControllerAnimated:YES completion:nil];
  104. }
  105. #pragma mark - GestureRecognizerDelegate
  106. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
  107. {
  108. // FIXME: is there any other way to figure out if the tab bar item is currenlty focused?
  109. UIView *view = [[UIScreen mainScreen] focusedView];
  110. while (view) {
  111. if ([view isKindOfClass:[UITabBar class]]) {
  112. return YES;
  113. }
  114. view = view.superview;
  115. }
  116. return NO;
  117. }
  118. #pragma mark - TabBarControllerDelegate
  119. - (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
  120. animationControllerForTransitionFromViewController:(UIViewController *)fromVC
  121. toViewController:(UIViewController *)toVC
  122. {
  123. VLCPlaybackInfoTabBarTVTransitioningAnimator* animator = [[VLCPlaybackInfoTabBarTVTransitioningAnimator alloc] init];
  124. animator.infoContainerViewController = self;
  125. return animator;
  126. }
  127. @end