VLCTrackSelectorView.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 "VLCPlaybackService.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. {
  49. _trackSelectorTableView.opaque = NO;
  50. _trackSelectorTableView.backgroundColor = [UIColor clearColor];
  51. _trackSelectorTableView.allowsMultipleSelection = YES;
  52. }
  53. - (void)layoutSubviews
  54. {
  55. CGFloat height = _trackSelectorTableView.contentSize.height;
  56. _heightConstraint.constant = height;
  57. [super layoutSubviews];
  58. }
  59. - (void)setupConstraints
  60. {
  61. _heightConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:44];
  62. _heightConstraint.priority = UILayoutPriorityDefaultHigh;
  63. NSArray *constraints = @[
  64. [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0],
  65. [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
  66. [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0],
  67. [NSLayoutConstraint constraintWithItem:_trackSelectorTableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0],
  68. _heightConstraint,
  69. ];
  70. [NSLayoutConstraint activateConstraints:constraints];
  71. }
  72. #pragma mark - track selector table view
  73. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  74. {
  75. NSInteger sections = 0;
  76. VLCPlaybackService *playbackController = [VLCPlaybackService sharedInstance];
  77. if (_switchingTracksNotChapters) {
  78. if([playbackController numberOfAudioTracks] > 2)
  79. sections++;
  80. if ([playbackController numberOfVideoSubtitlesIndexes] > 1)
  81. sections++;
  82. } else {
  83. if ([playbackController numberOfTitles] > 1)
  84. sections++;
  85. if (playbackController.indexOfCurrentTitle >= 0)
  86. if ([playbackController numberOfChaptersForCurrentTitle] > 1)
  87. sections++;
  88. }
  89. return sections;
  90. }
  91. - (void)updateView
  92. {
  93. [_trackSelectorTableView reloadData];
  94. [self setNeedsLayout];
  95. [self layoutIfNeeded];
  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. VLCPlaybackService *playbackController = [VLCPlaybackService sharedInstance];
  108. if (_switchingTracksNotChapters) {
  109. if ([playbackController numberOfAudioTracks] > 2 && section == 0)
  110. return NSLocalizedString(@"CHOOSE_AUDIO_TRACK", nil);
  111. if ([playbackController numberOfVideoSubtitlesIndexes] > 1)
  112. return NSLocalizedString(@"CHOOSE_SUBTITLE_TRACK", nil);
  113. } else {
  114. if ([playbackController numberOfTitles] > 1 && section == 0)
  115. return NSLocalizedString(@"CHOOSE_TITLE", nil);
  116. if ([playbackController numberOfChaptersForCurrentTitle] > 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 forIndexPath:indexPath];
  124. NSInteger row = indexPath.row;
  125. NSInteger section = indexPath.section;
  126. VLCPlaybackService *playbackController = [VLCPlaybackService sharedInstance];
  127. if (_switchingTracksNotChapters) {
  128. NSString *trackName;
  129. if ([playbackController numberOfAudioTracks] > 2 && section == 0) {
  130. if ([playbackController indexOfCurrentAudioTrack] == row) {
  131. [cell setShowsCurrentTrack];
  132. }
  133. trackName = [playbackController audioTrackNameAtIndex:row];
  134. } else {
  135. if ([playbackController indexOfCurrentSubtitleTrack] == row) {
  136. [cell setShowsCurrentTrack];
  137. }
  138. trackName = [playbackController videoSubtitleNameAtIndex:row];
  139. }
  140. if ([trackName isEqualToString:@"Disable"]) {
  141. cell.textLabel.text = NSLocalizedString(@"DISABLE_LABEL", nil);
  142. } else {
  143. cell.textLabel.text = trackName;
  144. }
  145. } else {
  146. if ([playbackController numberOfTitles] > 1 && section == 0) {
  147. NSDictionary *description = [playbackController titleDescriptionsDictAtIndex:row];
  148. if(description != nil) {
  149. cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", description[VLCTitleDescriptionName], [[VLCTime timeWithNumber:description[VLCTitleDescriptionDuration]] stringValue]];
  150. }
  151. if (row == [playbackController indexOfCurrentTitle]) {
  152. [cell setShowsCurrentTrack];
  153. }
  154. } else {
  155. NSDictionary *description = [playbackController chapterDescriptionsDictAtIndex:row];
  156. if (description != nil)
  157. cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@)", description[VLCChapterDescriptionName], [[VLCTime timeWithNumber:description[VLCChapterDescriptionDuration]] stringValue]];
  158. }
  159. if (row == [playbackController indexOfCurrentChapter])
  160. [cell setShowsCurrentTrack];
  161. }
  162. return cell;
  163. }
  164. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  165. {
  166. VLCPlaybackService *playbackController = [VLCPlaybackService sharedInstance];
  167. if (_switchingTracksNotChapters) {
  168. if ([playbackController numberOfAudioTracks] > 2 && section == 0)
  169. return [playbackController numberOfAudioTracks];
  170. return [playbackController numberOfVideoSubtitlesIndexes];
  171. } else {
  172. if ([playbackController numberOfTitles] > 1 && section == 0)
  173. return [playbackController numberOfTitles];
  174. else
  175. return [playbackController numberOfChaptersForCurrentTitle];
  176. }
  177. }
  178. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  179. {
  180. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  181. NSInteger index = indexPath.row;
  182. VLCPlaybackService *playbackController = [VLCPlaybackService sharedInstance];
  183. if (_switchingTracksNotChapters) {
  184. if ([playbackController numberOfAudioTracks] > 2 && indexPath.section == 0) {
  185. [playbackController selectAudioTrackAtIndex:index];
  186. } else if (index <= [playbackController numberOfVideoSubtitlesIndexes]) {
  187. [playbackController selectVideoSubtitleAtIndex:index];
  188. }
  189. } else {
  190. if ([playbackController numberOfTitles] > 1 && indexPath.section == 0)
  191. [playbackController selectTitleAtIndex:index];
  192. else
  193. [playbackController selectChapterAtIndex:index];
  194. }
  195. self.alpha = 1.0f;
  196. void (^animationBlock)(void) = ^() {
  197. self.alpha = 0.0f;;
  198. };
  199. NSTimeInterval animationDuration = .3;
  200. [UIView animateWithDuration:animationDuration animations:animationBlock completion:_completionHandler];
  201. }
  202. @end