VLCPlaylistCollectionViewCell.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // VLCPlaylistCollectionViewCell.m
  3. // VLC for iOS
  4. //
  5. // Created by Tamas Timar on 8/30/13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCPlaylistCollectionViewCell.h"
  11. #import "VLCPlaylistViewController.h"
  12. #import "VLCThumbnailsCache.h"
  13. @implementation VLCPlaylistCollectionViewCell
  14. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  15. {
  16. self.removeMediaButton.hidden = !editing;
  17. [self _updatedDisplayedInformationForKeyPath:@"editing"];
  18. }
  19. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  20. {
  21. [self _updatedDisplayedInformationForKeyPath:keyPath];
  22. }
  23. - (void)setMediaObject:(MLFile *)mediaObject
  24. {
  25. if (_mediaObject != mediaObject) {
  26. [_mediaObject removeObserver:self forKeyPath:@"computedThumbnail"];
  27. [_mediaObject removeObserver:self forKeyPath:@"lastPosition"];
  28. [_mediaObject removeObserver:self forKeyPath:@"duration"];
  29. [_mediaObject removeObserver:self forKeyPath:@"fileSizeInBytes"];
  30. [_mediaObject removeObserver:self forKeyPath:@"title"];
  31. [_mediaObject removeObserver:self forKeyPath:@"thumbnailTimeouted"];
  32. [_mediaObject removeObserver:self forKeyPath:@"unread"];
  33. [_mediaObject removeObserver:self forKeyPath:@"albumTrackNumber"];
  34. [_mediaObject removeObserver:self forKeyPath:@"album"];
  35. [_mediaObject removeObserver:self forKeyPath:@"artist"];
  36. [_mediaObject removeObserver:self forKeyPath:@"genre"];
  37. if ([_mediaObject respondsToSelector:@selector(didHide)])
  38. [(MLFile*)_mediaObject didHide];
  39. _mediaObject = mediaObject;
  40. [_mediaObject addObserver:self forKeyPath:@"computedThumbnail" options:0 context:nil];
  41. [_mediaObject addObserver:self forKeyPath:@"lastPosition" options:0 context:nil];
  42. [_mediaObject addObserver:self forKeyPath:@"duration" options:0 context:nil];
  43. [_mediaObject addObserver:self forKeyPath:@"fileSizeInBytes" options:0 context:nil];
  44. [_mediaObject addObserver:self forKeyPath:@"title" options:0 context:nil];
  45. [_mediaObject addObserver:self forKeyPath:@"thumbnailTimeouted" options:0 context:nil];
  46. [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
  47. [_mediaObject addObserver:self forKeyPath:@"albumTrackNumber" options:0 context:nil];
  48. [_mediaObject addObserver:self forKeyPath:@"album" options:0 context:nil];
  49. [_mediaObject addObserver:self forKeyPath:@"artist" options:0 context:nil];
  50. [_mediaObject addObserver:self forKeyPath:@"genre" options:0 context:nil];
  51. if ([_mediaObject respondsToSelector:@selector(willDisplay)])
  52. [(MLFile*)_mediaObject willDisplay];
  53. }
  54. [self _updatedDisplayedInformationForKeyPath:nil];
  55. }
  56. - (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
  57. {
  58. self.albumNameLabel.text = self.artistNameLabel.text = self.seriesNameLabel.text = @"";
  59. if ([self.mediaObject isKindOfClass:[MLFile class]]) {
  60. MLFile *mediaObject = self.mediaObject;
  61. [self _configureForMLFile:mediaObject];
  62. if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) && !mediaObject.isAlbumTrack) {
  63. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:mediaObject];
  64. }
  65. } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
  66. MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;
  67. [self _configureForAlbum:mediaObject];
  68. } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
  69. MLAlbumTrack *mediaObject = (MLAlbumTrack *)self.mediaObject;
  70. [self _configureForAlbumTrack:mediaObject];
  71. } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
  72. MLShow *mediaObject = (MLShow *)self.mediaObject;
  73. [self _configureForShow:mediaObject];
  74. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
  75. MLFile *anyFileFromAnyEpisode = [mediaObject.episodes.anyObject files].anyObject;
  76. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyEpisode];
  77. }
  78. } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
  79. MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
  80. [self _configureForShowEpisode:mediaObject];
  81. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
  82. MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
  83. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
  84. }
  85. }
  86. [self setNeedsDisplay];
  87. }
  88. - (IBAction)removeMedia:(id)sender
  89. {
  90. /* ask user if s/he really wants to delete the media file */
  91. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DELETE_FILE", @"") message:[NSString stringWithFormat:NSLocalizedString(@"DELETE_FILE_LONG", @""), self.mediaObject.title] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_DELETE", @""), nil];
  92. [alert show];
  93. }
  94. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  95. {
  96. if (buttonIndex == 1) {
  97. VLCPlaylistViewController *delegate = (VLCPlaylistViewController*)self.collectionView.delegate;
  98. [delegate removeMediaObject:self.mediaObject];
  99. }
  100. }
  101. #pragma mark - presentation
  102. - (void)_configureForShow:(MLShow *)show
  103. {
  104. self.titleLabel.text = show.name;
  105. NSUInteger count = show.episodes.count;
  106. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  107. NSString *string = @"";
  108. if (show.releaseYear)
  109. string = [NSString stringWithFormat:@"%@ — ", show.releaseYear];
  110. self.subtitleLabel.text = [string stringByAppendingString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", @"") : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", @""), count, show.unreadEpisodes.count]];
  111. } else {
  112. self.artistNameLabel.text = @"";
  113. self.albumNameLabel.text = show.releaseYear;
  114. self.subtitleLabel.text = [NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", @"") : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", @""), count, show.unreadEpisodes.count];
  115. }
  116. self.mediaIsUnreadView.hidden = YES;
  117. self.progressView.hidden = YES;
  118. }
  119. - (void)_configureForAlbumTrack:(MLAlbumTrack *)albumTrack
  120. {
  121. MLFile *anyFileFromTrack = albumTrack.files.anyObject;
  122. if (SYSTEM_RUNS_IOS7_OR_LATER)
  123. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@ — %@", albumTrack.artist, [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue], [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
  124. else {
  125. self.artistNameLabel.text = albumTrack.artist;
  126. self.albumNameLabel.text = [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue];
  127. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
  128. }
  129. self.titleLabel.text = albumTrack.title;
  130. self.thumbnailView.image = nil;
  131. [self _showPositionOfItem:anyFileFromTrack];
  132. }
  133. - (void)_configureForShowEpisode:(MLShowEpisode *)showEpisode
  134. {
  135. self.titleLabel.text = showEpisode.name;
  136. MLFile *anyFileFromEpisode = showEpisode.files.anyObject;
  137. if (self.titleLabel.text.length < 1) {
  138. self.titleLabel.text = [NSString stringWithFormat:@"S%02dE%02d", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue];
  139. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  140. } else
  141. self.subtitleLabel.text = [NSString stringWithFormat:@"S%02dE%02d — %@", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue, [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  142. [self _showPositionOfItem:anyFileFromEpisode];
  143. }
  144. - (void)_configureForAlbum:(MLAlbum *)album
  145. {
  146. self.titleLabel.text = album.name;
  147. MLAlbumTrack *anyTrack = [album.tracks anyObject];
  148. NSUInteger count = album.tracks.count;
  149. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  150. NSMutableString *string = [[NSMutableString alloc] init];
  151. if (anyTrack) {
  152. [string appendString:anyTrack.artist];
  153. [string appendString:@" — "];
  154. }
  155. [string appendFormat:@"%@ — %@", [NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count], album.releaseYear];
  156. self.subtitleLabel.text = string;
  157. } else {
  158. self.artistNameLabel.text = anyTrack? anyTrack.artist: @"";
  159. self.albumNameLabel.text = album.releaseYear;
  160. self.subtitleLabel.text = [NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count];
  161. }
  162. self.thumbnailView.image = nil;
  163. self.mediaIsUnreadView.hidden = YES;
  164. self.progressView.hidden = YES;
  165. }
  166. - (void)_configureForMLFile:(MLFile *)mediaFile
  167. {
  168. if (mediaFile.isAlbumTrack) {
  169. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  170. NSString *string = @"";
  171. if (mediaFile.albumTrack.artist)
  172. string = [NSString stringWithFormat:@"%@ — ", mediaFile.albumTrack.artist];
  173. else if (mediaFile.albumTrack.album.name)
  174. string = [NSString stringWithFormat:@"%@ — ", mediaFile.albumTrack.artist];
  175. self.titleLabel.text = [string stringByAppendingString:(mediaFile.albumTrack.title.length > 1) ? mediaFile.albumTrack.title : mediaFile.title];
  176. } else {
  177. self.artistNameLabel.text = mediaFile.albumTrack.artist;
  178. self.albumNameLabel.text = mediaFile.albumTrack.album.name;
  179. self.titleLabel.text = (mediaFile.albumTrack.title.length > 1) ? mediaFile.albumTrack.title : mediaFile.title;
  180. }
  181. self.thumbnailView.image = nil;
  182. } else
  183. self.titleLabel.text = mediaFile.title;
  184. VLCPlaylistViewController *delegate = (VLCPlaylistViewController*)self.collectionView.delegate;
  185. if (delegate.isEditing)
  186. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %i MB", [VLCTime timeWithNumber:[mediaFile duration]], (int)([mediaFile fileSizeInBytes] / 1e6)];
  187. else {
  188. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[mediaFile duration]]];
  189. if (mediaFile.videoTrack) {
  190. NSString *width = [[mediaFile videoTrack] valueForKey:@"width"];
  191. NSString *height = [[mediaFile videoTrack] valueForKey:@"height"];
  192. if (width.intValue > 0 && height.intValue > 0)
  193. self.subtitleLabel.text = [self.subtitleLabel.text stringByAppendingFormat:@" — %@x%@", width, height];
  194. }
  195. }
  196. [self _showPositionOfItem:mediaFile];
  197. }
  198. - (void)_showPositionOfItem:(MLFile *)mediaItem
  199. {
  200. CGFloat position = mediaItem.lastPosition.floatValue;
  201. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  202. CGFloat duration = mediaItem.duration.floatValue;
  203. if (position > .1f && position < .95f) {
  204. [(UITextView*)self.mediaIsUnreadView setText:[NSString stringWithFormat:NSLocalizedString(@"LIBRARY_MINUTES_LEFT", @""), [[VLCTime timeWithInt:(duration * position - duration)] minuteStringValue]]];
  205. self.mediaIsUnreadView.hidden = NO;
  206. } else if (mediaItem.unread.intValue) {
  207. [(UILabel *)self.mediaIsUnreadView setText:[NSLocalizedString(@"NEW", @"") capitalizedStringWithLocale:[NSLocale currentLocale]]];
  208. self.mediaIsUnreadView.hidden = NO;
  209. } else
  210. self.mediaIsUnreadView.hidden = YES;
  211. } else {
  212. self.progressView.progress = position;
  213. self.progressView.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
  214. [self.progressView setNeedsDisplay];
  215. self.mediaIsUnreadView.hidden = !mediaItem.unread.intValue;
  216. }
  217. }
  218. @end