VLCPlaylistTableViewCell.m 16 KB

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