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

#import "VLCPlaylistTableViewCell.h"
15
#import "VLCLinearProgressIndicator.h"
16
#import "VLCThumbnailsCache.h"
17
#import <MediaLibraryKit/MLAlbum.h>
18
19
20
21
22

@implementation VLCPlaylistTableViewCell

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

    return cell;
}

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

40
41
42
43
44
45
46
47
48
- (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"];
49
        [_mediaObject removeObserver:self forKeyPath:@"unread"];
50
51
52
53
54
55
        [_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];
56
57
58
59
60
61
62
63
64

        _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];
65
        [_mediaObject addObserver:self forKeyPath:@"unread" options:0 context:nil];
66
67
68
69
70
71
72
        [_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];
73
74
    }

75
    [self _updatedDisplayedInformationForKeyPath:nil];
76
77
}

78
79
80
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
81
    [self _updatedDisplayedInformationForKeyPath:@"editing"];
82
83
}

84
- (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
85
{
86
87
    self.albumNameLabel.text = self.artistNameLabel.text = @"";

88
89
    if ([self.mediaObject isKindOfClass:[MLFile class]]) {
        MLFile *mediaObject = (MLFile*)self.mediaObject;
90
        [self _configureForMLFile:mediaObject];
91

92
        if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) && !mediaObject.isAlbumTrack)
93
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:mediaObject];
94

95
96
    } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
        MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;
97
        [self _configureForAlbum:mediaObject];
98

99
100
    } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
        MLAlbumTrack *mediaObject = (MLAlbumTrack *)self.mediaObject;
101
        [self _configureForAlbumTrack:mediaObject];
102

103
104
    } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
        MLShow *mediaObject = (MLShow *)self.mediaObject;
105
        [self _configureForShow:mediaObject];
106
107

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

112
113
    } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
        MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
114
        [self _configureForShowEpisode:mediaObject];
115
116

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

122
123
124
    [self setNeedsDisplay];
}

125
126
+ (CGFloat)heightOfCell
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
127
    if (SYSTEM_RUNS_IOS7_OR_LATER)
128
        return 90.;
129

130
131
132
    return 80.;
}

133
134
#pragma mark - presentation

135
- (void)_configureForShow:(MLShow *)show
136
137
138
{
    self.titleLabel.text = show.name;
    NSUInteger count = show.episodes.count;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
139
    if (SYSTEM_RUNS_IOS7_OR_LATER) {
140
141
142
143
        NSString *string = @"";
        if (show.releaseYear)
            string = [NSString stringWithFormat:@"%@ — ", show.releaseYear];
        self.subtitleLabel.text = [string stringByAppendingString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", @"") : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", @""), count, show.unreadEpisodes.count]];
144
145
146
147
148
    } else {
        self.artistNameLabel.text = @"";
        self.albumNameLabel.text = show.releaseYear;
        self.subtitleLabel.text = [NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_EPISODES", @"") : NSLocalizedString(@"LIBRARY_SINGLE_EPISODE", @""), count, show.unreadEpisodes.count];
    }
149
150
151
152
    self.mediaIsUnreadView.hidden = YES;
    self.progressIndicator.hidden = YES;
}

153
- (void)_configureForAlbumTrack:(MLAlbumTrack *)albumTrack
154
{
155
156
    MLFile *anyFileFromTrack = albumTrack.files.anyObject;

Felix Paul Kühne's avatar
Felix Paul Kühne committed
157
    if (SYSTEM_RUNS_IOS7_OR_LATER)
158
159
160
161
162
163
        self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@ — %@", albumTrack.artist, [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue], [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
    else {
        self.artistNameLabel.text = albumTrack.artist;
        self.albumNameLabel.text = [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue];
        self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
    }
164
165
166
    self.titleLabel.text = albumTrack.title;
    self.thumbnailView.image = nil;

167
    [self _showPositionOfItem:anyFileFromTrack];
168
169
}

170
- (void)_configureForShowEpisode:(MLShowEpisode *)showEpisode
171
172
173
174
175
{
    self.titleLabel.text = showEpisode.name;

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

181
    [self _showPositionOfItem:anyFileFromEpisode];
182
183
}

184
- (void)_configureForAlbum:(MLAlbum *)album
185
186
187
188
{
    self.titleLabel.text = album.name;
    MLAlbumTrack *anyTrack = [album.tracks anyObject];
    NSUInteger count = album.tracks.count;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
189
    if (SYSTEM_RUNS_IOS7_OR_LATER) {
190
191
192
193
194
195
196
197
198
199
200
201
202
        NSMutableString *string = [[NSMutableString alloc] init];
        if (anyTrack) {
            [string appendString:anyTrack.artist];
            [string appendString:@" — "];
        }
        [string appendFormat:@"%@ — %@", [NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count], album.releaseYear];
        self.subtitleLabel.text = string;
    } else {
        self.artistNameLabel.text = anyTrack? anyTrack.artist: @"";
        self.albumNameLabel.text = album.releaseYear;
        self.subtitleLabel.text = [NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count];
    }
    self.thumbnailView.image = nil;
203
204
205
206
    self.mediaIsUnreadView.hidden = YES;
    self.progressIndicator.hidden = YES;
}

207
- (void)_configureForMLFile:(MLFile *)mediaFile
208
{
209
    if (mediaFile.isAlbumTrack) {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
210
        if (SYSTEM_RUNS_IOS7_OR_LATER) {
211
212
213
214
215
216
            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];
217
218
219
220
221
        } else {
            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;
        }
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
        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];
        }
    }

238
239
240
241
242
243
    [self _showPositionOfItem:mediaFile];
}

- (void)_showPositionOfItem:(MLFile *)mediaItem
{
    CGFloat position = mediaItem.lastPosition.floatValue;
244

Felix Paul Kühne's avatar
Felix Paul Kühne committed
245
    if (SYSTEM_RUNS_IOS7_OR_LATER) {
246
        CGFloat duration = mediaItem.duration.floatValue;
247
        if (position > .1f && position < .95f) {
248
            [(UITextView*)self.mediaIsUnreadView setText:[NSString stringWithFormat:NSLocalizedString(@"LIBRARY_MINUTES_LEFT", @""), [[VLCTime timeWithInt:(duration * position - duration)] minuteStringValue]]];
249
            self.mediaIsUnreadView.hidden = NO;
250
        } else if (mediaItem.unread.intValue) {
251
252
253
254
255
256
257
258
            [(UILabel *)self.mediaIsUnreadView setText:[NSLocalizedString(@"NEW", @"") capitalizedStringWithLocale:[NSLocale currentLocale]]];
            self.mediaIsUnreadView.hidden = NO;
        } else
            self.mediaIsUnreadView.hidden = YES;
    } else {
        self.progressIndicator.progress = position;
        self.progressIndicator.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
        [self.progressIndicator setNeedsDisplay];
259
        self.mediaIsUnreadView.hidden = !mediaItem.unread.intValue;
260
    }
261
262
}

263
@end