VLCPlaylistTableViewCell.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. @interface VLCPlaylistTableViewCell ()
  19. {
  20. UILongPressGestureRecognizer *_longPress;
  21. }
  22. @end
  23. @implementation VLCPlaylistTableViewCell
  24. + (VLCPlaylistTableViewCell *)cellWithReuseIdentifier:(NSString *)ident
  25. {
  26. NSArray *nibContentArray;
  27. if (SYSTEM_RUNS_IOS7_OR_LATER)
  28. nibContentArray = [[NSBundle mainBundle] loadNibNamed:@"VLCFuturePlaylistTableViewCell" owner:nil options:nil];
  29. else
  30. nibContentArray = [[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistTableViewCell" owner:nil options:nil];
  31. NSAssert([nibContentArray count] == 1, @"meh");
  32. NSAssert([[nibContentArray lastObject] isKindOfClass:[VLCPlaylistTableViewCell class]], @"meh meh");
  33. VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[nibContentArray lastObject];
  34. cell.multipleSelectionBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
  35. return cell;
  36. }
  37. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  38. {
  39. [self _updatedDisplayedInformationForKeyPath:keyPath];
  40. }
  41. - (void)setMediaObject:(MLFile *)mediaObject
  42. {
  43. if (_mediaObject != mediaObject) {
  44. if ([_mediaObject isKindOfClass:[MLLabel class]]) {
  45. [_mediaObject removeObserver:self forKeyPath:@"files"];
  46. [_mediaObject removeObserver:self forKeyPath:@"name"];
  47. } else if ([_mediaObject isKindOfClass:[MLShow class]])
  48. [_mediaObject removeObserver:self forKeyPath:@"episodes"];
  49. else if ([_mediaObject isKindOfClass:[MLShowEpisode class]]) {
  50. [_mediaObject removeObserver:self forKeyPath:@"name"];
  51. [_mediaObject removeObserver:self forKeyPath:@"files"];
  52. [_mediaObject removeObserver:self forKeyPath:@"artworkURL"];
  53. [_mediaObject removeObserver:self forKeyPath:@"unread"];
  54. } else if ([_mediaObject isKindOfClass:[MLAlbum class]]) {
  55. [_mediaObject removeObserver:self forKeyPath:@"name"];
  56. [_mediaObject removeObserver:self forKeyPath:@"tracks"];
  57. } else if ([_mediaObject isKindOfClass:[MLFile class]]) {
  58. [_mediaObject removeObserver:self forKeyPath:@"computedThumbnail"];
  59. [_mediaObject removeObserver:self forKeyPath:@"lastPosition"];
  60. [_mediaObject removeObserver:self forKeyPath:@"duration"];
  61. [_mediaObject removeObserver:self forKeyPath:@"fileSizeInBytes"];
  62. [_mediaObject removeObserver:self forKeyPath:@"title"];
  63. [_mediaObject removeObserver:self forKeyPath:@"thumbnailTimeouted"];
  64. [_mediaObject removeObserver:self forKeyPath:@"unread"];
  65. [_mediaObject removeObserver:self forKeyPath:@"albumTrackNumber"];
  66. [_mediaObject removeObserver:self forKeyPath:@"album"];
  67. [_mediaObject removeObserver:self forKeyPath:@"artist"];
  68. [_mediaObject removeObserver:self forKeyPath:@"genre"];
  69. [_mediaObject removeObserver:self forKeyPath:@"labels"];
  70. [(MLFile*)_mediaObject didHide];
  71. }
  72. _mediaObject = mediaObject;
  73. // prevent the cell from recycling the current snap for random contents
  74. self.thumbnailView.image = nil;
  75. if ([_mediaObject isKindOfClass:[MLLabel class]]) {
  76. [_mediaObject addObserver:self forKeyPath:@"files" options:0 context:nil];
  77. [_mediaObject addObserver:self forKeyPath:@"name" options:0 context:nil];
  78. } else if ([_mediaObject isKindOfClass:[MLShow class]])
  79. [_mediaObject addObserver:self forKeyPath:@"episodes" options:0 context:nil];
  80. else if ([_mediaObject isKindOfClass:[MLShowEpisode class]]) {
  81. [_mediaObject addObserver:self forKeyPath:@"name" options:0 context:nil];
  82. [_mediaObject addObserver:self forKeyPath:@"files" options:0 context:nil];
  83. [_mediaObject addObserver:self forKeyPath:@"artworkURL" options:0 context:nil];
  84. [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
  85. } else if ([_mediaObject isKindOfClass:[MLAlbum class]]) {
  86. [_mediaObject addObserver:self forKeyPath:@"name" options:0 context:nil];
  87. [_mediaObject addObserver:self forKeyPath:@"tracks" options:0 context:nil];
  88. } else if ([_mediaObject isKindOfClass:[MLFile class]]) {
  89. [_mediaObject addObserver:self forKeyPath:@"computedThumbnail" options:0 context:nil];
  90. [_mediaObject addObserver:self forKeyPath:@"lastPosition" options:0 context:nil];
  91. [_mediaObject addObserver:self forKeyPath:@"duration" options:0 context:nil];
  92. [_mediaObject addObserver:self forKeyPath:@"fileSizeInBytes" options:0 context:nil];
  93. [_mediaObject addObserver:self forKeyPath:@"title" options:0 context:nil];
  94. [_mediaObject addObserver:self forKeyPath:@"thumbnailTimeouted" options:0 context:nil];
  95. [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
  96. [_mediaObject addObserver:self forKeyPath:@"albumTrackNumber" options:0 context:nil];
  97. [_mediaObject addObserver:self forKeyPath:@"album" options:0 context:nil];
  98. [_mediaObject addObserver:self forKeyPath:@"artist" options:0 context:nil];
  99. [_mediaObject addObserver:self forKeyPath:@"genre" options:0 context:nil];
  100. [_mediaObject addObserver:self forKeyPath:@"labels" options:0 context:nil];
  101. [(MLFile*)_mediaObject willDisplay];
  102. }
  103. }
  104. [self _updatedDisplayedInformationForKeyPath:nil];
  105. }
  106. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  107. {
  108. [super setEditing:editing animated:animated];
  109. [self _updatedDisplayedInformationForKeyPath:@"editing"];
  110. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  111. if (editing) {
  112. if (_longPress)
  113. [self removeGestureRecognizer:_longPress];
  114. } else {
  115. if (!_longPress)
  116. _longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTouchGestureAction:)];
  117. [self addGestureRecognizer:_longPress];
  118. }
  119. }
  120. }
  121. - (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
  122. {
  123. self.thumbnailView.contentMode = UIViewContentModeScaleAspectFill;
  124. if ([self.mediaObject isKindOfClass:[MLFile class]]) {
  125. MLFile *mediaObject = (MLFile*)self.mediaObject;
  126. [self _configureForMLFile:mediaObject];
  127. if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])))
  128. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:mediaObject];
  129. } else if ([self.mediaObject isKindOfClass:[MLLabel class]]) {
  130. MLLabel *mediaObject = (MLLabel *)self.mediaObject;
  131. [self _configureForFolder:mediaObject];
  132. if ([keyPath isEqualToString:@"files"] || [keyPath isEqualToString:@"labels"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  133. if (mediaObject.files.count == 0) {
  134. self.thumbnailView.contentMode = UIViewContentModeScaleAspectFit;
  135. self.thumbnailView.image = [UIImage imageNamed:@"folderIcon"];
  136. } else
  137. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForLabel:mediaObject];
  138. }
  139. } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
  140. MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;
  141. [self _configureForAlbum:mediaObject];
  142. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  143. MLFile *anyFileFromAnyTrack = [mediaObject.tracks.anyObject files].anyObject;
  144. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyTrack];
  145. }
  146. } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
  147. MLAlbumTrack *mediaObject = (MLAlbumTrack *)self.mediaObject;
  148. [self _configureForAlbumTrack:mediaObject];
  149. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || !self.thumbnailView.image) {
  150. MLFile *anyFileFromTrack = mediaObject.files.anyObject;
  151. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromTrack];
  152. }
  153. } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
  154. MLShow *mediaObject = (MLShow *)self.mediaObject;
  155. [self _configureForShow:mediaObject];
  156. if ([keyPath isEqualToString:@"computedThumbnail"] || [keyPath isEqualToString:@"episodes"] || !keyPath || !self.thumbnailView.image) {
  157. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForShow:mediaObject];
  158. }
  159. } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
  160. MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
  161. [self _configureForShowEpisode:mediaObject];
  162. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || !self.thumbnailView.image) {
  163. MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
  164. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
  165. }
  166. }
  167. [self setNeedsDisplay];
  168. }
  169. + (CGFloat)heightOfCell
  170. {
  171. if (SYSTEM_RUNS_IOS7_OR_LATER)
  172. return 90.;
  173. return 80.;
  174. }
  175. #pragma mark - presentation
  176. - (void)_configureForShow:(MLShow *)show
  177. {
  178. self.titleLabel.text = show.name;
  179. NSUInteger count = show.episodes.count;
  180. NSString *string = @"";
  181. if (show.releaseYear)
  182. string = [NSString stringWithFormat:@"%@ — ", show.releaseYear];
  183. self.subtitleLabel.text = [string stringByAppendingString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", @"") : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", @""), count, show.unreadEpisodes.count]];
  184. self.mediaIsUnreadView.hidden = YES;
  185. self.progressIndicator.hidden = YES;
  186. self.folderIconView.image = [UIImage imageNamed:@"PlayingExternally"];
  187. self.folderIconView.hidden = NO;
  188. }
  189. - (void)_configureForAlbumTrack:(MLAlbumTrack *)albumTrack
  190. {
  191. MLFile *anyFileFromTrack = albumTrack.files.anyObject;
  192. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@ — %@", albumTrack.artist, [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue], [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
  193. self.titleLabel.text = albumTrack.title;
  194. [self _showPositionOfItem:anyFileFromTrack];
  195. self.folderIconView.hidden = YES;
  196. }
  197. - (void)_configureForShowEpisode:(MLShowEpisode *)showEpisode
  198. {
  199. self.titleLabel.text = showEpisode.name;
  200. MLFile *anyFileFromEpisode = showEpisode.files.anyObject;
  201. if (self.titleLabel.text.length < 1) {
  202. self.titleLabel.text = [NSString stringWithFormat:@"S%02dE%02d", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue];
  203. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  204. } else
  205. self.subtitleLabel.text = [NSString stringWithFormat:@"S%02dE%02d — %@", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue, [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  206. [self _showPositionOfItem:anyFileFromEpisode];
  207. self.folderIconView.hidden = YES;
  208. }
  209. - (void)_configureForAlbum:(MLAlbum *)album
  210. {
  211. self.titleLabel.text = album.name;
  212. MLAlbumTrack *anyTrack = [album.tracks anyObject];
  213. NSUInteger count = album.tracks.count;
  214. NSMutableString *string = [[NSMutableString alloc] init];
  215. if (anyTrack) {
  216. if (anyTrack.artist.length > 0) {
  217. [string appendString:anyTrack.artist];
  218. [string appendString:@" — "];
  219. }
  220. }
  221. [string appendString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count]];
  222. if (album.releaseYear.length > 0)
  223. [string appendFormat:@" — %@", album.releaseYear];
  224. self.subtitleLabel.text = string;
  225. self.mediaIsUnreadView.hidden = YES;
  226. self.progressIndicator.hidden = YES;
  227. self.folderIconView.hidden = YES;
  228. }
  229. - (void)_configureForFolder:(MLLabel *)label
  230. {
  231. self.titleLabel.text = label.name;
  232. NSUInteger count = label.files.count;
  233. self.subtitleLabel.text = [NSString stringWithFormat:(count == 1) ? NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @"") : NSLocalizedString(@"LIBRARY_TRACKS", @""), count];
  234. self.mediaIsUnreadView.hidden = YES;
  235. self.progressIndicator.hidden = YES;
  236. self.folderIconView.image = [UIImage imageNamed:@"folderIcon"];
  237. self.folderIconView.hidden = NO;
  238. }
  239. - (void)_configureForMLFile:(MLFile *)mediaFile
  240. {
  241. if (mediaFile.isAlbumTrack) {
  242. NSString *string = @"";
  243. if (mediaFile.albumTrack.artist)
  244. string = [NSString stringWithFormat:@"%@ — ", mediaFile.albumTrack.artist];
  245. else if (mediaFile.albumTrack.album.name)
  246. string = [NSString stringWithFormat:@"%@ — ", mediaFile.albumTrack.artist];
  247. self.titleLabel.text = [string stringByAppendingString:(mediaFile.albumTrack.title.length > 1) ? mediaFile.albumTrack.title : mediaFile.title];
  248. } else
  249. self.titleLabel.text = mediaFile.title;
  250. if (self.isEditing)
  251. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@", [VLCTime timeWithNumber:[mediaFile duration]], [NSByteCountFormatter stringFromByteCount:[mediaFile fileSizeInBytes] countStyle:NSByteCountFormatterCountStyleFile]];
  252. else {
  253. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[mediaFile duration]]];
  254. if (mediaFile.videoTrack) {
  255. NSString *width = [[mediaFile videoTrack] valueForKey:@"width"];
  256. NSString *height = [[mediaFile videoTrack] valueForKey:@"height"];
  257. if (width.intValue > 0 && height.intValue > 0)
  258. self.subtitleLabel.text = [self.subtitleLabel.text stringByAppendingFormat:@" — %@x%@", width, height];
  259. }
  260. }
  261. [self _showPositionOfItem:mediaFile];
  262. self.folderIconView.hidden = YES;
  263. }
  264. - (void)_showPositionOfItem:(MLFile *)mediaItem
  265. {
  266. CGFloat position = mediaItem.lastPosition.floatValue;
  267. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  268. CGFloat duration = mediaItem.duration.floatValue;
  269. if (position > .05f && position < .95f && (duration * position - duration) < -60000) {
  270. [(UITextView*)self.mediaIsUnreadView setText:[NSString stringWithFormat:NSLocalizedString(@"LIBRARY_MINUTES_LEFT", @""), [[VLCTime timeWithInt:(duration * position - duration)] minuteStringValue]]];
  271. self.mediaIsUnreadView.hidden = NO;
  272. } else if (mediaItem.unread.intValue) {
  273. [(UILabel *)self.mediaIsUnreadView setText:[NSLocalizedString(@"NEW", @"") capitalizedStringWithLocale:[NSLocale currentLocale]]];
  274. self.mediaIsUnreadView.hidden = NO;
  275. } else
  276. self.mediaIsUnreadView.hidden = YES;
  277. } else {
  278. self.progressIndicator.progress = position;
  279. self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
  280. [self.progressIndicator setNeedsDisplay];
  281. self.mediaIsUnreadView.hidden = !mediaItem.unread.intValue;
  282. }
  283. }
  284. - (void)longTouchGestureAction:(UIGestureRecognizer *)recognizer
  285. {
  286. CGRect frame = self.frame;
  287. if (frame.size.height > 90.)
  288. frame.size.height = 90.;
  289. else if (recognizer.state == UIGestureRecognizerStateBegan)
  290. frame.size.height = 180;
  291. void (^animationBlock)() = ^() {
  292. self.frame = frame;
  293. };
  294. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  295. self.frame = frame;
  296. };
  297. NSTimeInterval animationDuration = .2;
  298. [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
  299. }
  300. @end