VLCPlaylistTableViewCell.m 15 KB

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