VLCPlaybackInfoTracksTVViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "VLCPlaybackInfoTrackTVCell.h"
  13. #import "VLCPlaybackInfoTrackTVTitleView.h"
  14. #define CONTENT_INSET 20.
  15. @interface VLCPlaybackInfoTracksDataSource : NSObject
  16. @property (nonatomic, readonly) VLCMediaPlayer *mediaPlayer;
  17. @property (nonatomic) NSString *title;
  18. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
  19. @end
  20. @interface VLCPlaybackInfoTracksDataSourceAudio : VLCPlaybackInfoTracksDataSource <UICollectionViewDataSource, UICollectionViewDelegate>
  21. @end
  22. @interface VLCPlaybackInfoTracksDataSourceSubtitle : VLCPlaybackInfoTracksDataSource <UICollectionViewDataSource, UICollectionViewDelegate>
  23. @end
  24. @interface VLCPlaybackInfoTracksTVViewController ()
  25. @property (nonatomic) IBOutlet VLCPlaybackInfoTracksDataSourceAudio *audioDataSource;
  26. @property (nonatomic) IBOutlet VLCPlaybackInfoTracksDataSourceSubtitle *subtitleDataSource;
  27. @end
  28. @implementation VLCPlaybackInfoTracksTVViewController
  29. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  30. {
  31. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  32. if (self) {
  33. self.title = NSLocalizedString(@"TRACK_SELECTION", nil);
  34. }
  35. return self;
  36. }
  37. - (void)viewDidLoad
  38. {
  39. [super viewDidLoad];
  40. UINib *nib = [UINib nibWithNibName:@"VLCPlaybackInfoTrackTVCell" bundle:nil];
  41. NSString *identifier = [VLCPlaybackInfoTrackTVCell identifier];
  42. [self.audioTrackCollectionView registerNib:nib forCellWithReuseIdentifier:identifier];
  43. [self.subtitleTrackCollectionView registerNib:nib forCellWithReuseIdentifier:identifier];
  44. [VLCPlaybackInfoTrackTVTitleView registerInCollectionView:self.audioTrackCollectionView];
  45. [VLCPlaybackInfoTrackTVTitleView registerInCollectionView:self.subtitleTrackCollectionView];
  46. self.audioDataSource.title = NSLocalizedString(@"AUDIO", nil);
  47. self.subtitleDataSource.title = NSLocalizedString(@"SUBTITLES", nil);
  48. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mediaPlayerChanged) name:VLCPlaybackControllerPlaybackMetadataDidChange object:nil];
  49. }
  50. - (void)dealloc
  51. {
  52. [[NSNotificationCenter defaultCenter] removeObserver:self];
  53. }
  54. - (void)viewWillAppear:(BOOL)animated
  55. {
  56. [super viewWillAppear:animated];
  57. [self mediaPlayerChanged];
  58. }
  59. - (CGSize)preferredContentSize
  60. {
  61. CGFloat prefferedHeight = MAX(self.audioTrackCollectionView.contentSize.height, self.subtitleTrackCollectionView.contentSize.height) + CONTENT_INSET;
  62. return CGSizeMake(CGRectGetWidth(self.view.bounds), prefferedHeight);
  63. }
  64. - (void)mediaPlayerChanged
  65. {
  66. [self.audioTrackCollectionView reloadData];
  67. [self.subtitleTrackCollectionView reloadData];
  68. }
  69. @end
  70. @implementation VLCPlaybackInfoTracksDataSource
  71. - (VLCMediaPlayer *)mediaPlayer
  72. {
  73. return [VLCPlaybackController sharedInstance].mediaPlayer;
  74. }
  75. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  76. {
  77. VLCPlaybackInfoTrackTVTitleView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[VLCPlaybackInfoTrackTVTitleView identifier] forIndexPath:indexPath];
  78. BOOL showTitle = [collectionView numberOfItemsInSection:indexPath.section] != 0;
  79. header.titleLabel.text = showTitle ? self.title : nil;
  80. return header;
  81. }
  82. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  83. {
  84. return [collectionView dequeueReusableCellWithReuseIdentifier:[VLCPlaybackInfoTrackTVCell identifier] forIndexPath:indexPath];
  85. }
  86. @end
  87. @implementation VLCPlaybackInfoTracksDataSourceAudio
  88. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  89. {
  90. return self.mediaPlayer.numberOfAudioTracks;
  91. }
  92. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  93. {
  94. VLCPlaybackInfoTrackTVCell *trackCell = (VLCPlaybackInfoTrackTVCell*)cell;
  95. NSInteger row = indexPath.row;
  96. BOOL isSelected = [self.mediaPlayer.audioTrackIndexes[row] intValue] == self.mediaPlayer.currentAudioTrackIndex;
  97. trackCell.selectionMarkerVisible = isSelected;
  98. NSString *trackName = self.mediaPlayer.audioTrackNames[row];
  99. if (trackName != nil) {
  100. if ([trackName isEqualToString:@"Disable"])
  101. trackName = NSLocalizedString(@"DISABLE_LABEL", nil);
  102. }
  103. trackCell.titleLabel.text = trackName;
  104. }
  105. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  106. {
  107. self.mediaPlayer.currentAudioTrackIndex = [self.mediaPlayer.audioTrackIndexes[indexPath.row] intValue];
  108. [collectionView reloadData];
  109. }
  110. @end
  111. @implementation VLCPlaybackInfoTracksDataSourceSubtitle
  112. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  113. {
  114. return self.mediaPlayer.numberOfSubtitlesTracks;
  115. }
  116. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  117. {
  118. VLCPlaybackInfoTrackTVCell *trackCell = (VLCPlaybackInfoTrackTVCell*)cell;
  119. NSInteger row = indexPath.row;
  120. BOOL isSelected = [self.mediaPlayer.videoSubTitlesIndexes[row] intValue] == self.mediaPlayer.currentVideoSubTitleIndex;
  121. trackCell.selectionMarkerVisible = isSelected;
  122. NSString *trackName = self.mediaPlayer.videoSubTitlesNames[row];
  123. if (trackName != nil) {
  124. if ([trackName isEqualToString:@"Disable"])
  125. trackName = NSLocalizedString(@"DISABLE_LABEL", nil);
  126. }
  127. trackCell.titleLabel.text = trackName;
  128. }
  129. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  130. {
  131. self.mediaPlayer.currentVideoSubTitleIndex = [self.mediaPlayer.videoSubTitlesIndexes[indexPath.row] intValue];
  132. [collectionView reloadData];
  133. }
  134. @end