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

#import "VLCPlaylistCollectionViewCell.h"
17
#import "VLCThumbnailsCache.h"
18
#import "NSString+SupportedMedia.h"
19

20 21
#import <VLCMediaLibraryKit/VLCMLFile.h>

22 23 24 25
@interface VLCPlaylistCollectionViewCell ()
{
    UIImage *_checkboxEmptyImage;
    UIImage *_checkboxImage;
26
    BOOL _editing;
27 28 29 30
}

@end

31 32
@implementation VLCPlaylistCollectionViewCell

33 34 35 36 37
- (void)dealloc
{
    [self _removeObserver];
}

38 39
- (void)awakeFromNib
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
40 41
    _checkboxEmptyImage = [UIImage imageNamed:@"checkboxEmpty"];
    _checkboxImage = [UIImage imageNamed:@"checkbox"];
42
    self.metaDataLabel.hidden = YES;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
43
    [super awakeFromNib];
44 45
}

46 47 48 49 50
+ (NSString *)cellIdentifier
{
    return @"VLCPlaylistCollectionViewCell";
}

51 52
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
53 54
    _editing = editing;
    self.isSelectedView.hidden = !_editing;
55

56
    [self shake];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
57
    [self selectionUpdate];
58 59 60
    [self _updatedDisplayedInformationForKeyPath:@"editing"];
}

61 62 63
- (void)selectionUpdate
{
    if (self.selected)
64
        self.isSelectedView.image = _checkboxImage;
65
    else
66
        self.isSelectedView.image = _checkboxEmptyImage;
67 68
}

69
- (void)shake
70
{
71
    if (_editing) {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        [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"];
    }
}

95 96 97 98 99
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    [self _updatedDisplayedInformationForKeyPath:keyPath];
}

100 101
- (void)_removeObserver
{
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    @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"];
121
            MLFile *anyFileFromTrack = [(MLAlbumTrack *)_mediaObject anyFileFromTrack];
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            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");
141 142 143 144 145
    }
}

- (void)_addObserver
{
146 147
    if ([_mediaObject isKindOfClass:[MLLabel class]]) {
        [_mediaObject addObserver:self forKeyPath:@"files" options:0 context:nil];
148
        [_mediaObject addObserver:self forKeyPath:@"name" options:0 context:nil];
149
    } else if ([_mediaObject isKindOfClass:[MLShow class]])
150 151 152 153 154 155 156 157 158
        [_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];
159 160 161 162 163
    } 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];
164
        MLFile *anyFileFromTrack = [(MLAlbumTrack *)_mediaObject anyFileFromTrack];
165 166
        if (anyFileFromTrack)
            [anyFileFromTrack addObserver:self forKeyPath:@"artworkURL" options:0 context:nil];
167 168 169 170 171 172 173 174 175 176 177 178
    } 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];
179 180 181 182
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(thumbnailWasUpdated:)
                                                     name:MLFileThumbnailWasUpdated
                                                   object:nil];
183 184 185 186
        [(MLFile*)_mediaObject willDisplay];
    }
}

187 188 189
- (void)setMediaObject:(MLFile *)mediaObject
{
    if (_mediaObject != mediaObject) {
190 191

        [self _removeObserver];
192 193

        _mediaObject = mediaObject;
194 195
        // prevent the cell from recycling the current snap for random contents
        self.thumbnailView.image = nil;
196

197
        [self _addObserver];
198 199 200 201 202
    }

    [self _updatedDisplayedInformationForKeyPath:nil];
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
- (void)setMedia:(VLCMLMedia *)media
{
    if (_media != media) {
        _media = media;
    }
    [self _updateDisplayedInformations];
}

- (void)_updateDisplayedInformations
{
    _titleLabel.text = _media.title;
    _subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@", [VLCTime timeWithNumber:[NSNumber numberWithLongLong:_media.duration]],
                                                                  [NSByteCountFormatter stringFromByteCount:[_media.mainFile size] countStyle:NSByteCountFormatterCountStyleFile]];

}

219 220
- (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
{
221
    self.thumbnailView.contentMode = UIViewContentModeScaleAspectFill;
222
    if ([self.mediaObject isKindOfClass:[MLFile class]]) {
223
        [self _configureForMLFile:(MLFile *)self.mediaObject];
224
    } else if ([self.mediaObject isKindOfClass:[MLLabel class]]) {
225
        [self _configureForFolder:(MLLabel *)self.mediaObject];
226
    } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
227
        [self _configureForAlbum:(MLAlbum *)self.mediaObject];
228
    } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
229
        [self _configureForAlbumTrack:(MLAlbumTrack *)self.mediaObject];
230
    } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
