VLCPlaylistTableViewCell.m 15 KB

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