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