231

232
        MLShow *mediaObject = (MLShow *)self.mediaObject;
233
        [self _configureForShow:mediaObject];
234

235
        if ([keyPath isEqualToString:@"computedThumbnail"] || [keyPath isEqualToString:@"episodes"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
236
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForManagedObject:mediaObject];
237 238
        }
    } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
239 240 241 242
        [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];
243 244 245 246 247 248
    }
    [self setNeedsDisplay];
}

#pragma mark - presentation

249
- (void)_configureForShow:(MLShow *)show
250 251 252
{
    self.titleLabel.text = show.name;
    NSUInteger count = show.episodes.count;
253 254 255
    NSString *string = @"";
    if (show.releaseYear)
        string = [NSString stringWithFormat:@"%@ — ", show.releaseYear];
256
    self.subtitleLabel.text = [string stringByAppendingString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", nil) : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", nil), count, show.unreadEpisodes.count]];
257 258
    self.mediaIsUnreadView.hidden = YES;
    self.progressView.hidden = YES;
259
    self.folderIconView.image = [UIImage imageNamed:@"tvShow"];
260
    self.folderIconView.hidden = NO;
261 262
}

263
- (void)_configureForAlbumTrack:(MLAlbumTrack *)albumTrack
264
{
265
    MLFile *anyFileFromTrack = albumTrack.anyFileFromTrack;
266

267
    self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@ — %@", albumTrack.artist, [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", nil), albumTrack.trackNumber.intValue], [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
268
    self.titleLabel.text = albumTrack.title;
269

270
    [self _showPositionOfItem:anyFileFromTrack];
271
    self.folderIconView.hidden = YES;
272 273
}

274
- (void)_configureForShowEpisode:(MLShowEpisode *)showEpisode
275 276
{
    self.titleLabel.text = showEpisode.name;
277 278

    MLFile *anyFileFromEpisode = showEpisode.files.anyObject;
279
    if (self.titleLabel.text.length < 1) {
280
        self.titleLabel.text = [NSString stringWithFormat:@"S%02dE%02d", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue];
281 282
        self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
    } else
283
        self.subtitleLabel.text = [NSString stringWithFormat:@"S%02dE%02d — %@", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue, [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
284
    [self _showPositionOfItem:anyFileFromEpisode];
285
    self.folderIconView.hidden = YES;
286 287
}

288 289 290 291 292
- (void)_configureForAlbum:(MLAlbum *)album
{
    self.titleLabel.text = album.name;
    MLAlbumTrack *anyTrack = [album.tracks anyObject];
    NSUInteger count = album.tracks.count;
293 294
    NSMutableString *string = [[NSMutableString alloc] init];
    if (anyTrack) {
295 296 297 298
        if (anyTrack.artist.length > 0) {
            [string appendString:anyTrack.artist];
            [string appendString:@" — "];
        }
299
    }
300
    [string appendString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", nil) : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", nil), count]];
301
    if (album.releaseYear.length > 0)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
302
        [string appendFormat:@" — %@", album.releaseYear];
303
    self.subtitleLabel.text = string;
304 305
    self.mediaIsUnreadView.hidden = YES;
    self.progressView.hidden = YES;
306
    self.folderIconView.hidden = YES;
307 308
}

309 310 311 312
- (void)_configureForFolder:(MLLabel *)label
{
    self.titleLabel.text = label.name;
    NSUInteger count = label.files.count;
313
    self.subtitleLabel.text = (count == 1) ? NSLocalizedString(@"ONE_FILE", nil) : [NSString stringWithFormat:NSLocalizedString(@"NUM_OF_FILES", nil), count];
314 315
    self.mediaIsUnreadView.hidden = YES;
    self.progressView.hidden = YES;
316 317
    self.folderIconView.image = [UIImage imageNamed:@"folderIcon"];
    self.folderIconView.hidden = NO;
318 319
}

320
- (void)_configureForMLFile:(MLFile *)mediaFile
321
{
322
    if (mediaFile.isAlbumTrack) {
323 324 325 326 327 328
        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];
329 330 331
    } else
        self.titleLabel.text = mediaFile.title;

