VLCTrackSelectorView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*****************************************************************************
  2. * VLCTrackSelectorView.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2017 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <caro # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCTrackSelectorView.h"
  13. #import "VLCPlaybackController.h"
  14. #import "VLCTrackSelectorHeaderView.h"
  15. #import "VLCTrackSelectorTableViewCell.h"
  16. #import "UIDevice+VLC.h"
  17. #define TRACK_SELECTOR_TABLEVIEW_CELL @"track selector table view cell"
  18. #define TRACK_SELECTOR_TABLEVIEW_SECTIONHEADER @"track selector table view section header"
  19. @interface VLCTrackSelectorView() <UITableViewDataSource, UITableViewDelegate>
  20. {
  21. UITableView *_trackSelectorTableView;
  22. NSLayoutConstraint *_heightConstraint;
  23. }
  24. @end
  25. @implementation VLCTrackSelectorView
  26. - (instancetype)initWithFrame:(CGRect)frame
  27. {
  28. self = [super initWithFrame:frame];
  29. if(self){
  30. _trackSelectorTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  31. _trackSelectorTableView.delegate = self;
  32. _trackSelectorTableView.dataSource = self;
  33. _trackSelectorTableView.separatorColor = [UIColor clearColor];
  34. _trackSelectorTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  35. _trackSelectorTableView.rowHeight = 44.;
  36. _trackSelectorTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  37. _trackSelectorTableView.sectionHeaderHeight = 28.;
  38. [_trackSelectorTableView registerClass:[VLCTrackSelectorTableViewCell class] forCellReuseIdentifier:TRACK_SELECTOR_TABLEVIEW_CELL];
  39. [_trackSelectorTableView registerClass:[VLCTrackSelectorHeaderView class] forHeaderFooterViewReuseIdentifier:TRACK_SELECTOR_TABLEVIEW_SECTIONHEADER];
  40. _trackSelectorTableView.translatesAutoresizingMaskIntoConstraints = NO;
  41. [self addSubview:_trackSelectorTableView];
  42. [self setupConstraints];
  43. [self configureForDeviceCategory];
  44. }
  45. return self;
  46. }
  47. - (void)configureForDeviceCategory {
  48. _trackSelectorTableView.opaque = NO;
  49. _trackSelectorTableView.backgroundColor = [UIColor clearColor];
  50. _trackSelectorTableView.allowsMultipleSelection = YES;
  51. }
  52. - (void)layoutSubviews
  53. {
  54. CGFloat height = _trackSelectorTableView.contentSize.height;
  55. _heightConstraint.constant = height;
  56. [super layoutSubviews];
  57. }
  58. - (void)setupConstraints
  59. {
  60. _heightConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:44];
  61. _heightConstraint.priority = UILayoutPriorityDefaultHigh;
  62. NSArray *constraints = @[
  63. [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0],
  64. [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
  65. [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0],
  66. [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0],
  67. _heightConstraint,
  68. ];
  69. [NSLayoutConstraint activateConstraints:constraints];
  70. }
  71. #pragma mark - track selector table view
  72. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  73. {
  74. NSInteger ret = 0;
  75. VLCMediaPlayer *mediaPlayer = [VLCPlaybackController sharedInstance].mediaPlayer;
  76. if (_switchingTracksNotChapters) {
  77. if (mediaPlayer.audioTrackIndexes.count > 2)
  78. ret++;
  79. if (mediaPlayer.videoSubTitlesIndexes.count > 1)
  80. ret++;
  81. } else {
  82. if ([mediaPlayer numberOfTitles] > 1)
  83. ret++;
  84. if ([mediaPlayer numberOfChaptersForTitle:mediaPlayer.currentTitleIndex] > 1)
  85. ret++;
  86. }
  87. return ret;
  88. }
  89. - (void)updateView
  90. {
  91. [_trackSelectorTableView reloadData];
  92. }
  93. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  94. {
  95. UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:TRACK_SELECTOR_TABLEVIEW_SECTIONHEADER];
  96. if (!view) {
  97. view = [[VLCTrackSelectorHeaderView alloc] initWithReuseIdentifier:TRACK_SELECTOR_TABLEVIEW_SECTIONHEADER];
  98. }
  99. return view;
  100. }
  101. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  102. {
  103. VLCMediaPlayer *mediaPlayer = [VLCPlaybackController sharedInstance].mediaPlayer;
  104. if (_switchingTracksNotChapters == YES) {
  105. if (mediaPlayer.audioTrackIndexes.count > 2 && section == 0)
  106. return NSLocalizedString(@"CHOOSE_AUDIO_TRACK", nil);
  107. if (mediaPlayer.videoSubTitlesIndexes.count > 1)
  108. return NSLocalizedString(@"CHOOSE_SUBTITLE_TRACK", nil);
  109. } else {
  110. if ([mediaPlayer numberOfTitles] > 1 && section == 0)
  111. return NSLocalizedString(@"CHOOSE_TITLE", nil);
  112. if ([mediaPlayer numberOfChaptersForTitle:mediaPlayer.currentTitleIndex] > 1)
  113. return NSLocalizedString(@"CHOOSE_CHAPTER", nil);
  114. }
  115. return @"unknown track type";
  116. }
  117. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  118. {
  119. VLCTrackSelectorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TRACK_SELECTOR_TABLEVIEW_CELL];
  120. if (!cell) {
  121. cell = [[VLCTrackSelectorTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TRACK_SELECTOR_TABLEVIEW_CELL];
  122. }
  123. NSInteger row = indexPath.row;
  124. NSInteger section = indexPath.section;
  125. VLCMediaPlayer *mediaPlayer = [VLCPlaybackController sharedInstance].mediaPlayer;
  126. if (_switchingTracksNotChapters) {
  127. NSArray *indexArray;
  128. NSString *trackName;
  129. if ([mediaPlayer numberOfAudioTracks] > 2 && section == 0) {
  130. indexArray = mediaPlayer.audioTrackIndexes;
  131. if ([indexArray indexOfObject:[NSNumber numberWithInt:mediaPlayer.currentAudioTrackIndex]] == row)
  132. [cell setShowsCurrentTrack];
  133. NSArray *audioTrackNames = mediaPlayer.audioTrackNames;
  134. if (row < audioTrackNames.count) {
  135. trackName = audioTrackNames[row];
  136. }
  137. } else {
  138. indexArray = mediaPlayer.videoSubTitlesIndexes;
  139. if ([indexArray indexOfObject:[NSNumber numberWithInt:mediaPlayer.currentVideoSubTitleIndex]] == row)
  140. [cell setShowsCurrentTrack];
  141. NSArray *videoSubtitlesNames = mediaPlayer.videoSubTitlesNames;
  142. if (row < videoSubtitlesNames.count) {
  143. trackName = mediaPlayer.videoSubTitlesNames[row];
  144. }
  145. }
  146. if (trackName != nil) {
  147. if ([trackName isEqualToString:@"Disable"]) {
  148. cell.textLabel.text = NSLocalizedString(@"DISABLE_LABEL", nil);
  149. } else {
  150. cell.textLabel.text = trackName;
  151. }
  152. }
  153. } else {
  154. if ([mediaPlayer numberOfTitles] > 1 && section == 0) {
  155. NSArray *titleDescriptions = mediaPlayer.titleDescriptions;
  156. if (row < titleDescriptions.count) {
  157. NSDictionary *description = titleDescriptions[row];
  158. cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", description[VLCTitleDescriptionName], [[VLCTime timeWithNumber:description[VLCTitleDescriptionDuration]] stringValue]];
  159. }
  160. if (row == mediaPlayer.currentTitleIndex)
  161. [cell setShowsCurrentTrack];
  162. } else {
  163. NSArray *chapterDescriptions = [mediaPlayer chapterDescriptionsOfTitle:mediaPlayer.currentTitleIndex];
  164. if (row < chapterDescriptions.count) {
  165. NSDictionary *description = chapterDescriptions[row];
  166. cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", description[VLCChapterDescriptionName], [[VLCTime timeWithNumber:description[VLCChapterDescriptionDuration]] stringValue]];
  167. }
  168. if (row == mediaPlayer.currentChapterIndex)
  169. [cell setShowsCurrentTrack];
  170. }
  171. }
  172. return cell;
  173. }
  174. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  175. {
  176. VLCMediaPlayer *mediaPlayer = [VLCPlaybackController sharedInstance].mediaPlayer;
  177. if (_switchingTracksNotChapters == YES) {
  178. NSInteger audioTrackCount = mediaPlayer.audioTrackIndexes.count;
  179. if (audioTrackCount > 2 && section == 0)
  180. return audioTrackCount;
  181. return mediaPlayer.videoSubTitlesIndexes.count;
  182. } else {
  183. if ([mediaPlayer numberOfTitles] > 1 && section == 0)
  184. return [mediaPlayer numberOfTitles];
  185. else
  186. return [mediaPlayer numberOfChaptersForTitle:mediaPlayer.currentTitleIndex];
  187. }
  188. }
  189. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  190. {
  191. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  192. NSInteger index = indexPath.row;
  193. VLCMediaPlayer *mediaPlayer = [VLCPlaybackController sharedInstance].mediaPlayer;
  194. if (_switchingTracksNotChapters) {
  195. NSArray *indexArray;
  196. if (mediaPlayer.audioTrackIndexes.count > 2 && indexPath.section == 0) {
  197. indexArray = mediaPlayer.audioTrackIndexes;
  198. if (index <= indexArray.count)
  199. mediaPlayer.currentAudioTrackIndex = [indexArray[index] intValue];
  200. } else {
  201. indexArray = mediaPlayer.videoSubTitlesIndexes;
  202. if (index <= indexArray.count)
  203. mediaPlayer.currentVideoSubTitleIndex = [indexArray[index] intValue];
  204. }
  205. } else {
  206. if ([mediaPlayer numberOfTitles] > 1 && indexPath.section == 0)
  207. mediaPlayer.currentTitleIndex = (int)index;
  208. else
  209. mediaPlayer.currentChapterIndex = (int)index;
  210. }
  211. self.alpha = 1.0f;
  212. void (^animationBlock)() = ^() {
  213. self.alpha = 0.0f;;
  214. };
  215. NSTimeInterval animationDuration = .3;
  216. [UIView animateWithDuration:animationDuration animations:animationBlock completion:_completionHandler];
  217. }
  218. @end