VLCPlaylistCollectionViewCell.m 15 KB

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