VLCPlaylistTableViewCell.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // VLCPlaylistTableViewCell.m
  3. // AspenProject
  4. //
  5. // Created by Felix Paul Kühne on 01.04.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 "VLCPlaylistTableViewCell.h"
  11. #import "VLCLinearProgressIndicator.h"
  12. #import <MediaLibraryKit/MLAlbum.h>
  13. #define MAX_CACHE_SIZE 21 // three times the number of items shown on iPhone 5
  14. @implementation VLCPlaylistTableViewCell
  15. + (VLCPlaylistTableViewCell *)cellWithReuseIdentifier:(NSString *)ident
  16. {
  17. NSArray *nibContentArray;
  18. if (SYSTEM_RUNS_IN_THE_FUTURE)
  19. nibContentArray = [[NSBundle mainBundle] loadNibNamed:@"VLCFuturePlaylistTableViewCell" owner:nil options:nil];
  20. else
  21. nibContentArray = [[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistTableViewCell" owner:nil options:nil];
  22. NSAssert([nibContentArray count] == 1, @"meh");
  23. NSAssert([[nibContentArray lastObject] isKindOfClass:[VLCPlaylistTableViewCell class]], @"meh meh");
  24. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[nibContentArray lastObject];
  25. return cell;
  26. }
  27. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  28. {
  29. [self _updatedDisplayedInformationForKeyPath:keyPath];
  30. }
  31. - (void)setMediaObject:(MLFile *)mediaObject
  32. {
  33. if (_mediaObject != mediaObject) {
  34. [_mediaObject removeObserver:self forKeyPath:@"computedThumbnail"];
  35. [_mediaObject removeObserver:self forKeyPath:@"lastPosition"];
  36. [_mediaObject removeObserver:self forKeyPath:@"duration"];
  37. [_mediaObject removeObserver:self forKeyPath:@"fileSizeInBytes"];
  38. [_mediaObject removeObserver:self forKeyPath:@"title"];
  39. [_mediaObject removeObserver:self forKeyPath:@"thumbnailTimeouted"];
  40. [_mediaObject removeObserver:self forKeyPath:@"unread"];
  41. [_mediaObject removeObserver:self forKeyPath:@"albumTrackNumber"];
  42. [_mediaObject removeObserver:self forKeyPath:@"album"];
  43. [_mediaObject removeObserver:self forKeyPath:@"artist"];
  44. [_mediaObject removeObserver:self forKeyPath:@"genre"];
  45. if ([_mediaObject respondsToSelector:@selector(didHide)])
  46. [(MLFile*)_mediaObject didHide];
  47. _mediaObject = mediaObject;
  48. [_mediaObject addObserver:self forKeyPath:@"computedThumbnail" options:0 context:nil];
  49. [_mediaObject addObserver:self forKeyPath:@"lastPosition" options:0 context:nil];
  50. [_mediaObject addObserver:self forKeyPath:@"duration" options:0 context:nil];
  51. [_mediaObject addObserver:self forKeyPath:@"fileSizeInBytes" options:0 context:nil];
  52. [_mediaObject addObserver:self forKeyPath:@"title" options:0 context:nil];
  53. [_mediaObject addObserver:self forKeyPath:@"thumbnailTimeouted" options:0 context:nil];
  54. [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
  55. [_mediaObject addObserver:self forKeyPath:@"albumTrackNumber" options:0 context:nil];
  56. [_mediaObject addObserver:self forKeyPath:@"album" options:0 context:nil];
  57. [_mediaObject addObserver:self forKeyPath:@"artist" options:0 context:nil];
  58. [_mediaObject addObserver:self forKeyPath:@"genre" options:0 context:nil];
  59. if ([_mediaObject respondsToSelector:@selector(willDisplay)])
  60. [(MLFile*)_mediaObject willDisplay];
  61. }
  62. [self _updatedDisplayedInformationForKeyPath:NULL];
  63. }
  64. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  65. {
  66. [super setEditing:editing animated:animated];
  67. [self _updatedDisplayedInformationForKeyPath:@"editing"];
  68. }
  69. - (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
  70. {
  71. static NSMutableArray *_thumbnailCacheIndex;
  72. static NSMutableDictionary *_thumbnailCache;
  73. if (!_thumbnailCache)
  74. _thumbnailCache = [[NSMutableDictionary alloc] initWithCapacity:MAX_CACHE_SIZE];
  75. if (!_thumbnailCacheIndex)
  76. _thumbnailCacheIndex = [[NSMutableArray alloc] initWithCapacity:MAX_CACHE_SIZE];
  77. self.albumNameLabel.text = self.artistNameLabel.text = @"";
  78. if ([self.mediaObject isKindOfClass:[MLFile class]]) {
  79. MLFile *mediaObject = (MLFile*)self.mediaObject;
  80. [self configureForMLFile:mediaObject];
  81. if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) && !mediaObject.isAlbumTrack) {
  82. NSManagedObjectID *objID = mediaObject.objectID;
  83. UIImage *displayedImage;
  84. if ([_thumbnailCacheIndex containsObject:objID]) {
  85. [_thumbnailCacheIndex removeObject:objID];
  86. [_thumbnailCacheIndex insertObject:objID atIndex:0];
  87. displayedImage = [_thumbnailCache objectForKey:objID];
  88. if (!displayedImage) {
  89. displayedImage = mediaObject.computedThumbnail;
  90. if (displayedImage)
  91. [_thumbnailCache setObject:displayedImage forKey:objID];
  92. }
  93. } else {
  94. if (_thumbnailCacheIndex.count >= MAX_CACHE_SIZE) {
  95. [_thumbnailCache removeObjectForKey:[_thumbnailCacheIndex lastObject]];
  96. [_thumbnailCacheIndex removeLastObject];
  97. }
  98. displayedImage = mediaObject.computedThumbnail;
  99. if (displayedImage)
  100. [_thumbnailCache setObject:displayedImage forKey:objID];
  101. if (objID)
  102. [_thumbnailCacheIndex insertObject:objID atIndex:0];
  103. }
  104. self.thumbnailView.image = displayedImage;
  105. }
  106. } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
  107. MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;
  108. [self configureForAlbum:mediaObject];
  109. } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
  110. MLAlbumTrack *mediaObject = (MLAlbumTrack *)self.mediaObject;
  111. [self configureForAlbumTrack:mediaObject];
  112. } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
  113. MLShow *mediaObject = (MLShow *)self.mediaObject;
  114. [self configureForShow:mediaObject];
  115. MLFile *anyFileFromAnyEpisode = [mediaObject.episodes.anyObject files].anyObject;
  116. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
  117. NSManagedObjectID *objID = anyFileFromAnyEpisode.objectID;
  118. UIImage *displayedImage;
  119. if ([_thumbnailCacheIndex containsObject:objID]) {
  120. [_thumbnailCacheIndex removeObject:objID];
  121. [_thumbnailCacheIndex insertObject:objID atIndex:0];
  122. displayedImage = [_thumbnailCache objectForKey:objID];
  123. if (!displayedImage) {
  124. displayedImage = anyFileFromAnyEpisode.computedThumbnail;
  125. if (displayedImage)
  126. [_thumbnailCache setObject:displayedImage forKey:objID];
  127. }
  128. } else {
  129. if (_thumbnailCacheIndex.count >= MAX_CACHE_SIZE) {
  130. [_thumbnailCache removeObjectForKey:[_thumbnailCacheIndex lastObject]];
  131. [_thumbnailCacheIndex removeLastObject];
  132. }
  133. displayedImage = anyFileFromAnyEpisode.computedThumbnail;
  134. if (displayedImage)
  135. [_thumbnailCache setObject:displayedImage forKey:objID];
  136. if (objID)
  137. [_thumbnailCacheIndex insertObject:objID atIndex:0];
  138. }
  139. self.thumbnailView.image = displayedImage;
  140. }
  141. } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
  142. MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
  143. [self configureForShowEpisode:mediaObject];
  144. MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
  145. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
  146. NSManagedObjectID *objID = anyFileFromEpisode.objectID;
  147. UIImage *displayedImage;
  148. if ([_thumbnailCacheIndex containsObject:objID]) {
  149. [_thumbnailCacheIndex removeObject:objID];
  150. [_thumbnailCacheIndex insertObject:objID atIndex:0];
  151. displayedImage = [_thumbnailCache objectForKey:objID];
  152. } else {
  153. if (_thumbnailCacheIndex.count >= MAX_CACHE_SIZE) {
  154. [_thumbnailCache removeObjectForKey:[_thumbnailCacheIndex lastObject]];
  155. [_thumbnailCacheIndex removeLastObject];
  156. }
  157. displayedImage = anyFileFromEpisode.computedThumbnail;
  158. if (displayedImage)
  159. [_thumbnailCache setObject:displayedImage forKey:objID];
  160. if (objID)
  161. [_thumbnailCacheIndex insertObject:objID atIndex:0];
  162. }
  163. self.thumbnailView.image = displayedImage;
  164. }
  165. }
  166. [self setNeedsDisplay];
  167. }
  168. + (CGFloat)heightOfCell
  169. {
  170. if (SYSTEM_RUNS_IN_THE_FUTURE)
  171. return 185.;
  172. return 80.;
  173. }
  174. #pragma mark - presentation
  175. - (void)configureForShow:(MLShow *)show
  176. {
  177. self.titleLabel.text = show.name;
  178. self.artistNameLabel.text = @"";
  179. self.albumNameLabel.text = show.releaseYear;
  180. NSUInteger count = show.episodes.count;
  181. self.subtitleLabel.text = [NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", @"") : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", @""), count, show.unreadEpisodes.count];
  182. self.mediaIsUnreadView.hidden = YES;
  183. self.progressIndicator.hidden = YES;
  184. }
  185. - (void)configureForAlbumTrack:(MLAlbumTrack *)albumTrack
  186. {
  187. self.artistNameLabel.text = albumTrack.artist;
  188. self.albumNameLabel.text = [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue];
  189. self.titleLabel.text = albumTrack.title;
  190. self.thumbnailView.image = nil;
  191. MLFile *anyFileFromTrack = albumTrack.files.anyObject;
  192. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
  193. CGFloat position = anyFileFromTrack.lastPosition.floatValue;
  194. self.progressIndicator.progress = position;
  195. self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
  196. [self.progressIndicator setNeedsDisplay];
  197. self.mediaIsUnreadView.hidden = !anyFileFromTrack.unread.intValue;
  198. }
  199. - (void)configureForShowEpisode:(MLShowEpisode *)showEpisode
  200. {
  201. self.titleLabel.text = showEpisode.name;
  202. MLFile *anyFileFromEpisode = showEpisode.files.anyObject;
  203. if (self.titleLabel.text.length < 1) {
  204. self.titleLabel.text = [NSString stringWithFormat:@"S%02dE%02d", showEpisode.episodeNumber.intValue, showEpisode.seasonNumber.intValue];
  205. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  206. } else
  207. self.subtitleLabel.text = [NSString stringWithFormat:@"S%02dE%02d — %@", showEpisode.episodeNumber.intValue, showEpisode.seasonNumber.intValue, [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  208. CGFloat position = anyFileFromEpisode.lastPosition.floatValue;
  209. self.progressIndicator.progress = position;
  210. self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
  211. [self.progressIndicator setNeedsDisplay];
  212. self.mediaIsUnreadView.hidden = !showEpisode.unread.intValue;
  213. }
  214. - (void)configureForAlbum:(MLAlbum *)album
  215. {
  216. self.titleLabel.text = album.name;
  217. MLAlbumTrack *anyTrack = [album.tracks anyObject];
  218. self.artistNameLabel.text = anyTrack? anyTrack.artist: @"";
  219. self.albumNameLabel.text = album.releaseYear;
  220. self.thumbnailView.image = nil;
  221. NSUInteger count = album.tracks.count;
  222. self.subtitleLabel.text = [NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count];
  223. self.mediaIsUnreadView.hidden = YES;
  224. self.progressIndicator.hidden = YES;
  225. }
  226. - (void)configureForMLFile:(MLFile *)mediaFile {
  227. if (mediaFile.isAlbumTrack) {
  228. self.artistNameLabel.text = mediaFile.albumTrack.artist;
  229. self.albumNameLabel.text = mediaFile.albumTrack.album.name;
  230. self.titleLabel.text = (mediaFile.albumTrack.title.length > 1) ? mediaFile.albumTrack.title : mediaFile.title;
  231. self.thumbnailView.image = nil;
  232. } else
  233. self.titleLabel.text = mediaFile.title;
  234. if (self.isEditing)
  235. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %i MB", [VLCTime timeWithNumber:[mediaFile duration]], (int)([mediaFile fileSizeInBytes] / 1e6)];
  236. else {
  237. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[mediaFile duration]]];
  238. if (mediaFile.videoTrack) {
  239. NSString *width = [[mediaFile videoTrack] valueForKey:@"width"];
  240. NSString *height = [[mediaFile videoTrack] valueForKey:@"height"];
  241. if (width.intValue > 0 && height.intValue > 0)
  242. self.subtitleLabel.text = [self.subtitleLabel.text stringByAppendingFormat:@" — %@x%@", width, height];
  243. }
  244. }
  245. CGFloat position = mediaFile.lastPosition.floatValue;
  246. self.progressIndicator.progress = position;
  247. self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
  248. [self.progressIndicator setNeedsDisplay];
  249. self.mediaIsUnreadView.hidden = !mediaFile.unread.intValue;
  250. }
  251. @end