VLCPlaylistTableViewCell.m 16 KB

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