VLCPlaybackInfoChaptersTVViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "VLCPlaybackInfoChaptersTVViewController.h"
  12. #import "VLCPlaybackInfoCollectionViewDataSource.h"
  13. #import "VLCPlaybackInfoTVCollectionViewCell.h"
  14. #import "VLCPlaybackInfoTVCollectionSectionTitleView.h"
  15. #define CONTENT_INSET 20.
  16. @interface VLCPlaybackInfoTitlesDataSource : VLCPlaybackInfoCollectionViewDataSource <UICollectionViewDataSource, UICollectionViewDelegate>
  17. // other collectionView which sould be updated when selection changes
  18. @property (nonatomic) UICollectionView *dependendCollectionView;
  19. @end
  20. @interface VLCPlaybackInfoChaptersTVViewController ()
  21. @property (nonatomic) IBOutlet VLCPlaybackInfoTitlesDataSource *titlesDataSource;
  22. @property (nonatomic) IBOutlet VLCPlaybackInfoCollectionViewDataSource *chaptersDataSource;
  23. @end
  24. @implementation VLCPlaybackInfoChaptersTVViewController
  25. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  26. {
  27. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  28. if (self) {
  29. self.title = NSLocalizedString(@"CHAPTER_SELECTION_TITLE", nil);
  30. }
  31. return self;
  32. }
  33. - (void)viewDidLoad
  34. {
  35. [super viewDidLoad];
  36. UINib *nib = [UINib nibWithNibName:@"VLCPlaybackInfoTVCollectionViewCell" bundle:nil];
  37. NSString *identifier = [VLCPlaybackInfoTVCollectionViewCell identifier];
  38. [self.titlesCollectionView registerNib:nib forCellWithReuseIdentifier:identifier];
  39. [VLCPlaybackInfoTVCollectionSectionTitleView registerInCollectionView:self.titlesCollectionView];
  40. [self.chaptersCollectionView registerNib:nib forCellWithReuseIdentifier:identifier];
  41. [VLCPlaybackInfoTVCollectionSectionTitleView registerInCollectionView:self.chaptersCollectionView];
  42. NSLocale *currentLocale = [NSLocale currentLocale];
  43. self.titlesDataSource.title = [NSLocalizedString(@"TITLE", nil) uppercaseStringWithLocale:currentLocale];
  44. self.titlesDataSource.cellIdentifier = [VLCPlaybackInfoTVCollectionViewCell identifier];
  45. self.chaptersDataSource.title = [NSLocalizedString(@"CHAPTER", nil) uppercaseStringWithLocale:currentLocale];
  46. self.chaptersDataSource.cellIdentifier = [VLCPlaybackInfoTVCollectionViewCell identifier];
  47. self.titlesDataSource.dependendCollectionView = self.chaptersCollectionView;
  48. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mediaPlayerChanged) name:VLCPlaybackControllerPlaybackMetadataDidChange object:nil];
  49. }
  50. - (void)dealloc
  51. {
  52. [[NSNotificationCenter defaultCenter] removeObserver:self];
  53. }
  54. + (BOOL)shouldBeVisibleForPlaybackController:(VLCPlaybackController *)vpc
  55. {
  56. return [vpc numberOfChaptersForCurrentTitle] > 1;
  57. }
  58. - (void)viewWillAppear:(BOOL)animated
  59. {
  60. [super viewWillAppear:animated];
  61. [self mediaPlayerChanged];
  62. }
  63. - (CGSize)preferredContentSize
  64. {
  65. CGFloat prefferedHeight = MAX(self.titlesCollectionView.contentSize.height, self.chaptersCollectionView.contentSize.height) + CONTENT_INSET;
  66. return CGSizeMake(CGRectGetWidth(self.view.bounds), prefferedHeight);
  67. }
  68. - (void)mediaPlayerChanged
  69. {
  70. [self.titlesCollectionView reloadData];
  71. [self.chaptersCollectionView reloadData];
  72. }
  73. @end
  74. @interface VLCPlaybackInfoChaptersDataSource : VLCPlaybackInfoCollectionViewDataSource <UICollectionViewDataSource, UICollectionViewDelegate>
  75. @end
  76. @implementation VLCPlaybackInfoTitlesDataSource
  77. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  78. {
  79. return self.mediaPlayer.numberOfTitles;
  80. }
  81. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  82. {
  83. VLCPlaybackInfoTVCollectionViewCell *trackCell = (VLCPlaybackInfoTVCollectionViewCell*)cell;
  84. NSInteger row = indexPath.row;
  85. BOOL isSelected = self.mediaPlayer.currentTitleIndex == row;
  86. trackCell.selectionMarkerVisible = isSelected;
  87. NSDictionary *description = self.mediaPlayer.titleDescriptions[row];
  88. NSString *title = description[VLCTitleDescriptionName];
  89. if (title == nil)
  90. title = [NSString stringWithFormat:@"%@ %li", NSLocalizedString(@"TITLE", nil), row];
  91. NSString *titleName = [NSString stringWithFormat:@"%@ (%@)", title, [[VLCTime timeWithNumber:description[VLCTitleDescriptionDuration]] stringValue]];
  92. trackCell.titleLabel.text = titleName;
  93. }
  94. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  95. {
  96. self.mediaPlayer.currentTitleIndex = (int)indexPath.row;
  97. [collectionView reloadData];
  98. [self.dependendCollectionView reloadData];
  99. }
  100. @end
  101. @implementation VLCPlaybackInfoChaptersDataSource
  102. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  103. {
  104. VLCMediaPlayer *player = self.mediaPlayer;
  105. return [player numberOfChaptersForTitle:player.currentTitleIndex];
  106. }
  107. - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  108. {
  109. VLCPlaybackInfoTVCollectionViewCell *trackCell = (VLCPlaybackInfoTVCollectionViewCell*)cell;
  110. NSInteger row = indexPath.row;
  111. VLCMediaPlayer *player = self.mediaPlayer;
  112. BOOL isSelected = player.currentChapterIndex == row;
  113. trackCell.selectionMarkerVisible = isSelected;
  114. NSArray *chapterDescriptions = [player chapterDescriptionsOfTitle:player.currentTitleIndex];
  115. if (row < chapterDescriptions.count) {
  116. NSDictionary *description = chapterDescriptions[row];
  117. NSString *chapter = description[VLCChapterDescriptionName];
  118. if (chapter == nil)
  119. chapter = [NSString stringWithFormat:@"%@ %li", NSLocalizedString(@"CHAPTER", nil), row];
  120. NSString *chapterTitle = [NSString stringWithFormat:@"%@ (%@)", chapter, [[VLCTime timeWithNumber:description[VLCChapterDescriptionDuration]] stringValue]];
  121. trackCell.titleLabel.text = chapterTitle;
  122. }
  123. }
  124. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  125. {
  126. self.mediaPlayer.currentChapterIndex = (int)indexPath.row;
  127. [collectionView reloadData];
  128. }
  129. @end