VLCPlaylistTableViewCell.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*****************************************************************************
  2. * VLCPlaylistTableViewCell.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. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Carola Nitz <nitz.carola #googlemail.com>
  11. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLCPlaylistTableViewCell.h"
  15. #import "VLCLinearProgressIndicator.h"
  16. #import "VLCThumbnailsCache.h"
  17. #import <MediaLibraryKit/MLAlbum.h>
  18. @implementation VLCPlaylistTableViewCell
  19. + (VLCPlaylistTableViewCell *)cellWithReuseIdentifier:(NSString *)ident
  20. {
  21. NSArray *nibContentArray;
  22. if (SYSTEM_RUNS_IOS7_OR_LATER)
  23. nibContentArray = [[NSBundle mainBundle] loadNibNamed:@"VLCFuturePlaylistTableViewCell" owner:nil options:nil];
  24. else
  25. nibContentArray = [[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistTableViewCell" owner:nil options:nil];
  26. NSAssert([nibContentArray count] == 1, @"meh");
  27. NSAssert([[nibContentArray lastObject] isKindOfClass:[VLCPlaylistTableViewCell class]], @"meh meh");
  28. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[nibContentArray lastObject];
  29. cell.multipleSelectionBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
  30. return cell;
  31. }
  32. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  33. {
  34. [self _updatedDisplayedInformationForKeyPath:keyPath];
  35. }
  36. - (void)setMediaObject:(MLFile *)mediaObject
  37. {
  38. if (_mediaObject != mediaObject) {
  39. [_mediaObject removeObserver:self forKeyPath:@"computedThumbnail"];
  40. [_mediaObject removeObserver:self forKeyPath:@"lastPosition"];
  41. [_mediaObject removeObserver:self forKeyPath:@"duration"];
  42. [_mediaObject removeObserver:self forKeyPath:@"fileSizeInBytes"];
  43. [_mediaObject removeObserver:self forKeyPath:@"title"];
  44. [_mediaObject removeObserver:self forKeyPath:@"thumbnailTimeouted"];
  45. [_mediaObject removeObserver:self forKeyPath:@"unread"];
  46. [_mediaObject removeObserver:self forKeyPath:@"albumTrackNumber"];
  47. [_mediaObject removeObserver:self forKeyPath:@"album"];
  48. [_mediaObject removeObserver:self forKeyPath:@"artist"];
  49. [_mediaObject removeObserver:self forKeyPath:@"genre"];
  50. if ([_mediaObject respondsToSelector:@selector(didHide)])
  51. [(MLFile*)_mediaObject didHide];
  52. _mediaObject = mediaObject;
  53. [_mediaObject addObserver:self forKeyPath:@"computedThumbnail" options:0 context:nil];
  54. [_mediaObject addObserver:self forKeyPath:@"lastPosition" options:0 context:nil];
  55. [_mediaObject addObserver:self forKeyPath:@"duration" options:0 context:nil];
  56. [_mediaObject addObserver:self forKeyPath:@"fileSizeInBytes" options:0 context:nil];
  57. [_mediaObject addObserver:self forKeyPath:@"title" options:0 context:nil];
  58. [_mediaObject addObserver:self forKeyPath:@"thumbnailTimeouted" options:0 context:nil];
  59. [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
  60. [_mediaObject addObserver:self forKeyPath:@"albumTrackNumber" options:0 context:nil];
  61. [_mediaObject addObserver:self forKeyPath:@"album" options:0 context:nil];
  62. [_mediaObject addObserver:self forKeyPath:@"artist" options:0 context:nil];
  63. [_mediaObject addObserver:self forKeyPath:@"genre" options:0 context:nil];
  64. if ([_mediaObject respondsToSelector:@selector(willDisplay)])
  65. [(MLFile*)_mediaObject willDisplay];
  66. }
  67. [self _updatedDisplayedInformationForKeyPath:nil];
  68. }
  69. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  70. {
  71. [super setEditing:editing animated:animated];
  72. [self _updatedDisplayedInformationForKeyPath:@"editing"];
  73. }
  74. - (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
  75. {
  76. BOOL isFolder = [self.mediaObject isKindOfClass:[MLLabel class]];
  77. self.thumbnailView.contentMode = isFolder ? UIViewContentModeScaleAspectFit : UIViewContentModeScaleAspectFill;
  78. if ([self.mediaObject isKindOfClass:[MLFile class]]) {
  79. MLFile *mediaObject = (MLFile*)self.mediaObject;
  80. [self _configureForMLFile:mediaObject];
  81. if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])))
  82. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:mediaObject];
  83. } else if (isFolder) {
  84. MLLabel *mediaObject = (MLLabel *)self.mediaObject;
  85. [self _configureForFolder:mediaObject];
  86. if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  87. NSUInteger fileNumber = [mediaObject.files count] > 4 ? 4 : [mediaObject.files count];
  88. if (fileNumber == 0) {
  89. self.thumbnailView.image = [UIImage imageNamed:@"folderIcon"];
  90. } else {
  91. NSArray *files = [mediaObject.files allObjects];
  92. CGSize frameSize = self.thumbnailView.frame.size;
  93. UIImage *displayedImage;
  94. UIGraphicsBeginImageContext(frameSize);
  95. for (NSUInteger i = 0; i < fileNumber; i++) {
  96. MLFile *file = [files objectAtIndex:i];
  97. displayedImage = [VLCThumbnailsCache thumbnailForMediaFile:file];
  98. CGContextRef context = UIGraphicsGetCurrentContext();
  99. CGFloat imagePartWidth = (frameSize.width / fileNumber);
  100. //calculate imagesize to fit to the framewidth
  101. CGFloat ratio = frameSize.width / displayedImage.size.width;
  102. CGSize calculatedImageSize = CGSizeMake(displayedImage.size.width * ratio, displayedImage.size.height * ratio);
  103. //the rect in which the image should be drawn
  104. CGRect clippingRect = CGRectMake(imagePartWidth * i, 0, imagePartWidth, frameSize.height);
  105. CGContextSaveGState(context);
  106. CGContextClipToRect(context, clippingRect);
  107. //take the center of the clippingRect and calculate the offset from the original center
  108. CGFloat centerOffsetX = (imagePartWidth * i + imagePartWidth / 2) - frameSize.width / 2;
  109. //offset to center vertical
  110. CGFloat centerOffsetY = (frameSize.height - calculatedImageSize.height) / 2;
  111. //shift the rect to draw the middle of the image in the clippingrect
  112. CGRect drawingRect = CGRectMake(centerOffsetX, centerOffsetY, calculatedImageSize.width, calculatedImageSize.height);
  113. [displayedImage drawInRect:drawingRect];
  114. //get rid of the old clippingRect
  115. CGContextRestoreGState(context);
  116. }
  117. displayedImage = UIGraphicsGetImageFromCurrentImageContext();
  118. UIGraphicsEndImageContext();
  119. self.thumbnailView.image = displayedImage;
  120. }
  121. }
  122. } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
  123. MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;
  124. [self _configureForAlbum:mediaObject];
  125. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  126. MLFile *anyFileFromAnyTrack = [mediaObject.tracks.anyObject files].anyObject;
  127. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyTrack];
  128. }
  129. } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
  130. MLAlbumTrack *mediaObject = (MLAlbumTrack *)self.mediaObject;
  131. [self _configureForAlbumTrack:mediaObject];
  132. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  133. MLFile *anyFileFromTrack = mediaObject.files.anyObject;
  134. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromTrack];
  135. }
  136. } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
  137. MLShow *mediaObject = (MLShow *)self.mediaObject;
  138. [self _configureForShow:mediaObject];
  139. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  140. MLFile *anyFileFromAnyEpisode = [mediaObject.episodes.anyObject files].anyObject;
  141. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyEpisode];
  142. }
  143. } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
  144. MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
  145. [self _configureForShowEpisode:mediaObject];
  146. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  147. MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
  148. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
  149. }
  150. }
  151. [self setNeedsDisplay];
  152. }
  153. + (CGFloat)heightOfCell
  154. {
  155. if (SYSTEM_RUNS_IOS7_OR_LATER)
  156. return 90.;
  157. return 80.;
  158. }
  159. #pragma mark - presentation
  160. - (void)_configureForShow:(MLShow *)show
  161. {
  162. self.titleLabel.text = show.name;
  163. NSUInteger count = show.episodes.count;
  164. NSString *string = @"";
  165. if (show.releaseYear)
  166. string = [NSString stringWithFormat:@"%@ — ", show.releaseYear];
  167. self.subtitleLabel.text = [string stringByAppendingString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", @"") : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", @""), count, show.unreadEpisodes.count]];
  168. self.mediaIsUnreadView.hidden = YES;
  169. self.progressIndicator.hidden = YES;
  170. }
  171. - (void)_configureForAlbumTrack:(MLAlbumTrack *)albumTrack
  172. {
  173. MLFile *anyFileFromTrack = albumTrack.files.anyObject;
  174. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@ — %@", albumTrack.artist, [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue], [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
  175. self.titleLabel.text = albumTrack.title;
  176. [self _showPositionOfItem:anyFileFromTrack];
  177. }
  178. - (void)_configureForShowEpisode:(MLShowEpisode *)showEpisode
  179. {
  180. self.titleLabel.text = showEpisode.name;
  181. MLFile *anyFileFromEpisode = showEpisode.files.anyObject;
  182. if (self.titleLabel.text.length < 1) {
  183. self.titleLabel.text = [NSString stringWithFormat:@"S%02dE%02d", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue];
  184. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  185. } else
  186. self.subtitleLabel.text = [NSString stringWithFormat:@"S%02dE%02d — %@", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue, [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  187. [self _showPositionOfItem:anyFileFromEpisode];
  188. }
  189. - (void)_configureForAlbum:(MLAlbum *)album
  190. {
  191. self.titleLabel.text = album.name;
  192. MLAlbumTrack *anyTrack = [album.tracks anyObject];
  193. NSUInteger count = album.tracks.count;
  194. NSMutableString *string = [[NSMutableString alloc] init];
  195. if (anyTrack) {
  196. [string appendString:anyTrack.artist];
  197. [string appendString:@" — "];
  198. }
  199. [string appendString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count]];
  200. if (album.releaseYear)
  201. [string appendFormat:@" — %@", album.releaseYear];
  202. self.subtitleLabel.text = string;
  203. self.mediaIsUnreadView.hidden = YES;
  204. self.progressIndicator.hidden = YES;
  205. }
  206. - (void)_configureForFolder:(MLLabel *)label
  207. {
  208. self.titleLabel.text = label.name;
  209. NSUInteger count = label.files.count;
  210. self.subtitleLabel.text = [NSString stringWithFormat:(count == 1) ? NSLocalizedString(@"LIBRARY_TRACK", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACKS", @""), count];
  211. self.mediaIsUnreadView.hidden = YES;
  212. self.progressIndicator.hidden = YES;
  213. }
  214. - (void)_configureForMLFile:(MLFile *)mediaFile
  215. {
  216. if (mediaFile.isAlbumTrack) {
  217. NSString *string = @"";
  218. if (mediaFile.albumTrack.artist)
  219. string = [NSString stringWithFormat:@"%@ — ", mediaFile.albumTrack.artist];
  220. else if (mediaFile.albumTrack.album.name)
  221. string = [NSString stringWithFormat:@"%@ — ", mediaFile.albumTrack.artist];
  222. self.titleLabel.text = [string stringByAppendingString:(mediaFile.albumTrack.title.length > 1) ? mediaFile.albumTrack.title : mediaFile.title];
  223. } else
  224. self.titleLabel.text = mediaFile.title;
  225. if (self.isEditing)
  226. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@", [VLCTime timeWithNumber:[mediaFile duration]], [NSByteCountFormatter stringFromByteCount:[mediaFile fileSizeInBytes] countStyle:NSByteCountFormatterCountStyleFile]];
  227. else {
  228. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[mediaFile duration]]];
  229. if (mediaFile.videoTrack) {
  230. NSString *width = [[mediaFile videoTrack] valueForKey:@"width"];
  231. NSString *height = [[mediaFile videoTrack] valueForKey:@"height"];
  232. if (width.intValue > 0 && height.intValue > 0)
  233. self.subtitleLabel.text = [self.subtitleLabel.text stringByAppendingFormat:@" — %@x%@", width, height];
  234. }
  235. }
  236. [self _showPositionOfItem:mediaFile];
  237. }
  238. - (void)_showPositionOfItem:(MLFile *)mediaItem
  239. {
  240. CGFloat position = mediaItem.lastPosition.floatValue;
  241. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  242. CGFloat duration = mediaItem.duration.floatValue;
  243. if (position > .05f && position < .95f && (duration * position - duration) < -60000) {
  244. [(UITextView*)self.mediaIsUnreadView setText:[NSString stringWithFormat:NSLocalizedString(@"LIBRARY_MINUTES_LEFT", @""), [[VLCTime timeWithInt:(duration * position - duration)] minuteStringValue]]];
  245. self.mediaIsUnreadView.hidden = NO;
  246. } else if (mediaItem.unread.intValue) {
  247. [(UILabel *)self.mediaIsUnreadView setText:[NSLocalizedString(@"NEW", @"") capitalizedStringWithLocale:[NSLocale currentLocale]]];
  248. self.mediaIsUnreadView.hidden = NO;
  249. } else
  250. self.mediaIsUnreadView.hidden = YES;
  251. } else {
  252. self.progressIndicator.progress = position;
  253. self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
  254. [self.progressIndicator setNeedsDisplay];
  255. self.mediaIsUnreadView.hidden = !mediaItem.unread.intValue;
  256. }
  257. }
  258. @end