VLCPlaylistTableViewCell.m 23.2 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * VLCPlaylistTableViewCell.m
 * VLC for iOS
 *****************************************************************************
5
 * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
6 7 8 9
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan.org>
 *          Gleb Pinigin <gpinigin # gmail.com>
10
 *          Carola Nitz <nitz.carola #googlemail.com>
11 12 13
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/
14 15

#import "VLCPlaylistTableViewCell.h"
16
#import "VLCLinearProgressIndicator.h"
17
#import "VLCThumbnailsCache.h"
18
#import "NSString+SupportedMedia.h"
19

20 21 22 23 24 25 26
@interface VLCPlaylistTableViewCell ()
{
    UILongPressGestureRecognizer *_longPress;
}

@end

27 28
@implementation VLCPlaylistTableViewCell

29
- (void)awakeFromNib
30
{
31 32 33 34
    [super awakeFromNib];
    UIView *bgColorView = [[UIView alloc] init];
    bgColorView.backgroundColor = [UIColor clearColor];
    [self setSelectedBackgroundView:bgColorView];
35 36
}

37 38 39 40 41
+ (NSString *)cellIdentifier
{
    return @"VLCPlaylistTableViewCell";
}

42
- (void)dealloc
43
{
44
    [self _removeObserver];
45 46
}

47 48 49 50 51 52
- (void)prepareForReuse
{
    [super prepareForReuse];
    [self collapsWithAnimation:NO];
}

53 54 55 56 57 58
- (void)setNeedsDisplay
{
    [self collapsWithAnimation:NO];
    [super setNeedsDisplay];
}

59 60
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
61
    [self _updatedDisplayedInformationForKeyPath:keyPath];
62 63
}

64 65
- (void)_removeObserver
{
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    @try {
        if ([_mediaObject isKindOfClass:[MLLabel class]]) {
            [_mediaObject removeObserver:self forKeyPath:@"files"];
            [_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:[MLAlbumTrack class]]) {
            [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
            [_mediaObject removeObserver:self forKeyPath:@"artist"];
            [_mediaObject removeObserver:self forKeyPath:@"title"];
            [_mediaObject removeObserver:self forKeyPath:@"files"];
85
            MLFile *anyFileFromTrack = [(MLAlbumTrack *)_mediaObject anyFileFromTrack];
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
            if (anyFileFromTrack)
                [anyFileFromTrack removeObserver:self forKeyPath:@"artworkURL"];
        } 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];
        }
    }
    @catch (NSException *exception) {
        APLog(@"removing observer failed");
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    }
}

- (void)_addObserver
{
    if ([_mediaObject isKindOfClass:[MLLabel class]]) {
        [_mediaObject addObserver:self forKeyPath:@"files" options:0 context:nil];
        [_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];
123 124 125 126 127
    } else if ([_mediaObject isKindOfClass:[MLAlbumTrack class]]) {
        [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
        [_mediaObject addObserver:self forKeyPath:@"artist" options:0 context:nil];
        [_mediaObject addObserver:self forKeyPath:@"title" options:0 context:nil];
        [_mediaObject addObserver:self forKeyPath:@"files" options:0 context:nil];
128
        MLFile *anyFileFromTrack = [(MLAlbumTrack *)_mediaObject anyFileFromTrack];
129 130
        if (anyFileFromTrack)
            [anyFileFromTrack addObserver:self forKeyPath:@"artworkURL" options:0 context:nil];
131 132 133 134 135 136 137 138 139 140 141 142
    } 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];
143 144 145 146
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(thumbnailWasUpdated:)
                                                     name:MLFileThumbnailWasUpdated
                                                   object:nil];
147 148 149 150
        [(MLFile*)_mediaObject willDisplay];
    }
}

151 152 153
- (void)setMediaObject:(MLFile *)mediaObject
{
    if (_mediaObject != mediaObject) {
154
        [self _removeObserver];
155 156

        _mediaObject = mediaObject;
157 158
        // prevent the cell from recycling the current snap for random contents
        self.thumbnailView.image = nil;
159

160
        [self _addObserver];
161 162
    }

163
    [self collapsWithAnimation:NO];
164
    [self _updatedDisplayedInformationForKeyPath:nil];
165 166
}

167 168 169
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
170
    [self _updatedDisplayedInformationForKeyPath:@"editing"];
171

Felix Paul Kühne's avatar
Felix Paul Kühne committed
172 173 174 175 176 177 178
    if (editing) {
        if (_longPress)
            [self removeGestureRecognizer:_longPress];
    } else {
        if (!_longPress)
            _longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTouchGestureAction:)];
        [self addGestureRecognizer:_longPress];
179
    }
180 181
}

