VLCPlaylistCollectionViewCell.m 13 KB

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