VLCPlaylistTableViewCell.m 9.7 KB
Newer Older
1 2 3 4 5 6 7
//
//  VLCPlaylistTableViewCell.m
//  AspenProject
//
//  Created by Felix Paul Kühne on 01.04.13.
//  Copyright (c) 2013 VideoLAN. All rights reserved.
//
8 9
//  Refer to the COPYING file of the official project for license.
//
10 11

#import "VLCPlaylistTableViewCell.h"
12
#import "VLCLinearProgressIndicator.h"
13
#import "VLCThumbnailsCache.h"
14
#import <MediaLibraryKit/MLAlbum.h>
15 16 17 18 19

@implementation VLCPlaylistTableViewCell

+ (VLCPlaylistTableViewCell *)cellWithReuseIdentifier:(NSString *)ident
{
20 21 22 23 24
    NSArray *nibContentArray;
    if (SYSTEM_RUNS_IN_THE_FUTURE)
        nibContentArray = [[NSBundle mainBundle] loadNibNamed:@"VLCFuturePlaylistTableViewCell" owner:nil options:nil];
    else
        nibContentArray = [[NSBundle mainBundle] loadNibNamed:@"VLCPlaylistTableViewCell" owner:nil options:nil];
25 26
    NSAssert([nibContentArray count] == 1, @"meh");
    NSAssert([[nibContentArray lastObject] isKindOfClass:[VLCPlaylistTableViewCell class]], @"meh meh");
Felix Paul Kühne's avatar
Felix Paul Kühne committed
27
    VLCPlaylistTableViewCell *cell = (VLCPlaylistTableViewCell *)[nibContentArray lastObject];
28 29 30 31

    return cell;
}

32 33
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
34
    [self _updatedDisplayedInformationForKeyPath:keyPath];
35 36
}

37 38 39 40 41 42 43 44 45
- (void)setMediaObject:(MLFile *)mediaObject
{
    if (_mediaObject != mediaObject) {
        [_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"];
46
        [_mediaObject removeObserver:self forKeyPath:@"unread"];
47 48 49 50 51 52
        [_mediaObject removeObserver:self forKeyPath:@"albumTrackNumber"];
        [_mediaObject removeObserver:self forKeyPath:@"album"];
        [_mediaObject removeObserver:self forKeyPath:@"artist"];
        [_mediaObject removeObserver:self forKeyPath:@"genre"];
        if ([_mediaObject respondsToSelector:@selector(didHide)])
            [(MLFile*)_mediaObject didHide];
53 54 55 56 57 58 59 60 61

        _mediaObject = mediaObject;

        [_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];
62
        [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
63 64 65 66 67 68 69
        [_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];

        if ([_mediaObject respondsToSelector:@selector(willDisplay)])
            [(MLFile*)_mediaObject willDisplay];
70 71
    }

72
    [self _updatedDisplayedInformationForKeyPath:nil];
73 74
}

75 76 77
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
78
    [self _updatedDisplayedInformationForKeyPath:@"editing"];
79 80
}

81
- (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
82
{
83 84
    self.albumNameLabel.text = self.artistNameLabel.text = @"";

85 86
    if ([self.mediaObject isKindOfClass:[MLFile class]]) {
        MLFile *mediaObject = (MLFile*)self.mediaObject;
87
        [self configureForMLFile:mediaObject];
88

89
        if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) && !mediaObject.isAlbumTrack) {
90
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:mediaObject];
91
        }
92

93 94
    } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
        MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;
95 96
        [self configureForAlbum:mediaObject];

97 98
    } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
        MLAlbumTrack *mediaObject = (MLAlbumTrack *)self.mediaObject;
99
        [self configureForAlbumTrack:mediaObject];
100

101 102
    } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
        MLShow *mediaObject = (MLShow *)self.mediaObject;
103
        [self configureForShow:mediaObject];
104 105

        if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
106
            MLFile *anyFileFromAnyEpisode = [mediaObject.episodes.anyObject files].anyObject;
