VLCPlaylistCollectionViewCell.m 16 KB

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