VLCTrackSelectorView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. if ([[UIDevice currentDevice] VLCSpeedCategory] >= 3) {
  49. _trackSelectorTableView.opaque = NO;
  50. _trackSelectorTableView.backgroundColor = [UIColor clearColor];
  51. } else {
  52. _trackSelectorTableView.backgroundColor = [UIColor blackColor];
  53. }
  54. _trackSelectorTableView.allowsMultipleSelection = YES;
  55. }
  56. - (void)layoutSubviews
  57. {
  58. CGFloat height = _trackSelectorTableView.contentSize.height;
  59. _heightConstraint.constant = height;
  60. [super layoutSubviews];
  61. }
  62. - (void)setupConstraints
  63. {
  64. _heightConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:44];
  65. _heightConstraint.priority = UILayoutPriorityDefaultHigh;
  66. NSArray *constraints = @[
  67. [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0],
  68. [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
  69. [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0],
  70. [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0],
  71. _heightConstraint,
  72. ];
  73. [NSLayoutConstraint activateConstraints:constraints];
  74. }
  75. #pragma mark - track selector table view
  76. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  77. {
  78. NSInteger ret = 0;
  79. VLCMediaPlayer *mediaPlayer = [VLCPlaybackController sharedInstance].mediaPlayer;
  80. if (_switchingTracksNotChapters) {
  81. if (mediaPlayer.audioTrackIndexes.count > 2)
  82. ret++;
  83. if (mediaPlayer.videoSubTitlesIndexes.count > 1)
  84. ret++;
  85. } else {
  86. if ([mediaPlayer numberOfTitles] > 1)
  87. ret++;
  88. if ([mediaPlayer numberOfChaptersForTitle:mediaPlayer.currentTitleIndex] > 1)
  89. ret++;
  90. }
  91. return ret;
  92. }
  93. - (void)updateView
  94. {
  95. [_trackSelectorTableView reloadData];
  96. }
  97. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  98. {
  99. UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:TRACK_SELECTOR_TABLEVIEW_SECTIONHEADER];
  100. if (!view) {
  101. view = [[VLCTrackSelectorHeaderView alloc] initWithReuseIdentifier:TRACK_SELECTOR_TABLEVIEW_SECTIONHEADER];
  102. }
  103. return view;
  104. }
  105. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  106. {
  107. VLCMediaPlayer *mediaPlayer = [VLCPlaybackController sharedInstance].mediaPlayer;
  108. if (_switchingTracksNotChapters == YES) {
  109. if (mediaPlayer.audioTrackIndexes.count > 2 && section == 0)
  110. return NSLocalizedString(@"CHOOSE_AUDIO_TRACK", nil);
  111. if (mediaPlayer.videoSubTitlesIndexes.count > 1)
  112. return NSLocalizedString(@"CHOOSE_SUBTITLE_TRACK", nil);
  113. } else {
  114. if ([mediaPlayer numberOfTitles] > 1 && section == 0)
  115. return NSLocalizedString(@"CHOOSE_TITLE", nil);
  116. if ([mediaPlayer numberOfChaptersForTitle:mediaPlayer.currentTitleIndex] > 1)
  117. return NSLocalizedString(@"CHOOSE_CHAPTER", nil);
  118. }
  119. return @"unknown track type";
  120. }
  121. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  122. {
  123. VLCTrackSelectorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TRACK_SELECTOR_TABLEVIEW_CELL];
  124. if (!cell) {
  125. cell = [[VLCTrackSelectorTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TRACK_SELECTOR_TABLEVIEW_CELL];
  126. }
  127. NSInteger row = indexPath.row;
  128. NSInteger section = indexPath.section;
  129. VLCMediaPlayer *mediaPlayer = [VLCPlaybackController sharedInstance].mediaPlayer;
  130. BOOL cellShowsCurrentTrack = NO;
  131. if (_switchingTracksNotChapters) {
  132. NSArray *indexArray;
  133. NSString *trackName;
  134. if ([mediaPlayer numberOfAudioTracks] > 2 && section == 0) {
  135. indexArray = mediaPlayer.audioTrackIndexes;
  136. if ([indexArray indexOfObject:[NSNumber numberWithInt:mediaPlayer.currentAudioTrackIndex]] == row)
  137. cellShowsCurrentTrack = YES;
  138. NSArray *audioTrackNames = mediaPlayer.audioTrackNames;
  139. if (row < audioTrackNames.count) {
  140. trackName = audioTrackNames[row];
  141. }
  142. } else {
  143. indexArray = mediaPlayer.videoSubTitlesIndexes;
  144. if ([indexArray indexOfObject:[NSNumber numberWithInt:mediaPlayer.currentVideoSubTitleIndex]] == row)
  145. cellShowsCurrentTrack = YES;
  146. NSArray *videoSubtitlesNames = mediaPlayer.videoSubTitlesNames;
  147. if (row < videoSubtitlesNames.count) {
  148. trackName = mediaPlayer.videoSubTitlesNames[row];
  149. }
  150. }
  151. if (trackName != nil) {
  152. if ([trackName isEqualToString:@"Disable"]) {
  153. cell.textLabel.text = NSLocalizedString(@"DISABLE_LABEL", nil);
  154. } else {
  155. cell.textLabel.text = trackName;
  156. }
  157. }
  158. } else {
  159. if ([mediaPlayer numberOfTitles] > 1 && section == 0) {
  160. NSArray *titleDescriptions = mediaPlayer.titleDescriptions;
  161. if (row < titleDescriptions.count) {
  162. NSDictionary *description = titleDescriptions[row];
  163. cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", description[VLCTitleDescriptionName], [[VLCTime timeWithNumber:description[VLCTitleDescriptionDuration]] stringValue]];
  164. }
  165. if (row == mediaPlayer.currentTitleIndex)
  166. cellShowsCurrentTrack = YES;
  167. } else {
  168. NSArray *chapterDescriptions = [mediaPlayer chapterDescriptionsOfTitle:mediaPlayer.currentTitleIndex];
  169. if (row < chapterDescriptions.count) {
  170. NSDictionary *description = chapterDescriptions[row];
  171. cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", description[VLCChapterDescriptionName], [[VLCTime timeWithNumber:description[VLCChapterDescriptionDuration]] stringValue]];
  172. }
  173. if (row == mediaPlayer.currentChapterIndex)
  174. cellShowsCurrentTrack = YES;
  175. }
  176. }
  177. [cell setShowsCurrentTrack:cellShowsCurrentTrack];
  178. return cell;
  179. }
  180. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  181. {
  182. VLCMediaPlayer *mediaPlayer = [VLCPlaybackController sharedInstance].mediaPlayer;
  183. if (_switchingTracksNotChapters == YES) {
  184. NSInteger audioTrackCount = mediaPlayer.audioTrackIndexes.count;
  185. if (audioTrackCount > 2 && section == 0)
  186. return audioTrackCount;
  187. return mediaPlayer.videoSubTitlesIndexes.count;
  188. } else {
  189. if ([mediaPlayer numberOfTitles] > 1 && section == 0)
  190. return [mediaPlayer numberOfTitles];
  191. else
  192. return [mediaPlayer numberOfChaptersForTitle:mediaPlayer.currentTitleIndex];
  193. }
  194. }
  195. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  196. {
  197. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  198. NSInteger index = indexPath.row;
  199. VLCMediaPlayer *mediaPlayer = [VLCPlaybackController sharedInstance].mediaPlayer;
  200. if (_switchingTracksNotChapters) {
  201. NSArray *indexArray;
  202. if (mediaPlayer.audioTrackIndexes.count > 2 && indexPath.section == 0) {
  203. indexArray = mediaPlayer.audioTrackIndexes;
  204. if (index <= indexArray.count)
  205. mediaPlayer.currentAudioTrackIndex = [indexArray[index] intValue];
  206. } else {
  207. indexArray = mediaPlayer.videoSubTitlesIndexes;
  208. if (index <= indexArray.count)
  209. mediaPlayer.currentVideoSubTitleIndex = [indexArray[index] intValue];
  210. }
  211. } else {
  212. if ([mediaPlayer numberOfTitles] > 1 && indexPath.section == 0)
  213. mediaPlayer.currentTitleIndex = (int)index;
  214. else
  215. mediaPlayer.currentChapterIndex = (int)index;
  216. }
  217. self.alpha = 1.0f;
  218. void (^animationBlock)() = ^() {
  219. self.alpha = 0.0f;;
  220. };
  221. NSTimeInterval animationDuration = .3;
  222. [UIView animateWithDuration:animationDuration animations:animationBlock completion:_completionHandler];
  223. }
  224. @end