VLCPlaybackInfoTracksTVViewController.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "VLCPlaybackInfoTracksTVViewController.h"
  12. #import "VLCPlaybackInfoTVCollectionViewCell.h"
  13. #import "VLCPlaybackInfoTVCollectionSectionTitleView.h"
  14. #import "VLCPlaybackInfoCollectionViewDataSource.h"
  15. #import "VLCPlaybackInfoSubtitlesFetcherViewController.h"
  16. #define CONTENT_INSET 20.
  17. @interface VLCPlaybackInfoTracksDataSourceAudio : VLCPlaybackInfoCollectionViewDataSource <UICollectionViewDataSource, UICollectionViewDelegate>
  18. @end
  19. @interface VLCPlaybackInfoTracksDataSourceSubtitle : VLCPlaybackInfoCollectionViewDataSource <UICollectionViewDataSource, UICollectionViewDelegate>
  20. @end
  21. @interface VLCPlaybackInfoTracksTVViewController ()
  22. @property (nonatomic) IBOutlet VLCPlaybackInfoTracksDataSourceAudio *audioDataSource;
  23. @property (nonatomic) IBOutlet VLCPlaybackInfoTracksDataSourceSubtitle *subtitleDataSource;
  24. @end
  25. @implementation VLCPlaybackInfoTracksTVViewController
  26. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  27. {
  28. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  29. if (self) {
  30. self.title = NSLocalizedString(@"TRACK_SELECTION", nil);
  31. }
  32. return self;
  33. }
  34. - (void)viewDidLoad
  35. {
  36. [super viewDidLoad];
  37. UINib *nib = [UINib nibWithNibName:@"VLCPlaybackInfoTVCollectionViewCell" bundle:nil];
  38. NSString *identifier = [VLCPlaybackInfoTVCollectionViewCell identifier];
  39. [self.audioTrackCollectionView registerNib:nib forCellWithReuseIdentifier:identifier];
  40. [self.subtitleTrackCollectionView registerNib:nib forCellWithReuseIdentifier:identifier];
  41. [VLCPlaybackInfoTVCollectionSectionTitleView registerInCollectionView:self.audioTrackCollectionView];
  42. [VLCPlaybackInfoTVCollectionSectionTitleView registerInCollectionView:self.subtitleTrackCollectionView];
  43. NSLocale *currentLocale = [NSLocale currentLocale];
  44. self.audioDataSource.title = [NSLocalizedString(@"AUDIO", nil) uppercaseStringWithLocale:currentLocale];
  45. self.audioDataSource.cellIdentifier = [VLCPlaybackInfoTVCollectionViewCell identifier];
  46. self.subtitleDataSource.title = [NSLocalizedString(@"SUBTITLES", nil) uppercaseStringWithLocale:currentLocale];
  47. self.subtitleDataSource.cellIdentifier = [VLCPlaybackInfoTVCollectionViewCell identifier];
  48. self.subtitleDataSource.parentViewController = self;
  49. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mediaPlayerChanged) name:VLCPlaybackControllerPlaybackMetadataDidChange object:nil];
  50. }
  51. - (void)dealloc
  52. {
  53. [[NSNotificationCenter defaultCenter] removeObserver:self];
  54. }
  55. - (void)viewWillAppear:(BOOL)animated
  56. {
  57. [super viewWillAppear:animated];
  58. [self mediaPlayerChanged];
  59. }
  60. - (CGSize)preferredContentSize
  61. {
  62. CGFloat prefferedHeight = MAX(self.audioTrackCollectionView.contentSize.height, self.subtitleTrackCollectionView.contentSize.height) + CONTENT_INSET;
  63. return CGSizeMake(CGRectGetWidth(self.view.bounds), prefferedHeight);
  64. }
  65. - (void)mediaPlayerChanged
  66. {
  67. [self.audioTrackCollectionView reloadData];
  68. [self.subtitleTrackCollectionView reloadData];
  69. }
  70. - (void)downloadMoreSPU
  71. {
  72. VLCPlaybackInfoSubtitlesFetcherViewController *targetViewController = [[VLCPlaybackInfoSubtitlesFetcherViewController alloc] initWithNibName:nil bundle:nil];
  73. targetViewController.title = NSLocalizedString(@"DOWNLOAD_SUBS_FROM_OSO", nil);
  74. [self presentViewController:targetViewController
  75. animated:YES
  76. completion:nil];
  77. }
  78. @end
  79. @implementation VLCPlaybackInfoTracksDataSourceAudio
  80. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  81. {
  82. return [[VLCPlaybackController sharedInstance] numberOfAudioTracks] + 1;
  83. }
  84. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  85. {
  86. VLCPlaybackInfoTVCollectionViewCell *trackCell = (VLCPlaybackInfoTVCollectionViewCell*)cell;
  87. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  88. NSInteger row = indexPath.row;
  89. NSString *trackName;
  90. trackCell.titleLabel.font = [UIFont systemFontOfSize:29.];
  91. if (row >= [vpc numberOfAudioTracks]) {
  92. if ([[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingUseSPDIF]) {
  93. trackName = [@"✓ " stringByAppendingString:NSLocalizedString(@"USE_SPDIF", nil)];
  94. trackCell.titleLabel.font = [UIFont boldSystemFontOfSize:29.];
  95. } else
  96. trackName = NSLocalizedString(@"USE_SPDIF", nil);
  97. } else {
  98. BOOL isSelected = row == [vpc indexOfCurrentAudioTrack];
  99. trackCell.selectionMarkerVisible = isSelected;
  100. if (isSelected) {
  101. trackCell.titleLabel.font = [UIFont boldSystemFontOfSize:29.];
  102. }
  103. trackName = [vpc audioTrackNameAtIndex:row];
  104. if (trackName != nil) {
  105. if ([trackName isEqualToString:@"Disable"])
  106. trackName = NSLocalizedString(@"DISABLE_LABEL", nil);
  107. }
  108. }
  109. trackCell.titleLabel.text = trackName;
  110. }
  111. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  112. {
  113. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  114. NSInteger row = indexPath.row;
  115. if (row >= [vpc numberOfAudioTracks]) {
  116. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  117. BOOL bValue = ![defaults boolForKey:kVLCSettingUseSPDIF];
  118. [vpc setAudioPassthrough:bValue];
  119. [defaults setBool:bValue forKey:kVLCSettingUseSPDIF];
  120. [defaults synchronize];
  121. /* restart the audio output */
  122. NSInteger currentAudioTrackIndex = [vpc indexOfCurrentAudioTrack];
  123. [vpc selectAudioTrackAtIndex:0];
  124. [vpc selectAudioTrackAtIndex:currentAudioTrackIndex];
  125. } else {
  126. [vpc selectAudioTrackAtIndex:row];
  127. }
  128. [collectionView reloadData];
  129. }
  130. @end
  131. @implementation VLCPlaybackInfoTracksDataSourceSubtitle
  132. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  133. {
  134. return [[VLCPlaybackController sharedInstance] numberOfVideoSubtitlesIndexes] + 1;
  135. }
  136. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  137. {
  138. VLCPlaybackInfoTVCollectionViewCell *trackCell = (VLCPlaybackInfoTVCollectionViewCell*)cell;
  139. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  140. NSInteger row = indexPath.row;
  141. NSString *trackName;
  142. if (row >= [vpc numberOfVideoSubtitlesIndexes]) {
  143. trackName = NSLocalizedString(@"DOWNLOAD_SUBS_FROM_OSO", nil);
  144. } else {
  145. BOOL isSelected = [vpc indexOfCurrentSubtitleTrack] == row;
  146. trackCell.selectionMarkerVisible = isSelected;
  147. trackName = [vpc videoSubtitleNameAtIndex:row];
  148. if (trackName != nil) {
  149. if ([trackName isEqualToString:@"Disable"])
  150. trackName = NSLocalizedString(@"DISABLE_LABEL", nil);
  151. }
  152. }
  153. trackCell.titleLabel.text = trackName;
  154. }
  155. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  156. {
  157. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  158. NSInteger row = indexPath.row;
  159. if (row >= [vpc numberOfVideoSubtitlesIndexes]) {
  160. if (self.parentViewController) {
  161. if ([self.parentViewController respondsToSelector:@selector(downloadMoreSPU)]) {
  162. [self.parentViewController performSelector:@selector(downloadMoreSPU)];
  163. }
  164. }
  165. } else {
  166. [vpc selectVideoSubtitleAtIndex:row];
  167. [collectionView reloadData];
  168. }
  169. }
  170. @end