VLCPlaylistCollectionViewCell.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*****************************************************************************
  2. * VLCPlaylistCollectionViewCell.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. * Tamas Timar <ttimar.vlc # gmail.com>
  10. * Gleb Pinigin <gpinigin # gmail.com>
  11. * Carola Nitz <nitz.carola # googlemail.com>
  12. *
  13. * Refer to the COPYING file of the official project for license.
  14. *****************************************************************************/
  15. #import "VLCPlaylistCollectionViewCell.h"
  16. #import "VLCPlaylistViewController.h"
  17. #import "VLCThumbnailsCache.h"
  18. @interface VLCPlaylistCollectionViewCell ()
  19. {
  20. UIImage *_checkboxEmptyImage;
  21. UIImage *_checkboxImage;
  22. }
  23. @end
  24. @implementation VLCPlaylistCollectionViewCell
  25. - (void)awakeFromNib
  26. {
  27. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  28. _checkboxEmptyImage = [UIImage imageNamed:@"checkboxEmpty"];
  29. _checkboxImage = [UIImage imageNamed:@"checkbox"];
  30. } else {
  31. _checkboxEmptyImage = [UIImage imageNamed:@"checkbox-legacy-empty"];
  32. _checkboxImage = [UIImage imageNamed:@"checkbox-legacy"];
  33. }
  34. }
  35. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  36. {
  37. self.isSelectedView.hidden = !editing;
  38. if (!([_mediaObject isKindOfClass:[MLFile class]] && [_mediaObject.labels count] > 0))
  39. [self shake:editing];
  40. [self selectionUpdate];
  41. [self _updatedDisplayedInformationForKeyPath:@"editing"];
  42. }
  43. - (void)selectionUpdate
  44. {
  45. if (self.selected)
  46. self.isSelectedView.image = _checkboxImage;
  47. else
  48. self.isSelectedView.image = _checkboxEmptyImage;
  49. }
  50. - (void)shake:(BOOL)shake
  51. {
  52. if (shake) {
  53. [UIView animateWithDuration:0.3 animations:^{
  54. self.contentView.transform = CGAffineTransformMakeScale(0.9f, 0.9f);
  55. }];
  56. CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
  57. CGFloat shakeAngle = 0.02f;
  58. animation.values = @[@(-shakeAngle), @(shakeAngle)];
  59. animation.autoreverses = YES;
  60. animation.duration = 0.125;
  61. animation.repeatCount = HUGE_VALF;
  62. [[self layer] addAnimation:animation forKey:@"shakeAnimation"];
  63. self.contentView.layer.cornerRadius = 10.0;
  64. self.contentView.clipsToBounds = YES;
  65. } else {
  66. [UIView animateWithDuration:0.3 animations:^{
  67. self.contentView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
  68. self.contentView.layer.cornerRadius = 0.0;
  69. self.contentView.clipsToBounds = NO;
  70. }];
  71. [[self layer] removeAnimationForKey:@"shakeAnimation"];
  72. }
  73. }
  74. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  75. {
  76. [self _updatedDisplayedInformationForKeyPath:keyPath];
  77. }
  78. - (void)setMediaObject:(MLFile *)mediaObject
  79. {
  80. if (_mediaObject != mediaObject) {
  81. if ([_mediaObject isKindOfClass:[MLLabel class]]) {
  82. [_mediaObject removeObserver:self forKeyPath:@"files"];
  83. [_mediaObject removeObserver:self forKeyPath:@"name"];
  84. } else if ([_mediaObject isKindOfClass:[MLShow class]])
  85. [_mediaObject removeObserver:self forKeyPath:@"episodes"];
  86. else if ([_mediaObject isKindOfClass:[MLShowEpisode class]]) {
  87. [_mediaObject removeObserver:self forKeyPath:@"name"];
  88. [_mediaObject removeObserver:self forKeyPath:@"files"];
  89. [_mediaObject removeObserver:self forKeyPath:@"artworkURL"];
  90. [_mediaObject removeObserver:self forKeyPath:@"unread"];
  91. } else if ([_mediaObject isKindOfClass:[MLAlbum class]]) {
  92. [_mediaObject removeObserver:self forKeyPath:@"name"];
  93. [_mediaObject removeObserver:self forKeyPath:@"tracks"];
  94. } else if ([_mediaObject isKindOfClass:[MLFile class]]) {
  95. [_mediaObject removeObserver:self forKeyPath:@"computedThumbnail"];
  96. [_mediaObject removeObserver:self forKeyPath:@"lastPosition"];
  97. [_mediaObject removeObserver:self forKeyPath:@"duration"];
  98. [_mediaObject removeObserver:self forKeyPath:@"fileSizeInBytes"];
  99. [_mediaObject removeObserver:self forKeyPath:@"title"];
  100. [_mediaObject removeObserver:self forKeyPath:@"thumbnailTimeouted"];
  101. [_mediaObject removeObserver:self forKeyPath:@"unread"];
  102. [_mediaObject removeObserver:self forKeyPath:@"albumTrackNumber"];
  103. [_mediaObject removeObserver:self forKeyPath:@"album"];
  104. [_mediaObject removeObserver:self forKeyPath:@"artist"];
  105. [_mediaObject removeObserver:self forKeyPath:@"genre"];
  106. [_mediaObject removeObserver:self forKeyPath:@"labels"];
  107. [(MLFile*)_mediaObject didHide];
  108. }
  109. _mediaObject = mediaObject;
  110. // prevent the cell from recycling the current snap for random contents
  111. self.thumbnailView.image = nil;
  112. if ([_mediaObject isKindOfClass:[MLLabel class]]) {
  113. [_mediaObject addObserver:self forKeyPath:@"files" options:0 context:nil];
  114. [_mediaObject addObserver:self forKeyPath:@"name" options:0 context:nil];
  115. } else if ([_mediaObject isKindOfClass:[MLShow class]])
  116. [_mediaObject addObserver:self forKeyPath:@"episodes" options:0 context:nil];
  117. else if ([_mediaObject isKindOfClass:[MLShowEpisode class]]) {
  118. [_mediaObject addObserver:self forKeyPath:@"name" options:0 context:nil];
  119. [_mediaObject addObserver:self forKeyPath:@"files" options:0 context:nil];
  120. [_mediaObject addObserver:self forKeyPath:@"artworkURL" options:0 context:nil];
  121. [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
  122. } else if ([_mediaObject isKindOfClass:[MLAlbum class]]) {
  123. [_mediaObject addObserver:self forKeyPath:@"name" options:0 context:nil];
  124. [_mediaObject addObserver:self forKeyPath:@"tracks" options:0 context:nil];
  125. } else if ([_mediaObject isKindOfClass:[MLFile class]]) {
  126. [_mediaObject addObserver:self forKeyPath:@"computedThumbnail" options:0 context:nil];
  127. [_mediaObject addObserver:self forKeyPath:@"lastPosition" options:0 context:nil];
  128. [_mediaObject addObserver:self forKeyPath:@"duration" options:0 context:nil];
  129. [_mediaObject addObserver:self forKeyPath:@"fileSizeInBytes" options:0 context:nil];
  130. [_mediaObject addObserver:self forKeyPath:@"title" options:0 context:nil];
  131. [_mediaObject addObserver:self forKeyPath:@"thumbnailTimeouted" options:0 context:nil];
  132. [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
  133. [_mediaObject addObserver:self forKeyPath:@"albumTrackNumber" options:0 context:nil];
  134. [_mediaObject addObserver:self forKeyPath:@"album" options:0 context:nil];
  135. [_mediaObject addObserver:self forKeyPath:@"artist" options:0 context:nil];
  136. [_mediaObject addObserver:self forKeyPath:@"genre" options:0 context:nil];
  137. [_mediaObject addObserver:self forKeyPath:@"labels" options:0 context:nil];
  138. [(MLFile*)_mediaObject willDisplay];
  139. }
  140. }
  141. [self _updatedDisplayedInformationForKeyPath:nil];
  142. }
  143. - (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
  144. {
  145. self.thumbnailView.contentMode = UIViewContentModeScaleAspectFill;
  146. if ([self.mediaObject isKindOfClass:[MLFile class]]) {
  147. MLFile *mediaObject = self.mediaObject;
  148. [self _configureForMLFile:mediaObject];
  149. if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"]))
  150. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:mediaObject];
  151. } else if ([self.mediaObject isKindOfClass:[MLLabel class]]) {
  152. MLLabel *mediaObject = (MLLabel *)self.mediaObject;
  153. [self _configureForFolder:mediaObject];
  154. if ([keyPath isEqualToString:@"files"] || [keyPath isEqualToString:@"labels"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  155. if (mediaObject.files.count == 0) {
  156. self.thumbnailView.contentMode = UIViewContentModeScaleAspectFit;
  157. self.thumbnailView.image = [UIImage imageNamed:@"folderIcon"];
  158. } else
  159. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForLabel:mediaObject];
  160. }
  161. } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
  162. MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;
  163. [self _configureForAlbum:mediaObject];
  164. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  165. MLFile *anyFileFromAnyTrack = [mediaObject.tracks.anyObject files].anyObject;
  166. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyTrack];
  167. }
  168. } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
  169. MLAlbumTrack *mediaObject = (MLAlbumTrack *)self.mediaObject;
  170. [self _configureForAlbumTrack:mediaObject];
  171. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || !self.thumbnailView.image) {
  172. MLFile *anyFileFromTrack = mediaObject.files.anyObject;
  173. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromTrack];
  174. }
  175. } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
  176. MLShow *mediaObject = (MLShow *)self.mediaObject;
  177. [self _configureForShow:mediaObject];
  178. if ([keyPath isEqualToString:@"computedThumbnail"] || [keyPath isEqualToString:@"episodes"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
  179. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForShow:mediaObject];
  180. }
  181. } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
  182. MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
  183. [self _configureForShowEpisode:mediaObject];
  184. if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || !self.thumbnailView.image) {
  185. MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
  186. self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
  187. }
  188. }
  189. [self setNeedsDisplay];
  190. }
  191. #pragma mark - presentation
  192. - (void)_configureForShow:(MLShow *)show
  193. {
  194. self.titleLabel.text = show.name;
  195. NSUInteger count = show.episodes.count;
  196. NSString *string = @"";
  197. if (show.releaseYear)
  198. string = [NSString stringWithFormat:@"%@ — ", show.releaseYear];
  199. self.subtitleLabel.text = [string stringByAppendingString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", @"") : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", @""), count, show.unreadEpisodes.count]];
  200. self.mediaIsUnreadView.hidden = YES;
  201. self.progressView.hidden = YES;
  202. self.folderIconView.image = [UIImage imageNamed:@"PlayingExternally"];
  203. self.folderIconView.hidden = NO;
  204. }
  205. - (void)_configureForAlbumTrack:(MLAlbumTrack *)albumTrack
  206. {
  207. MLFile *anyFileFromTrack = albumTrack.files.anyObject;
  208. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@ — %@", albumTrack.artist, [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue], [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
  209. self.titleLabel.text = albumTrack.title;
  210. [self _showPositionOfItem:anyFileFromTrack];
  211. self.folderIconView.hidden = YES;
  212. }
  213. - (void)_configureForShowEpisode:(MLShowEpisode *)showEpisode
  214. {
  215. self.titleLabel.text = showEpisode.name;
  216. MLFile *anyFileFromEpisode = showEpisode.files.anyObject;
  217. if (self.titleLabel.text.length < 1) {
  218. self.titleLabel.text = [NSString stringWithFormat:@"S%02dE%02d", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue];
  219. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  220. } else
  221. self.subtitleLabel.text = [NSString stringWithFormat:@"S%02dE%02d — %@", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue, [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
  222. [self _showPositionOfItem:anyFileFromEpisode];
  223. self.folderIconView.hidden = YES;
  224. }
  225. - (void)_configureForAlbum:(MLAlbum *)album
  226. {
  227. self.titleLabel.text = album.name;
  228. MLAlbumTrack *anyTrack = [album.tracks anyObject];
  229. NSUInteger count = album.tracks.count;
  230. NSMutableString *string = [[NSMutableString alloc] init];
  231. if (anyTrack) {
  232. if (anyTrack.artist.length > 0) {
  233. [string appendString:anyTrack.artist];
  234. [string appendString:@" — "];
  235. }
  236. }
  237. [string appendString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count]];
  238. if (album.releaseYear.length > 0)
  239. [string appendFormat:@" — %@", album.releaseYear];
  240. self.subtitleLabel.text = string;
  241. self.mediaIsUnreadView.hidden = YES;
  242. self.progressView.hidden = YES;
  243. self.folderIconView.hidden = YES;
  244. }
  245. - (void)_configureForFolder:(MLLabel *)label
  246. {
  247. self.titleLabel.text = label.name;
  248. NSUInteger count = label.files.count;
  249. self.subtitleLabel.text = [NSString stringWithFormat:(count == 1) ? NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @"") : NSLocalizedString(@"LIBRARY_TRACKS", @""), count];
  250. self.mediaIsUnreadView.hidden = YES;
  251. self.progressView.hidden = YES;
  252. self.folderIconView.image = [UIImage imageNamed:@"folderIcon"];
  253. self.folderIconView.hidden = NO;
  254. }
  255. - (void)_configureForMLFile:(MLFile *)mediaFile
  256. {
  257. if (mediaFile.isAlbumTrack) {
  258. NSString *string = @"";
  259. if (mediaFile.albumTrack.artist)
  260. string = [NSString stringWithFormat:@"%@ — ", mediaFile.albumTrack.artist];
  261. else if (mediaFile.albumTrack.album.name)
  262. string = [NSString stringWithFormat:@"%@ — ", mediaFile.albumTrack.artist];
  263. self.titleLabel.text = [string stringByAppendingString:(mediaFile.albumTrack.title.length > 1) ? mediaFile.albumTrack.title : mediaFile.title];
  264. } else
  265. self.titleLabel.text = mediaFile.title;
  266. VLCPlaylistViewController *delegate = (VLCPlaylistViewController*)self.collectionView.delegate;
  267. if (delegate.isEditing)
  268. self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@", [VLCTime timeWithNumber:[mediaFile duration]], [NSByteCountFormatter stringFromByteCount:[mediaFile fileSizeInBytes] countStyle:NSByteCountFormatterCountStyleFile]];
  269. else {
  270. self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[mediaFile duration]]];
  271. if (mediaFile.videoTrack) {
  272. NSString *width = [[mediaFile videoTrack] valueForKey:@"width"];
  273. NSString *height = [[mediaFile videoTrack] valueForKey:@"height"];
  274. if (width.intValue > 0 && height.intValue > 0)
  275. self.subtitleLabel.text = [self.subtitleLabel.text stringByAppendingFormat:@" — %@x%@", width, height];
  276. }
  277. }
  278. [self _showPositionOfItem:mediaFile];
  279. self.folderIconView.hidden = YES;
  280. }
  281. - (void)_showPositionOfItem:(MLFile *)mediaItem
  282. {
  283. CGFloat position = mediaItem.lastPosition.floatValue;
  284. if (SYSTEM_RUNS_IOS7_OR_LATER) {
  285. CGFloat duration = mediaItem.duration.floatValue;
  286. if (position > .05f && position < .95f && (duration * position - duration) < -60000) {
  287. [(UITextView*)self.mediaIsUnreadView setText:[NSString stringWithFormat:NSLocalizedString(@"LIBRARY_MINUTES_LEFT", @""), [[VLCTime timeWithInt:(duration * position - duration)] minuteStringValue]]];
  288. self.mediaIsUnreadView.hidden = NO;
  289. } else if (mediaItem.unread.intValue) {
  290. [(UILabel *)self.mediaIsUnreadView setText:[NSLocalizedString(@"NEW", @"") capitalizedStringWithLocale:[NSLocale currentLocale]]];
  291. self.mediaIsUnreadView.hidden = NO;
  292. } else
  293. self.mediaIsUnreadView.hidden = YES;
  294. } else {
  295. self.progressView.progress = position;
  296. self.progressView.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
  297. [self.progressView setNeedsDisplay];
  298. self.mediaIsUnreadView.hidden = !mediaItem.unread.intValue;
  299. }
  300. }
  301. @end