332
    if (_editing)
333
        self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@", [VLCTime timeWithNumber:[mediaFile duration]], [NSByteCountFormatter stringFromByteCount:[mediaFile fileSizeInBytes] countStyle:NSByteCountFormatterCountStyleFile]];
334 335 336 337 338 339 340 341 342
    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];
        }
    }
343
    [self _showPositionOfItem:mediaFile];
344
    self.folderIconView.hidden = YES;
345 346
}

347
- (void)_showPositionOfItem:(MLFile *)mediaLibraryFile
348
{
349 350
    if (!mediaLibraryFile)
        return;
351
    CGFloat position = mediaLibraryFile.lastPosition.floatValue;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
352 353 354 355 356
    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;
357 358 359
    } else if (position != 0.) {
        self.mediaIsUnreadView.hidden = YES;
    } else {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
360 361
        [(UILabel *)self.mediaIsUnreadView setText:[NSLocalizedString(@"NEW", nil) capitalizedStringWithLocale:[NSLocale currentLocale]]];
        self.mediaIsUnreadView.hidden = NO;
362
    }
363 364
}

365 366 367 368 369 370 371
- (void)showMetadata:(BOOL)showMeta
{
    if (showMeta) {
        NSMutableString *mediaInfo = [[NSMutableString alloc] init];

        MLFile *theFile;
        if ([self.mediaObject isKindOfClass:[MLFile class]])
Carola Nitz's avatar
Carola Nitz committed
372
            theFile = (MLFile *)self.mediaObject;
373 374 375
        else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]])
            theFile = [[(MLShowEpisode *)self.mediaObject files]anyObject];
        else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]])
376
            theFile = [(MLAlbumTrack *)self.mediaObject anyFileFromTrack];
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

        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) {
404 405 406 407 408
            if (trackCount != 1)
                [mediaInfo appendFormat:@"%lu video tracks", (unsigned long)trackCount];
            else
                [mediaInfo appendString:@"1 video track"];

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
            [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) {
424 425 426 427 428 429 430 431
            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"];

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
            [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;
477 478
        if (trackCount == 0) {
            NSFileManager *fileManager = [NSFileManager defaultManager];
479
            NSString *folderLocation = [theFile.url.path stringByDeletingLastPathComponent];
480
            NSArray *allfiles = [fileManager contentsOfDirectoryAtPath:folderLocation error:nil];
481
            NSString *fileName = [theFile.path.lastPathComponent stringByDeletingPathExtension];
482 483 484 485 486 487 488 489 490
            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 ++;
            }
        }
491

492
        if ((trackCount > 0) || (spuTracks.count > 0)) {
493 494 495 496 497 498 499 500
            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"];

501 502 503 504
            if (spuTracks.count > 0) {
                [mediaInfo appendString:@" ("];
                for (NSUInteger x = 0; x < trackCount; x++) {
                    NSString *language = [spuTracks[x] valueForKey:@"language"];
505

506 507 508 509 510 511
                    if (language) {
                        if (x != 0)
                            [mediaInfo appendFormat:@", %@", [language uppercaseString]];
                        else
                            [mediaInfo appendString:[language uppercaseString]];
                    }
512
                }
513
                [mediaInfo appendString:@")"];
514 515 516 517 518 519 520
            }
        }

        self.metaDataLabel.text = mediaInfo;
        videoTracks = audioTracks = spuTracks = nil;
    }

521
    void (^animationBlock)(void) = ^() {
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
        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;
}

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

Carola Nitz's avatar
Carola Nitz committed
544 545 546
- (void)prepareForReuse
{
    [super prepareForReuse];
547
    self.media = nil;
Carola Nitz's avatar
Carola Nitz committed
548 549 550 551 552 553 554 555 556 557 558 559
    self.mediaObject = nil;
    self.thumbnailView.image = nil;
    self.titleLabel.text = @"";
    self.subtitleLabel.text = @"";
    self.mediaIsUnreadView.hidden = YES;
    self.progressView.hidden = YES;
    self.metaDataLabel.hidden = YES;
    self.folderIconView.hidden = YES;
    self.isSelectedView.hidden = YES;
    self.metaDataLabel.hidden = YES;
    self.mediaIsUnreadView.hidden = YES;
}
560
@end