VLCPlaylistTableViewCell.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.image = [VLCThumbnailsCache thumbnailForLabel:mediaObject];
  145. }
  146. } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
  147. MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;
  148. [self _configureForAlbum:mediaObject];
  149. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  150. MLFile *anyFileFromAnyTrack = [mediaObject.tracks.anyObject files].anyObject;
  151. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyTrack];
  152. }
  153. } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
  154. MLAlbumTrack *mediaObject = (MLAlbumTrack *)self.mediaObject;
  155. [self _configureForAlbumTrack:mediaObject];
  156. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || !self.thumbnailView.image) {
  157. MLFile *anyFileFromTrack = mediaObject.files.anyObject;
  158. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromTrack];
  159. }
  160. } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
  161. MLShow *mediaObject = (MLShow *)self.mediaObject;
  162. [self _configureForShow:mediaObject];
  163. if ([keyPath isEqualToString:@"computedThumbnail"] || [keyPath isEqualToString:@"episodes"] || !keyPath || !self.thumbnailView.image) {
  164. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForShow:mediaObject];
  165. }
  166. } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
  167. MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
  168. [self _configureForShowEpisode:mediaObject];
  169. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || !self.thumbnailView.image) {
  170. MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
  171. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
  172. }
  173. }
  174. [self setNeedsDisplay];
  175. }
  176. + (CGFloat)heightOfCell
  177. {
  178. if (SYSTEM_RUNS_IOS7_OR_LATER)
  179. return 90.;
  180. return 80.;
  181. }
  182. #pragma mark - presentation
  183. - (void)_configureForShow:(MLShow *)show
  184. {
  185. self.titleLabel.text = show.name;
  186. NSUInteger count = show.episodes.count;
  187. NSString *string = @"";
  188. if (show.releaseYear)
  189. string = [NSString stringWithFormat:@"%@ — ", show.releaseYear];
  190. self.subtitleLabel.text = [string stringByAppendingString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", @"") : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", @""), count, show.unreadEpisodes.count]];
  191. self.mediaIsUnreadView.hidden = YES;
  192. self.progressIndicator.hidden = YES;
  193. self.folderIconView.image = [UIImage imageNamed:@"PlayingExternally"];
  194. self.folderIconView.hidden = NO;
  195. }
  196. - (void)_configureForAlbumTrack:(MLAlbumTrack *)albumTrack
  197. {
  198. MLFile *anyFileFromTrack = albumTrack.files.anyObject;
  199. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@ — %@", albumTrack.artist, [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue], [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
  200. self.titleLabel.text = albumTrack.title;
  201. [self _showPositionOfItem:anyFileFromTrack];
  202. self.folderIconView.hidden = YES;
  203. }
  204. - (void)_configureForShowEpisode:(MLShowEpisode *)showEpisode
  205. {
  206. self.titleLabel.text = showEpisode.name;
  207. MLFile *anyFileFromEpisode = showEpisode.files.anyObject;
  208. if (self.titleLabel.text.length < 1) {
  209. self.titleLabel.text = [NSString stringWithFormat:@"S%02dE%02d", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue];
  210. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  211. } else
  212. self.subtitleLabel.text = [NSString stringWithFormat:@"S%02dE%02d — %@", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue, [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  213. [self _showPositionOfItem:anyFileFromEpisode];
  214. self.folderIconView.hidden = YES;
  215. }
  216. - (void)_configureForAlbum:(MLAlbum *)album
  217. {
  218. self.titleLabel.text = album.name;
  219. MLAlbumTrack *anyTrack = [album.tracks anyObject];
  220. NSUInteger count = album.tracks.count;
  221. NSMutableString *string = [[NSMutableString alloc] init];
  222. if (anyTrack) {
  223. if (anyTrack.artist.length > 0) {
  224. [string appendString:anyTrack.artist];
  225. [string appendString:@" — "];
  226. }
  227. }
  228. [string appendString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count]];
  229. if (album.releaseYear.length > 0)
  230. [string appendFormat:@" — %@", album.releaseYear];
  231. self.subtitleLabel.text = string;
  232. self.mediaIsUnreadView.hidden = YES;
  233. self.progressIndicator.hidden = YES;
  234. self.folderIconView.hidden = YES;
  235. }
  236. - (void)_configureForFolder:(MLLabel *)label
  237. {
  238. self.titleLabel.text = label.name;
  239. NSUInteger count = label.files.count;
  240. self.subtitleLabel.text = [NSString stringWithFormat:(count == 1) ? NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @"") : NSLocalizedString(@"LIBRARY_TRACKS", @""), count];
  241. self.mediaIsUnreadView.hidden = YES;
  242. self.progressIndicator.hidden = YES;
  243. self.folderIconView.image = [UIImage imageNamed:@"folderIcon"];
  244. self.folderIconView.hidden = NO;
  245. }
  246. - (void)_configureForMLFile:(MLFile *)mediaFile
  247. {
  248. if (mediaFile.isAlbumTrack) {
  249. NSString *string = @"";
  250. if (mediaFile.albumTrack.artist)
  251. string = [NSString stringWithFormat:@"%@ — ", mediaFile.albumTrack.artist];
  252. else if (mediaFile.albumTrack.album.name)
  253. string = [NSString stringWithFormat:@"%@ — ", mediaFile.albumTrack.artist];
  254. self.titleLabel.text = [string stringByAppendingString:(mediaFile.albumTrack.title.length > 1) ? mediaFile.albumTrack.title : mediaFile.title];
  255. } else
  256. self.titleLabel.text = mediaFile.title;
  257. if (self.isEditing)
  258. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@", [VLCTime timeWithNumber:[mediaFile duration]], [NSByteCountFormatter stringFromByteCount:[mediaFile fileSizeInBytes] countStyle:NSByteCountFormatterCountStyleFile]];
  259. else {
  260. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[mediaFile duration]]];
  261. if (mediaFile.videoTrack) {
  262. NSString *width = [[mediaFile videoTrack] valueForKey:@"width"];
  263. NSString *height = [[mediaFile videoTrack] valueForKey:@"height"];
  264. if (width.intValue > 0 && height.intValue > 0)
  265. self.subtitleLabel.text = [self.subtitleLabel.text stringByAppendingFormat:@" — %@x%@", width, height];
  266. }
  267. }
  268. [self _showPositionOfItem:mediaFile];
  269. self.folderIconView.hidden = YES;
  270. }
  271. - (void)_showPositionOfItem:(MLFile *)mediaLibraryFile
  272. {
  273. CGFloat position = mediaLibraryFile.lastPosition.floatValue;
  274. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  275. CGFloat duration = mediaLibraryFile.duration.floatValue;
  276. if (position > .05f && position < .95f && (duration * position - duration) < -60000) {
  277. [(UITextView*)self.mediaIsUnreadView setText:[NSString stringWithFormat:NSLocalizedString(@"LIBRARY_MINUTES_LEFT", @""), [[VLCTime timeWithInt:(duration * position - duration)] minuteStringValue]]];
  278. self.mediaIsUnreadView.hidden = NO;
  279. } else if (mediaLibraryFile.unread.intValue) {
  280. [(UILabel *)self.mediaIsUnreadView setText:[NSLocalizedString(@"NEW", @"") capitalizedStringWithLocale:[NSLocale currentLocale]]];
  281. self.mediaIsUnreadView.hidden = NO;
  282. } else
  283. self.mediaIsUnreadView.hidden = YES;
  284. } else {
  285. self.progressIndicator.progress = position;
  286. self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
  287. [self.progressIndicator setNeedsDisplay];
  288. self.mediaIsUnreadView.hidden = !mediaLibraryFile.unread.intValue;
  289. }
  290. }
  291. - (void)longTouchGestureAction:(UIGestureRecognizer *)recognizer
  292. {
  293. CGRect frame = self.frame;
  294. if (frame.size.height > 90.)
  295. frame.size.height = 90.;
  296. else if (recognizer.state == UIGestureRecognizerStateBegan)
  297. frame.size.height = 180;
  298. void (^animationBlock)() = ^() {
  299. self.frame = frame;
  300. };
  301. void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
  302. self.frame = frame;
  303. };
  304. NSTimeInterval animationDuration = .2;
  305. [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
  306. }
  307. @end