107
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyEpisode];
108
        }
109

110 111
    } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
        MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
112
        [self configureForShowEpisode:mediaObject];
113 114

        if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) {
115
            MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
116
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
117
        }
118
    }
119

120 121 122
    [self setNeedsDisplay];
}

123 124
+ (CGFloat)heightOfCell
{
125 126 127
    if (SYSTEM_RUNS_IN_THE_FUTURE)
        return 185.;

128 129 130
    return 80.;
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
#pragma mark - presentation

- (void)configureForShow:(MLShow *)show
{
    self.titleLabel.text = show.name;
    self.artistNameLabel.text = @"";
    self.albumNameLabel.text = show.releaseYear;
    NSUInteger count = show.episodes.count;
    self.subtitleLabel.text = [NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", @"") : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", @""), count, show.unreadEpisodes.count];
    self.mediaIsUnreadView.hidden = YES;
    self.progressIndicator.hidden = YES;
}

- (void)configureForAlbumTrack:(MLAlbumTrack *)albumTrack
{
    self.artistNameLabel.text = albumTrack.artist;
    self.albumNameLabel.text = [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue];
    self.titleLabel.text = albumTrack.title;
    self.thumbnailView.image = nil;

    MLFile *anyFileFromTrack = albumTrack.files.anyObject;
    self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromTrack duration]]];

    CGFloat position = anyFileFromTrack.lastPosition.floatValue;
    self.progressIndicator.progress = position;
    self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
    [self.progressIndicator setNeedsDisplay];
    self.mediaIsUnreadView.hidden = !anyFileFromTrack.unread.intValue;
}

- (void)configureForShowEpisode:(MLShowEpisode *)showEpisode
{
    self.titleLabel.text = showEpisode.name;

    MLFile *anyFileFromEpisode = showEpisode.files.anyObject;
    if (self.titleLabel.text.length < 1) {
167
        self.titleLabel.text = [NSString stringWithFormat:@"S%02dE%02d", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue];
168 169
        self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
    } else
170
        self.subtitleLabel.text = [NSString stringWithFormat:@"S%02dE%02d — %@", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue, [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

    CGFloat position = anyFileFromEpisode.lastPosition.floatValue;
    self.progressIndicator.progress = position;
    self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
    [self.progressIndicator setNeedsDisplay];
    self.mediaIsUnreadView.hidden = !showEpisode.unread.intValue;
}

- (void)configureForAlbum:(MLAlbum *)album
{
    self.titleLabel.text = album.name;
    MLAlbumTrack *anyTrack = [album.tracks anyObject];
    self.artistNameLabel.text = anyTrack? anyTrack.artist: @"";
    self.albumNameLabel.text = album.releaseYear;
    self.thumbnailView.image = nil;
    NSUInteger count = album.tracks.count;
    self.subtitleLabel.text = [NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count];
    self.mediaIsUnreadView.hidden = YES;
    self.progressIndicator.hidden = YES;
}

192 193
- (void)configureForMLFile:(MLFile *)mediaFile
{
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    if (mediaFile.isAlbumTrack) {
        self.artistNameLabel.text = mediaFile.albumTrack.artist;
        self.albumNameLabel.text = mediaFile.albumTrack.album.name;
        self.titleLabel.text = (mediaFile.albumTrack.title.length > 1) ? mediaFile.albumTrack.title : mediaFile.title;
        self.thumbnailView.image = nil;
    } else
        self.titleLabel.text = mediaFile.title;

    if (self.isEditing)
        self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %i MB", [VLCTime timeWithNumber:[mediaFile duration]], (int)([mediaFile fileSizeInBytes] / 1e6)];
    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];
        }
    }

    CGFloat position = mediaFile.lastPosition.floatValue;
    self.progressIndicator.progress = position;
    self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
    [self.progressIndicator setNeedsDisplay];
    self.mediaIsUnreadView.hidden = !mediaFile.unread.intValue;
}

221
@end