123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- /*****************************************************************************
- * VLCPlaylistCollectionViewCell.m
- * VLC for iOS
- *****************************************************************************
- * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
- * $Id$
- *
- * Authors: Felix Paul Kühne <fkuehne # videolan.org>
- * Tamas Timar <ttimar.vlc # gmail.com>
- * Gleb Pinigin <gpinigin # gmail.com>
- * Carola Nitz <nitz.carola # googlemail.com>
- *
- * Refer to the COPYING file of the official project for license.
- *****************************************************************************/
- #import "VLCPlaylistCollectionViewCell.h"
- #import "VLCPlaylistViewController.h"
- #import "VLCThumbnailsCache.h"
- #import "NSString+SupportedMedia.h"
- @interface VLCPlaylistCollectionViewCell ()
- {
- UIImage *_checkboxEmptyImage;
- UIImage *_checkboxImage;
- }
- @end
- @implementation VLCPlaylistCollectionViewCell
- - (void)dealloc
- {
- [self _removeObserver];
- }
- - (void)awakeFromNib
- {
- if (SYSTEM_RUNS_IOS7_OR_LATER) {
- _checkboxEmptyImage = [UIImage imageNamed:@"checkboxEmpty"];
- _checkboxImage = [UIImage imageNamed:@"checkbox"];
- } else {
- _checkboxEmptyImage = [UIImage imageNamed:@"checkbox-legacy-empty"];
- _checkboxImage = [UIImage imageNamed:@"checkbox-legacy"];
- }
- self.metaDataLabel.hidden = YES;
- }
- - (void)setEditing:(BOOL)editing animated:(BOOL)animated
- {
- self.isSelectedView.hidden = !editing;
- if (!([_mediaObject isKindOfClass:[MLFile class]] && [_mediaObject.labels count] > 0))
- [self shake:editing];
- [self selectionUpdate];
- [self _updatedDisplayedInformationForKeyPath:@"editing"];
- }
- - (void)selectionUpdate
- {
- if (self.selected)
- self.isSelectedView.image = _checkboxImage;
- else
- self.isSelectedView.image = _checkboxEmptyImage;
- }
- - (void)shake:(BOOL)shake
- {
- if (shake) {
- [UIView animateWithDuration:0.3 animations:^{
- self.contentView.transform = CGAffineTransformMakeScale(0.9f, 0.9f);
- }];
- CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
- CGFloat shakeAngle = 0.02f;
- animation.values = @[@(-shakeAngle), @(shakeAngle)];
- animation.autoreverses = YES;
- animation.duration = 0.125;
- animation.repeatCount = HUGE_VALF;
- [[self layer] addAnimation:animation forKey:@"shakeAnimation"];
- self.contentView.layer.cornerRadius = 10.0;
- self.contentView.clipsToBounds = YES;
- } else {
- [UIView animateWithDuration:0.3 animations:^{
- self.contentView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
- self.contentView.layer.cornerRadius = 0.0;
- self.contentView.clipsToBounds = NO;
- }];
- [[self layer] removeAnimationForKey:@"shakeAnimation"];
- }
- }
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
- {
- [self _updatedDisplayedInformationForKeyPath:keyPath];
- }
- - (void)_removeObserver
- {
- if ([_mediaObject isKindOfClass:[MLLabel class]])
- [_mediaObject removeObserver:self forKeyPath:@"name"];
- else if ([_mediaObject isKindOfClass:[MLShow class]])
- [_mediaObject removeObserver:self forKeyPath:@"episodes"];
- else if ([_mediaObject isKindOfClass:[MLShowEpisode class]]) {
- [_mediaObject removeObserver:self forKeyPath:@"name"];
- [_mediaObject removeObserver:self forKeyPath:@"files"];
- [_mediaObject removeObserver:self forKeyPath:@"artworkURL"];
- [_mediaObject removeObserver:self forKeyPath:@"unread"];
- } else if ([_mediaObject isKindOfClass:[MLAlbum class]]) {
- [_mediaObject removeObserver:self forKeyPath:@"name"];
- [_mediaObject removeObserver:self forKeyPath:@"tracks"];
- } else if ([_mediaObject isKindOfClass:[MLFile class]]) {
- [_mediaObject removeObserver:self forKeyPath:@"computedThumbnail"];
- [_mediaObject removeObserver:self forKeyPath:@"lastPosition"];
- [_mediaObject removeObserver:self forKeyPath:@"duration"];
- [_mediaObject removeObserver:self forKeyPath:@"fileSizeInBytes"];
- [_mediaObject removeObserver:self forKeyPath:@"title"];
- [_mediaObject removeObserver:self forKeyPath:@"thumbnailTimeouted"];
- [_mediaObject removeObserver:self forKeyPath:@"unread"];
- [_mediaObject removeObserver:self forKeyPath:@"albumTrackNumber"];
- [_mediaObject removeObserver:self forKeyPath:@"album"];
- [_mediaObject removeObserver:self forKeyPath:@"artist"];
- [_mediaObject removeObserver:self forKeyPath:@"genre"];
- [(MLFile*)_mediaObject didHide];
- }
- }
- - (void)_addObserver
- {
- if ([_mediaObject isKindOfClass:[MLLabel class]])
- [_mediaObject addObserver:self forKeyPath:@"name" options:0 context:nil];
- else if ([_mediaObject isKindOfClass:[MLShow class]])
- [_mediaObject addObserver:self forKeyPath:@"episodes" options:0 context:nil];
- else if ([_mediaObject isKindOfClass:[MLShowEpisode class]]) {
- [_mediaObject addObserver:self forKeyPath:@"name" options:0 context:nil];
- [_mediaObject addObserver:self forKeyPath:@"files" options:0 context:nil];
- [_mediaObject addObserver:self forKeyPath:@"artworkURL" options:0 context:nil];
- [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
- } else if ([_mediaObject isKindOfClass:[MLAlbum class]]) {
- [_mediaObject addObserver:self forKeyPath:@"name" options:0 context:nil];
- [_mediaObject addObserver:self forKeyPath:@"tracks" options:0 context:nil];
- } else if ([_mediaObject isKindOfClass:[MLFile class]]) {
- [_mediaObject addObserver:self forKeyPath:@"computedThumbnail" options:0 context:nil];
- [_mediaObject addObserver:self forKeyPath:@"lastPosition" options:0 context:nil];
- [_mediaObject addObserver:self forKeyPath:@"duration" options:0 context:nil];
- [_mediaObject addObserver:self forKeyPath:@"fileSizeInBytes" options:0 context:nil];
- [_mediaObject addObserver:self forKeyPath:@"title" options:0 context:nil];
- [_mediaObject addObserver:self forKeyPath:@"thumbnailTimeouted" options:0 context:nil];
- [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
- [_mediaObject addObserver:self forKeyPath:@"albumTrackNumber" options:0 context:nil];
- [_mediaObject addObserver:self forKeyPath:@"album" options:0 context:nil];
- [_mediaObject addObserver:self forKeyPath:@"artist" options:0 context:nil];
- [_mediaObject addObserver:self forKeyPath:@"genre" options:0 context:nil];
- [(MLFile*)_mediaObject willDisplay];
- }
- }
- - (void)setMediaObject:(MLFile *)mediaObject
- {
- if (_mediaObject != mediaObject) {
- [self _removeObserver];
- _mediaObject = mediaObject;
- // prevent the cell from recycling the current snap for random contents
- self.thumbnailView.image = nil;
- [self _addObserver];
- }
- [self _updatedDisplayedInformationForKeyPath:nil];
- }
- - (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
- {
- self.thumbnailView.contentMode = UIViewContentModeScaleAspectFill;
- if ([self.mediaObject isKindOfClass:[MLFile class]]) {
- [self _configureForMLFile:self.mediaObject];
- } else if ([self.mediaObject isKindOfClass:[MLLabel class]]) {
- [self _configureForFolder:(MLLabel *)self.mediaObject];
- } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
- [self _configureForAlbum:(MLAlbum *)self.mediaObject];
- } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
- [self _configureForAlbumTrack:(MLAlbumTrack *)self.mediaObject];
- } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
- MLShow *mediaObject = (MLShow *)self.mediaObject;
- [self _configureForShow:mediaObject];
- if ([keyPath isEqualToString:@"computedThumbnail"] || [keyPath isEqualToString:@"episodes"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
- self.thumbnailView.image = [VLCThumbnailsCache thumbnailForManagedObject:mediaObject];
- }
- } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
- [self _configureForShowEpisode:(MLShowEpisode *)self.mediaObject];
- }
- if (![self.mediaObject isKindOfClass:[MLShow class]] && ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"]))) {
- self.thumbnailView.image = [VLCThumbnailsCache thumbnailForManagedObject:self.mediaObject];
- }
- [self setNeedsDisplay];
- }
- #pragma mark - presentation
- - (void)_configureForShow:(MLShow *)show
- {
- self.titleLabel.text = show.name;
- NSUInteger count = show.episodes.count;
- NSString *string = @"";
- if (show.releaseYear)
- string = [NSString stringWithFormat:@"%@ — ", show.releaseYear];
- self.subtitleLabel.text = [string stringByAppendingString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", nil) : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", nil), count, show.unreadEpisodes.count]];
- self.mediaIsUnreadView.hidden = YES;
- self.progressView.hidden = YES;
- self.folderIconView.image = [UIImage imageNamed:@"tvShow"];
- self.folderIconView.hidden = NO;
- }
- - (void)_configureForAlbumTrack:(MLAlbumTrack *)albumTrack
- {
- MLFile *anyFileFromTrack = albumTrack.files.anyObject;
- self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@ — %@", albumTrack.artist, [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", nil), albumTrack.trackNumber.intValue], [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
- self.titleLabel.text = albumTrack.title;
- [self _showPositionOfItem:anyFileFromTrack];
- self.folderIconView.hidden = YES;
- }
- - (void)_configureForShowEpisode:(MLShowEpisode *)showEpisode
- {
- self.titleLabel.text = showEpisode.name;
- MLFile *anyFileFromEpisode = showEpisode.files.anyObject;
- if (self.titleLabel.text.length < 1) {
- self.titleLabel.text = [NSString stringWithFormat:@"S%02dE%02d", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue];
- self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
- } else
- self.subtitleLabel.text = [NSString stringWithFormat:@"S%02dE%02d — %@", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue, [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
- [self _showPositionOfItem:anyFileFromEpisode];
- self.folderIconView.hidden = YES;
- }
- - (void)_configureForAlbum:(MLAlbum *)album
- {
- self.titleLabel.text = album.name;
- MLAlbumTrack *anyTrack = [album.tracks anyObject];
- NSUInteger count = album.tracks.count;
- NSMutableString *string = [[NSMutableString alloc] init];
- if (anyTrack) {
- if (anyTrack.artist.length > 0) {
- [string appendString:anyTrack.artist];
- [string appendString:@" — "];
- }
- }
- [string appendString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", nil) : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", nil), count]];
- if (album.releaseYear.length > 0)
- [string appendFormat:@" — %@", album.releaseYear];
- self.subtitleLabel.text = string;
- self.mediaIsUnreadView.hidden = YES;
- self.progressView.hidden = YES;
- self.folderIconView.hidden = YES;
- }
- - (void)_configureForFolder:(MLLabel *)label
- {
- self.titleLabel.text = label.name;
- NSUInteger count = label.files.count;
- self.subtitleLabel.text = [NSString stringWithFormat:(count == 1) ? NSLocalizedString(@"LIBRARY_SINGLE_TRACK", nil) : NSLocalizedString(@"LIBRARY_TRACKS", nil), count];
- self.mediaIsUnreadView.hidden = YES;
- self.progressView.hidden = YES;
- self.folderIconView.image = [UIImage imageNamed:@"folderIcon"];
- self.folderIconView.hidden = NO;
- }
- - (void)_configureForMLFile:(MLFile *)mediaFile
- {
- if (mediaFile.isAlbumTrack) {
- NSString *string = @"";
- if (mediaFile.albumTrack.artist)
- string = [NSString stringWithFormat:@"%@ — ", mediaFile.albumTrack.artist];
- else if (mediaFile.albumTrack.album.name)
- string = [NSString stringWithFormat:@"%@ — ", mediaFile.albumTrack.artist];
- self.titleLabel.text = [string stringByAppendingString:(mediaFile.albumTrack.title.length > 1) ? mediaFile.albumTrack.title : mediaFile.title];
- } else
- self.titleLabel.text = mediaFile.title;
- VLCPlaylistViewController *delegate = (VLCPlaylistViewController*)self.collectionView.delegate;
- if (delegate.isEditing)
- self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@", [VLCTime timeWithNumber:[mediaFile duration]], [NSByteCountFormatter stringFromByteCount:[mediaFile fileSizeInBytes] countStyle:NSByteCountFormatterCountStyleFile]];
- else {
- self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[mediaFile duration]]];
- if (mediaFile.videoTrack) {
- NSString *width = [[mediaFile videoTrack] valueForKey:@"width"];
- NSString *height = [[mediaFile videoTrack] valueForKey:@"height"];
- if (width.intValue > 0 && height.intValue > 0)
- self.subtitleLabel.text = [self.subtitleLabel.text stringByAppendingFormat:@" — %@x%@", width, height];
- }
- }
- [self _showPositionOfItem:mediaFile];
- self.folderIconView.hidden = YES;
- }
- - (void)_showPositionOfItem:(MLFile *)mediaLibraryFile
- {
- CGFloat position = mediaLibraryFile.lastPosition.floatValue;
- if (SYSTEM_RUNS_IOS7_OR_LATER) {
- CGFloat duration = mediaLibraryFile.duration.floatValue;
- if (position > .05f && position < .95f && (duration * position - duration) < -60000) {
- [(UITextView*)self.mediaIsUnreadView setText:[NSString stringWithFormat:NSLocalizedString(@"LIBRARY_MINUTES_LEFT", nil), [[VLCTime timeWithInt:(duration * position - duration)] minuteStringValue]]];
- self.mediaIsUnreadView.hidden = NO;
- } else if (mediaLibraryFile.unread.intValue) {
- [(UILabel *)self.mediaIsUnreadView setText:[NSLocalizedString(@"NEW", nil) capitalizedStringWithLocale:[NSLocale currentLocale]]];
- self.mediaIsUnreadView.hidden = NO;
- } else
- self.mediaIsUnreadView.hidden = YES;
- } else {
- self.progressView.progress = position;
- self.progressView.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
- [self.progressView setNeedsDisplay];
- self.mediaIsUnreadView.hidden = !mediaLibraryFile.unread.intValue;
- }
- }
- - (void)showMetadata:(BOOL)showMeta
- {
- if (showMeta) {
- NSMutableString *mediaInfo = [[NSMutableString alloc] init];
- MLFile *theFile;
- if ([self.mediaObject isKindOfClass:[MLFile class]])
- theFile = self.mediaObject;
- else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]])
- theFile = [[(MLShowEpisode *)self.mediaObject files]anyObject];
- else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]])
- theFile = [[(MLAlbumTrack *)self.mediaObject files]anyObject];
- if (!theFile) {
- self.metaDataLabel.hidden = YES;
- return;
- }
- NSMutableArray *videoTracks = [[NSMutableArray alloc] init];
- NSMutableArray *audioTracks = [[NSMutableArray alloc] init];
- NSMutableArray *spuTracks = [[NSMutableArray alloc] init];
- NSArray *tracks = [[theFile tracks] allObjects];
- NSUInteger trackCount = tracks.count;
- for (NSUInteger x = 0; x < trackCount; x++) {
- NSManagedObject *track = tracks[x];
- NSString *trackEntityName = [[track entity] name];
- if ([trackEntityName isEqualToString:@"VideoTrackInformation"])
- [videoTracks addObject:track];
- else if ([trackEntityName isEqualToString:@"AudioTrackInformation"])
- [audioTracks addObject:track];
- else if ([trackEntityName isEqualToString:@"SubtitlesTrackInformation"])
- [spuTracks addObject:track];
- }
- /* print video info */
- trackCount = videoTracks.count;
- if (trackCount > 0) {
- if (trackCount != 1)
- [mediaInfo appendFormat:@"%lu video tracks", (unsigned long)trackCount];
- else
- [mediaInfo appendString:@"1 video track"];
- [mediaInfo appendString:@" ("];
- for (NSUInteger x = 0; x < trackCount; x++) {
- int fourcc = [[videoTracks[x] valueForKey:@"codec"] intValue];
- if (x != 0)
- [mediaInfo appendFormat:@", %4.4s", (char *)&fourcc];
- else
- [mediaInfo appendFormat:@"%4.4s", (char *)&fourcc];
- }
- [mediaInfo appendString:@")"];
- }
- /* print audio info */
- trackCount = audioTracks.count;
- if (trackCount > 0) {
- if (mediaInfo.length > 0)
- [mediaInfo appendString:@"\n\n"];
- if (trackCount != 1)
- [mediaInfo appendFormat:@"%lu audio tracks", (unsigned long)trackCount];
- else
- [mediaInfo appendString:@"1 audio track"];
- [mediaInfo appendString:@":\n"];
- for (NSUInteger x = 0; x < trackCount; x++) {
- NSManagedObject *track = audioTracks[x];
- int fourcc = [[track valueForKey:@"codec"] intValue];
- if (x != 0)
- [mediaInfo appendFormat:@", %4.4s", (char *)&fourcc];
- else
- [mediaInfo appendFormat:@"%4.4s", (char *)&fourcc];
- int channelNumber = [[track valueForKey:@"channelsNumber"] intValue];
- NSString *language = [track valueForKey:@"language"];
- int bitrate = [[track valueForKey:@"bitrate"] intValue];
- if (channelNumber != 0 || language != nil || bitrate > 0) {
- [mediaInfo appendString:@" ["];
- if (bitrate > 0)
- [mediaInfo appendFormat:@"%i kbit/s", bitrate / 1024];
- if (channelNumber > 0) {
- if (bitrate > 0)
- [mediaInfo appendString:@", "];
- if (channelNumber == 1)
- [mediaInfo appendString:@"MONO"];
- else if (channelNumber == 2)
- [mediaInfo appendString:@"STEREO"];
- else
- [mediaInfo appendString:@"MULTI-CHANNEL"];
- }
- if (language != nil) {
- if (channelNumber > 0 || bitrate > 0)
- [mediaInfo appendString:@", "];
- [mediaInfo appendString:[language uppercaseString]];
- }
- [mediaInfo appendString:@"]"];
- }
- }
- }
- [mediaInfo appendString:@"\n\n"];
- /* SPU */
- trackCount = spuTracks.count;
- if (trackCount == 0) {
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *folderLocation = [theFile.url.path stringByDeletingLastPathComponent];
- NSArray *allfiles = [fileManager contentsOfDirectoryAtPath:folderLocation error:nil];
- NSString *fileName = [theFile.path.lastPathComponent stringByDeletingPathExtension];
- NSUInteger count = allfiles.count;
- NSString *additionalFilePath;
- for (unsigned int x = 0; x < count; x++) {
- additionalFilePath = [NSString stringWithFormat:@"%@", allfiles[x]];
- if ([additionalFilePath isSupportedSubtitleFormat])
- if ([additionalFilePath rangeOfString:fileName].location != NSNotFound)
- trackCount ++;
- }
- }
- if ((trackCount > 0) && (spuTracks.count > 0)) {
- if (mediaInfo.length > 0)
- [mediaInfo appendString:@"\n\n"];
- if (trackCount != 1)
- [mediaInfo appendFormat:@"%lu subtitles tracks", (unsigned long)trackCount];
- else
- [mediaInfo appendString:@"1 subtitles track"];
- [mediaInfo appendString:@" ("];
- for (NSUInteger x = 0; x < trackCount; x++) {
- NSString *language = [spuTracks[x] valueForKey:@"language"];
- if (language) {
- if (x != 0)
- [mediaInfo appendFormat:@", %@", [language uppercaseString]];
- else
- [mediaInfo appendString:[language uppercaseString]];
- }
- }
- [mediaInfo appendString:@")"];
- }
- self.metaDataLabel.text = mediaInfo;
- videoTracks = audioTracks = spuTracks = nil;
- }
- void (^animationBlock)() = ^() {
- self.metaDataLabel.hidden = !showMeta;
- };
- void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
- self.metaDataLabel.hidden = !showMeta;
- };
- NSTimeInterval animationDuration = .2;
- [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
- }
- - (BOOL)showsMetaData
- {
- return !self.metaDataLabel.hidden;
- }
- @end
|