182
- (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
183
{
184
    self.thumbnailView.contentMode = UIViewContentModeScaleAspectFill;
185
    if ([self.mediaObject isKindOfClass:[MLFile class]]) {
186
        [self _configureForMLFile:(MLFile *)self.mediaObject];
187
    } else if ([self.mediaObject isKindOfClass:[MLLabel class]]) {
188
        [self _configureForFolder:(MLLabel *)self.mediaObject];
189
    } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
190
        [self _configureForAlbum:(MLAlbum *)self.mediaObject];
191
    } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
192
        [self _configureForAlbumTrack:(MLAlbumTrack *)self.mediaObject];
193 194 195
        if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForManagedObject:self.mediaObject];
        }
196
    } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
197

198
        MLShow *mediaObject = (MLShow *)self.mediaObject;
199
        [self _configureForShow:mediaObject];
200

201 202
        if ([keyPath isEqualToString:@"computedThumbnail"] || [keyPath isEqualToString:@"episodes"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForManagedObject:mediaObject];
203
        }
204
    } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
205 206 207 208
        [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];
209
    }
210 211 212
    [self setNeedsDisplay];
}

213 214
+ (CGFloat)heightOfCell
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
215
    return 90.;
216 217
}

218 219
#pragma mark - presentation

220
- (void)_configureForShow:(MLShow *)show
221 222 223
{
    self.titleLabel.text = show.name;
    NSUInteger count = show.episodes.count;
224 225 226
    NSString *string = @"";
    if (show.releaseYear)
        string = [NSString stringWithFormat:@"%@ — ", show.releaseYear];
227
    self.subtitleLabel.text = [string stringByAppendingString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", nil) : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", nil), count, show.unreadEpisodes.count]];
228 229
    self.mediaIsUnreadView.hidden = YES;
    self.progressIndicator.hidden = YES;
230
    self.folderIconView.image = [UIImage imageNamed:@"tvShow"];
231
    self.folderIconView.hidden = NO;
232 233
}

234
- (void)_configureForAlbumTrack:(MLAlbumTrack *)albumTrack
235
{
236
    MLFile *anyFileFromTrack = albumTrack.anyFileFromTrack;
237
    self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@ — %@", albumTrack.artist, [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", nil), albumTrack.trackNumber.intValue], [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
238
    self.titleLabel.text = albumTrack.title;
239
    [self _showPositionOfItem:anyFileFromTrack];
240
    self.folderIconView.hidden = YES;
241 242
}

243
- (void)_configureForShowEpisode:(MLShowEpisode *)showEpisode
244 245 246 247 248
{
    self.titleLabel.text = showEpisode.name;

    MLFile *anyFileFromEpisode = showEpisode.files.anyObject;
    if (self.titleLabel.text.length < 1) {
249
        self.titleLabel.text = [NSString stringWithFormat:@"S%02dE%02d", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue];
250 251
        self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
    } else
252
        self.subtitleLabel.text = [NSString stringWithFormat:@"S%02dE%02d — %@", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue, [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
253

254
    [self _showPositionOfItem:anyFileFromEpisode];
255
    self.folderIconView.hidden = YES;
256 257
}

258
- (void)_configureForAlbum:(MLAlbum *)album
259 260 261 262
{
    self.titleLabel.text = album.name;
    MLAlbumTrack *anyTrack = [album.tracks anyObject];
    NSUInteger count = album.tracks.count;
263 264
    NSMutableString *string = [[NSMutableString alloc] init];
    if (anyTrack) {
265 266 267 268
        if (anyTrack.artist.length > 0) {
            [string appendString:anyTrack.artist];
            [string appendString:@" — "];
        }
269
    }
270
    [string appendString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", nil) : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", nil), count]];
271
    if (album.releaseYear.length > 0)
272
        [string appendFormat:@" — %@", album.releaseYear];
273
    self.subtitleLabel.text = string;
274 275
    self.mediaIsUnreadView.hidden = YES;
    self.progressIndicator.hidden = YES;
276
    self.folderIconView.hidden = YES;
277 278
}

279 280 281 282
- (void)_configureForFolder:(MLLabel *)label
{
    self.titleLabel.text = label.name;
    NSUInteger count = label.files.count;
283
    self.subtitleLabel.text = (count == 1) ? NSLocalizedString(@"ONE_FILE", nil) : [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), count];
284 285
    self.mediaIsUnreadView.hidden = YES;
    self.progressIndicator.hidden = YES;
286 287
    self.folderIconView.image = [UIImage imageNamed:@"folderIcon"];
    self.folderIconView.hidden = NO;
288 289
}

290
- (void)_configureForMLFile:(MLFile *)mediaFile
291
{
292
    if (mediaFile.isAlbumTrack) {
293 294 295 296 297 298
        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];
299 300 301 302
    } else
        self.titleLabel.text = mediaFile.title;

    if (self.isEditing)
303
        self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@", [VLCTime timeWithNumber:[mediaFile duration]], [NSByteCountFormatter stringFromByteCount:[mediaFile fileSizeInBytes] countStyle:NSByteCountFormatterCountStyleFile]];
