VLCPlaylistTableViewCell.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. if ([_mediaObject isKindOfClass:[MLLabel class]]) {
  40. [_mediaObject removeObserver:self forKeyPath:@"files"];
  41. [_mediaObject removeObserver:self forKeyPath:@"name"];
  42. } else if ([_mediaObject isKindOfClass:[MLShow class]])
  43. [_mediaObject removeObserver:self forKeyPath:@"episodes"];
  44. else if ([mediaObject isKindOfClass:[MLFile class]]) {
  45. [_mediaObject removeObserver:self forKeyPath:@"computedThumbnail"];
  46. [_mediaObject removeObserver:self forKeyPath:@"lastPosition"];
  47. [_mediaObject removeObserver:self forKeyPath:@"duration"];
  48. [_mediaObject removeObserver:self forKeyPath:@"fileSizeInBytes"];
  49. [_mediaObject removeObserver:self forKeyPath:@"title"];
  50. [_mediaObject removeObserver:self forKeyPath:@"thumbnailTimeouted"];
  51. [_mediaObject removeObserver:self forKeyPath:@"unread"];
  52. [_mediaObject removeObserver:self forKeyPath:@"albumTrackNumber"];
  53. [_mediaObject removeObserver:self forKeyPath:@"album"];
  54. [_mediaObject removeObserver:self forKeyPath:@"artist"];
  55. [_mediaObject removeObserver:self forKeyPath:@"genre"];
  56. [_mediaObject removeObserver:self forKeyPath:@"labels"];
  57. [(MLFile*)_mediaObject didHide];
  58. }
  59. _mediaObject = mediaObject;
  60. if ([_mediaObject isKindOfClass:[MLLabel class]]) {
  61. [_mediaObject addObserver:self forKeyPath:@"files" options:0 context:nil];
  62. [_mediaObject addObserver:self forKeyPath:@"name" options:0 context:nil];
  63. } else if ([_mediaObject isKindOfClass:[MLShow class]])
  64. [_mediaObject addObserver:self forKeyPath:@"episodes" options:0 context:nil];
  65. else if ([_mediaObject isKindOfClass:[MLFile class]]) {
  66. [_mediaObject addObserver:self forKeyPath:@"computedThumbnail" options:0 context:nil];
  67. [_mediaObject addObserver:self forKeyPath:@"lastPosition" options:0 context:nil];
  68. [_mediaObject addObserver:self forKeyPath:@"duration" options:0 context:nil];
  69. [_mediaObject addObserver:self forKeyPath:@"fileSizeInBytes" options:0 context:nil];
  70. [_mediaObject addObserver:self forKeyPath:@"title" options:0 context:nil];
  71. [_mediaObject addObserver:self forKeyPath:@"thumbnailTimeouted" options:0 context:nil];
  72. [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
  73. [_mediaObject addObserver:self forKeyPath:@"albumTrackNumber" options:0 context:nil];
  74. [_mediaObject addObserver:self forKeyPath:@"album" options:0 context:nil];
  75. [_mediaObject addObserver:self forKeyPath:@"artist" options:0 context:nil];
  76. [_mediaObject addObserver:self forKeyPath:@"genre" options:0 context:nil];
  77. [_mediaObject addObserver:self forKeyPath:@"labels" options:0 context:nil];
  78. [(MLFile*)_mediaObject willDisplay];
  79. }
  80. }
  81. [self _updatedDisplayedInformationForKeyPath:nil];
  82. }
  83. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  84. {
  85. [super setEditing:editing animated:animated];
  86. [self _updatedDisplayedInformationForKeyPath:@"editing"];
  87. }
  88. - (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
  89. {
  90. BOOL isFolder = [self.mediaObject isKindOfClass:[MLLabel class]];
  91. self.thumbnailView.contentMode = isFolder ? UIViewContentModeScaleAspectFit : UIViewContentModeScaleAspectFill;
  92. if ([self.mediaObject isKindOfClass:[MLFile class]]) {
  93. MLFile *mediaObject = (MLFile*)self.mediaObject;
  94. [self _configureForMLFile:mediaObject];
  95. if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])))
  96. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:mediaObject];
  97. } else if (isFolder) {
  98. MLLabel *mediaObject = (MLLabel *)self.mediaObject;
  99. [self _configureForFolder:mediaObject];
  100. BOOL forceRefresh = NO;
  101. if ([keyPath isEqualToString:@"files"] || [keyPath isEqualToString:@"labels"] || !keyPath)
  102. forceRefresh = YES;
  103. if (forceRefresh || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  104. if (mediaObject.files.count == 0)
  105. self.thumbnailView.image = [UIImage imageNamed:@"folderIcon"];
  106. else
  107. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForLabel:mediaObject forceRefresh:forceRefresh];
  108. }
  109. } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
  110. MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;
  111. [self _configureForAlbum:mediaObject];
  112. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  113. MLFile *anyFileFromAnyTrack = [mediaObject.tracks.anyObject files].anyObject;
  114. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyTrack];
  115. }
  116. } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
  117. MLAlbumTrack *mediaObject = (MLAlbumTrack *)self.mediaObject;
  118. [self _configureForAlbumTrack:mediaObject];
  119. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  120. MLFile *anyFileFromTrack = mediaObject.files.anyObject;
  121. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromTrack];
  122. }
  123. } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
  124. MLShow *mediaObject = (MLShow *)self.mediaObject;
  125. [self _configureForShow:mediaObject];
  126. BOOL forceRefresh = NO;
  127. if ([keyPath isEqualToString:@"episodes"])
  128. forceRefresh = YES;
  129. if ([keyPath isEqualToString:@"computedThumbnail"] || forceRefresh || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  130. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForShow:mediaObject forceRefresh:forceRefresh];
  131. }
  132. } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
  133. MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
  134. [self _configureForShowEpisode:mediaObject];
  135. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  136. MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
  137. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
  138. }
  139. }
  140. [self setNeedsDisplay];
  141. }
  142. + (CGFloat)heightOfCell
  143. {
  144. if (SYSTEM_RUNS_IOS7_OR_LATER)
  145. return 90.;
  146. return 80.;
  147. }
  148. #pragma mark - presentation
  149. - (void)_configureForShow:(MLShow *)show
  150. {
  151. self.titleLabel.text = show.name;
  152. NSUInteger count = show.episodes.count;
  153. NSString *string = @"";
  154. if (show.releaseYear)
  155. string = [NSString stringWithFormat:@"%@ — ", show.releaseYear];
  156. self.subtitleLabel.text = [string stringByAppendingString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", @"") : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", @""), count, show.unreadEpisodes.count]];
  157. self.mediaIsUnreadView.hidden = YES;
  158. self.progressIndicator.hidden = YES;
  159. }
  160. - (void)_configureForAlbumTrack:(MLAlbumTrack *)albumTrack
  161. {
  162. MLFile *anyFileFromTrack = albumTrack.files.anyObject;
  163. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@ — %@", albumTrack.artist, [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue], [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
  164. self.titleLabel.text = albumTrack.title;
  165. [self _showPositionOfItem:anyFileFromTrack];
  166. }
  167. - (void)_configureForShowEpisode:(MLShowEpisode *)showEpisode
  168. {
  169. self.titleLabel.text = showEpisode.name;
  170. MLFile *anyFileFromEpisode = showEpisode.files.anyObject;
  171. if (self.titleLabel.text.length < 1) {
  172. self.titleLabel.text = [NSString stringWithFormat:@"S%02dE%02d", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue];
  173. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  174. } else
  175. self.subtitleLabel.text = [NSString stringWithFormat:@"S%02dE%02d — %@", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue, [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  176. [self _showPositionOfItem:anyFileFromEpisode];
  177. }
  178. - (void)_configureForAlbum:(MLAlbum *)album
  179. {
  180. self.titleLabel.text = album.name;
  181. MLAlbumTrack *anyTrack = [album.tracks anyObject];
  182. NSUInteger count = album.tracks.count;
  183. NSMutableString *string = [[NSMutableString alloc] init];
  184. if (anyTrack) {
  185. [string appendString:anyTrack.artist];
  186. [string appendString:@" — "];
  187. }
  188. [string appendString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count]];
  189. if (album.releaseYear)
  190. [string appendFormat:@" — %@", album.releaseYear];
  191. self.subtitleLabel.text = string;
  192. self.mediaIsUnreadView.hidden = YES;
  193. self.progressIndicator.hidden = YES;
  194. }
  195. - (void)_configureForFolder:(MLLabel *)label
  196. {
  197. self.titleLabel.text = label.name;
  198. NSUInteger count = label.files.count;
  199. self.subtitleLabel.text = [NSString stringWithFormat:(count == 1) ? NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @"") : NSLocalizedString(@"LIBRARY_TRACKS", @""), count];
  200. self.mediaIsUnreadView.hidden = YES;
  201. self.progressIndicator.hidden = YES;
  202. }
  203. - (void)_configureForMLFile:(MLFile *)mediaFile
  204. {
  205. if (mediaFile.isAlbumTrack) {
  206. NSString *string = @"";
  207. if (mediaFile.albumTrack.artist)
  208. string = [NSString stringWithFormat:@"%@ — ", mediaFile.albumTrack.artist];
  209. else if (mediaFile.albumTrack.album.name)
  210. string = [NSString stringWithFormat:@"%@ — ", mediaFile.albumTrack.artist];
  211. self.titleLabel.text = [string stringByAppendingString:(mediaFile.albumTrack.title.length > 1) ? mediaFile.albumTrack.title : mediaFile.title];
  212. } else
  213. self.titleLabel.text = mediaFile.title;
  214. if (self.isEditing)
  215. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@", [VLCTime timeWithNumber:[mediaFile duration]], [NSByteCountFormatter stringFromByteCount:[mediaFile fileSizeInBytes] countStyle:NSByteCountFormatterCountStyleFile]];
  216. else {
  217. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[mediaFile duration]]];
  218. if (mediaFile.videoTrack) {
  219. NSString *width = [[mediaFile videoTrack] valueForKey:@"width"];
  220. NSString *height = [[mediaFile videoTrack] valueForKey:@"height"];
  221. if (width.intValue > 0 && height.intValue > 0)
  222. self.subtitleLabel.text = [self.subtitleLabel.text stringByAppendingFormat:@" — %@x%@", width, height];
  223. }
  224. }
  225. [self _showPositionOfItem:mediaFile];
  226. }
  227. - (void)_showPositionOfItem:(MLFile *)mediaItem
  228. {
  229. CGFloat position = mediaItem.lastPosition.floatValue;
  230. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  231. CGFloat duration = mediaItem.duration.floatValue;
  232. if (position > .05f && position < .95f && (duration * position - duration) < -60000) {
  233. [(UITextView*)self.mediaIsUnreadView setText:[NSString stringWithFormat:NSLocalizedString(@"LIBRARY_MINUTES_LEFT", @""), [[VLCTime timeWithInt:(duration * position - duration)] minuteStringValue]]];
  234. self.mediaIsUnreadView.hidden = NO;
  235. } else if (mediaItem.unread.intValue) {
  236. [(UILabel *)self.mediaIsUnreadView setText:[NSLocalizedString(@"NEW", @"") capitalizedStringWithLocale:[NSLocale currentLocale]]];
  237. self.mediaIsUnreadView.hidden = NO;
  238. } else
  239. self.mediaIsUnreadView.hidden = YES;
  240. } else {
  241. self.progressIndicator.progress = position;
  242. self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
  243. [self.progressIndicator setNeedsDisplay];
  244. self.mediaIsUnreadView.hidden = !mediaItem.unread.intValue;
  245. }
  246. }
  247. @end