VLCPlaylistTableViewCell.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "VLCThumbnailsCache.h"
  13. #import <MediaLibraryKit/MLAlbum.h>
  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)awakeFromNib
  28. {
  29. if (SYSTEM_RUNS_IN_THE_FUTURE) {
  30. [(UILabel *)self.mediaIsUnreadView setText:[NSLocalizedString(@"NEW", @"") capitalizedStringWithLocale:[NSLocale currentLocale]]];
  31. }
  32. }
  33. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  34. {
  35. [self _updatedDisplayedInformationForKeyPath:keyPath];
  36. }
  37. - (void)setMediaObject:(MLFile *)mediaObject
  38. {
  39. if (_mediaObject != mediaObject) {
  40. [_mediaObject removeObserver:self forKeyPath:@"computedThumbnail"];
  41. [_mediaObject removeObserver:self forKeyPath:@"lastPosition"];
  42. [_mediaObject removeObserver:self forKeyPath:@"duration"];
  43. [_mediaObject removeObserver:self forKeyPath:@"fileSizeInBytes"];
  44. [_mediaObject removeObserver:self forKeyPath:@"title"];
  45. [_mediaObject removeObserver:self forKeyPath:@"thumbnailTimeouted"];
  46. [_mediaObject removeObserver:self forKeyPath:@"unread"];
  47. [_mediaObject removeObserver:self forKeyPath:@"albumTrackNumber"];
  48. [_mediaObject removeObserver:self forKeyPath:@"album"];
  49. [_mediaObject removeObserver:self forKeyPath:@"artist"];
  50. [_mediaObject removeObserver:self forKeyPath:@"genre"];
  51. if ([_mediaObject respondsToSelector:@selector(didHide)])
  52. [(MLFile*)_mediaObject didHide];
  53. _mediaObject = mediaObject;
  54. [_mediaObject addObserver:self forKeyPath:@"computedThumbnail" options:0 context:nil];
  55. [_mediaObject addObserver:self forKeyPath:@"lastPosition" options:0 context:nil];
  56. [_mediaObject addObserver:self forKeyPath:@"duration" options:0 context:nil];
  57. [_mediaObject addObserver:self forKeyPath:@"fileSizeInBytes" options:0 context:nil];
  58. [_mediaObject addObserver:self forKeyPath:@"title" options:0 context:nil];
  59. [_mediaObject addObserver:self forKeyPath:@"thumbnailTimeouted" options:0 context:nil];
  60. [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
  61. [_mediaObject addObserver:self forKeyPath:@"albumTrackNumber" options:0 context:nil];
  62. [_mediaObject addObserver:self forKeyPath:@"album" options:0 context:nil];
  63. [_mediaObject addObserver:self forKeyPath:@"artist" options:0 context:nil];
  64. [_mediaObject addObserver:self forKeyPath:@"genre" options:0 context:nil];
  65. if ([_mediaObject respondsToSelector:@selector(willDisplay)])
  66. [(MLFile*)_mediaObject willDisplay];
  67. }
  68. [self _updatedDisplayedInformationForKeyPath:nil];
  69. }
  70. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  71. {
  72. [super setEditing:editing animated:animated];
  73. [self _updatedDisplayedInformationForKeyPath:@"editing"];
  74. }
  75. - (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
  76. {
  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. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:mediaObject];
  83. }
  84. } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
  85. MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;
  86. [self configureForAlbum:mediaObject];
  87. } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
  88. MLAlbumTrack *mediaObject = (MLAlbumTrack *)self.mediaObject;
  89. [self configureForAlbumTrack:mediaObject];
  90. } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
  91. MLShow *mediaObject = (MLShow *)self.mediaObject;
  92. [self configureForShow:mediaObject];
  93. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
  94. MLFile *anyFileFromAnyEpisode = [mediaObject.episodes.anyObject files].anyObject;
  95. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyEpisode];
  96. }
  97. } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
  98. MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
  99. [self configureForShowEpisode:mediaObject];
  100. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
  101. MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
  102. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
  103. }
  104. }
  105. [self setNeedsDisplay];
  106. }
  107. + (CGFloat)heightOfCell
  108. {
  109. if (SYSTEM_RUNS_IN_THE_FUTURE)
  110. return 90.;
  111. return 80.;
  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_IN_THE_FUTURE) {
  119. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@", show.releaseYear, [NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", @"") : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", @""), count, show.unreadEpisodes.count]];
  120. } else {
  121. self.artistNameLabel.text = @"";
  122. self.albumNameLabel.text = show.releaseYear;
  123. self.subtitleLabel.text = [NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", @"") : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", @""), count, show.unreadEpisodes.count];
  124. }
  125. self.mediaIsUnreadView.hidden = YES;
  126. self.progressIndicator.hidden = YES;
  127. }
  128. - (void)configureForAlbumTrack:(MLAlbumTrack *)albumTrack
  129. {
  130. MLFile *anyFileFromTrack = albumTrack.files.anyObject;
  131. if (SYSTEM_RUNS_IN_THE_FUTURE)
  132. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@ — %@", albumTrack.artist, [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue], [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
  133. else {
  134. self.artistNameLabel.text = albumTrack.artist;
  135. self.albumNameLabel.text = [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue];
  136. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
  137. }
  138. self.titleLabel.text = albumTrack.title;
  139. self.thumbnailView.image = nil;
  140. CGFloat position = anyFileFromTrack.lastPosition.floatValue;
  141. self.progressIndicator.progress = position;
  142. self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
  143. [self.progressIndicator setNeedsDisplay];
  144. self.mediaIsUnreadView.hidden = !anyFileFromTrack.unread.intValue;
  145. }
  146. - (void)configureForShowEpisode:(MLShowEpisode *)showEpisode
  147. {
  148. self.titleLabel.text = showEpisode.name;
  149. MLFile *anyFileFromEpisode = showEpisode.files.anyObject;
  150. if (self.titleLabel.text.length < 1) {
  151. self.titleLabel.text = [NSString stringWithFormat:@"S%02dE%02d", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue];
  152. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  153. } else
  154. self.subtitleLabel.text = [NSString stringWithFormat:@"S%02dE%02d — %@", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue, [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  155. CGFloat position = anyFileFromEpisode.lastPosition.floatValue;
  156. self.progressIndicator.progress = position;
  157. self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
  158. [self.progressIndicator setNeedsDisplay];
  159. self.mediaIsUnreadView.hidden = !showEpisode.unread.intValue;
  160. }
  161. - (void)configureForAlbum:(MLAlbum *)album
  162. {
  163. self.titleLabel.text = album.name;
  164. MLAlbumTrack *anyTrack = [album.tracks anyObject];
  165. NSUInteger count = album.tracks.count;
  166. if (SYSTEM_RUNS_IN_THE_FUTURE) {
  167. NSMutableString *string = [[NSMutableString alloc] init];
  168. if (anyTrack) {
  169. [string appendString:anyTrack.artist];
  170. [string appendString:@" — "];
  171. }
  172. [string appendFormat:@"%@ — %@", [NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count], album.releaseYear];
  173. self.subtitleLabel.text = string;
  174. } else {
  175. self.artistNameLabel.text = anyTrack? anyTrack.artist: @"";
  176. self.albumNameLabel.text = album.releaseYear;
  177. self.subtitleLabel.text = [NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count];
  178. }
  179. self.thumbnailView.image = nil;
  180. self.mediaIsUnreadView.hidden = YES;
  181. self.progressIndicator.hidden = YES;
  182. }
  183. - (void)configureForMLFile:(MLFile *)mediaFile
  184. {
  185. if (mediaFile.isAlbumTrack) {
  186. if (SYSTEM_RUNS_IN_THE_FUTURE) {
  187. self.titleLabel.text = [NSString stringWithFormat:@"%@ — %@ — %@", mediaFile.albumTrack.artist, mediaFile.albumTrack.album.name, (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. if (self.isEditing)
  197. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %i MB", [VLCTime timeWithNumber:[mediaFile duration]], (int)([mediaFile fileSizeInBytes] / 1e6)];
  198. else {
  199. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[mediaFile duration]]];
  200. if (mediaFile.videoTrack) {
  201. NSString *width = [[mediaFile videoTrack] valueForKey:@"width"];
  202. NSString *height = [[mediaFile videoTrack] valueForKey:@"height"];
  203. if (width.intValue > 0 && height.intValue > 0)
  204. self.subtitleLabel.text = [self.subtitleLabel.text stringByAppendingFormat:@" — %@x%@", width, height];
  205. }
  206. }
  207. CGFloat position = mediaFile.lastPosition.floatValue;
  208. self.progressIndicator.progress = position;
  209. self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
  210. [self.progressIndicator setNeedsDisplay];
  211. self.mediaIsUnreadView.hidden = !mediaFile.unread.intValue;
  212. }
  213. @end