304 305 306 307 308 309 310 311 312
    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];
        }
    }
313
    [self _showPositionOfItem:mediaFile];
314
    self.folderIconView.hidden = YES;
315 316
}

317
- (void)_showPositionOfItem:(MLFile *)mediaLibraryFile
318
{
319 320
    if (!mediaLibraryFile)
        return;
321
    CGFloat position = mediaLibraryFile.lastPosition.floatValue;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
322 323 324 325 326
    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;
327 328 329
    } else if (position != 0.) {
        self.mediaIsUnreadView.hidden = YES;
    } else {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
330 331
        [(UILabel *)self.mediaIsUnreadView setText:[NSLocalizedString(@"NEW", nil) capitalizedStringWithLocale:[NSLocale currentLocale]]];
        self.mediaIsUnreadView.hidden = NO;
332
    }
333 334
}

335 336
- (void)longTouchGestureAction:(UIGestureRecognizer *)recognizer
{
337
    _isExpanded = YES;
338
    CGRect frame = self.frame;
339 340 341 342 343 344 345
    frame.size.height = 180.;
    BOOL metaHidden = NO;

    MLFile *theFile;
    if ([self.mediaObject isKindOfClass:[MLFile class]])
        theFile = (MLFile *)self.mediaObject;
    else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]])
346
        theFile = [(MLShowEpisode *)self.mediaObject anyFileFromEpisode];
347
    else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]])
348
        theFile = [(MLAlbumTrack *)self.mediaObject anyFileFromTrack];
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

    NSMutableString *mediaInfo = [[NSMutableString alloc] init];

    if (theFile) {
        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) {
374 375 376 377 378
            if (trackCount != 1)
                [mediaInfo appendFormat:@"%lu video tracks", (unsigned long)trackCount];
            else
                [mediaInfo appendString:@"1 video track"];

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
            [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) {
394 395 396 397 398 399 400 401
            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"];

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
            [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:@"]"];
                }
            }
        }

        /* SPU */
        trackCount = spuTracks.count;
446 447
        if (trackCount == 0) {
            NSFileManager *fileManager = [NSFileManager defaultManager];
448
            NSString *folderLocation = [theFile.url.path stringByDeletingLastPathComponent];
449
            NSArray *allfiles = [fileManager contentsOfDirectoryAtPath:folderLocation error:nil];
450
            NSString *fileName = [theFile.path.lastPathComponent stringByDeletingPathExtension];
451 452 453 454 455 456 457 458 459
            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 ++;
            }
        }
460

461
        if ((trackCount > 0) || (spuTracks.count > 0)) {
462 463 464 465 466 467 468
            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"];
469
            
470
            if (spuTracks.count > 0) {
471 472 473 474 475 476 477 478 479 480
                [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]];
                    }
481
                }
482
                [mediaInfo appendString:@")"];
483
            }
484
        }
485 486

        self.metaDataLabel.text = mediaInfo;
487 488
        [self.metaDataLabel sizeToFit];
        [self.metaDataLabel setFrame:CGRectMake((self.frame.size.width - self.metaDataLabel.frame.size.width)/2, self.metaDataLabel.frame.origin.y, self.metaDataLabel.frame.size.width, self.metaDataLabel.frame.size.height)];
489 490 491 492
        videoTracks = audioTracks = spuTracks = nil;
    } else
        self.metaDataLabel.text = @"";

493
    void (^animationBlock)(void) = ^() {
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
        self.frame = frame;
        self.metaDataLabel.hidden = metaHidden;
    };

    void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
        self.frame = frame;
        self.metaDataLabel.hidden = metaHidden;
    };

    NSTimeInterval animationDuration = .2;
    [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
}

- (void)collapsWithAnimation:(BOOL)animate
{
    _isExpanded = NO;
    if (!animate) {
        self.metaDataLabel.hidden = YES;
        CGRect frame = self.frame;
513
        frame.size.height = 90.;
514 515 516 517 518 519 520
        self.frame = frame;
        return;
    }

    CGRect frame = self.frame;
    frame.size.height = 90.;
    BOOL metaHidden = YES;
521

522
    void (^animationBlock)(void) = ^() {
523
        self.frame = frame;
524
        self.metaDataLabel.hidden = metaHidden;
525 526 527 528
    };

    void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
        self.frame = frame;
529
        self.metaDataLabel.hidden = metaHidden;
530 531 532 533 534 535
    };

    NSTimeInterval animationDuration = .2;
    [UIView animateWithDuration:animationDuration animations:animationBlock completion:completionBlock];
}

536 537 538 539 540 541
- (void)thumbnailWasUpdated:(NSNotification *)aNotification
{
    self.thumbnailView.contentMode = UIViewContentModeScaleAspectFill;
    self.thumbnailView.image = [VLCThumbnailsCache thumbnailForManagedObject:self.mediaObject refreshCache:YES];
}

542
@end