VLCPlaylistTableViewCell.m 15 KB

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