VLCPlaylistTableViewCell.m 13.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*****************************************************************************
 * 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>
Carola's avatar
Carola committed
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 <MediaLibraryKit/MLAlbum.h>
19
20
21
22
23

@implementation VLCPlaylistTableViewCell

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

    return cell;
}

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

42
43
44
- (void)setMediaObject:(MLFile *)mediaObject
{
    if (_mediaObject != mediaObject) {
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        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:[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"];
            [_mediaObject removeObserver:self forKeyPath:@"labels"];
63
            [(MLFile*)_mediaObject didHide];
64
        }
65
66
67

        _mediaObject = mediaObject;

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        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:[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];
            [_mediaObject addObserver:self forKeyPath:@"labels" options:0 context:nil];
86
            [(MLFile*)_mediaObject willDisplay];
87
        }
88
89
    }

90
    [self _updatedDisplayedInformationForKeyPath:nil];
91
92
}

93
94
95
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
96
    [self _updatedDisplayedInformationForKeyPath:@"editing"];
97
98
}

99
- (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
100
{
Carola's avatar
Carola committed
101
102
103
    BOOL isFolder = [self.mediaObject isKindOfClass:[MLLabel class]];
    self.thumbnailView.contentMode = isFolder ? UIViewContentModeScaleAspectFit : UIViewContentModeScaleAspectFill;

104
105
    if ([self.mediaObject isKindOfClass:[MLFile class]]) {
        MLFile *mediaObject = (MLFile*)self.mediaObject;
106
        [self _configureForMLFile:mediaObject];
107

108
        if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])))
109
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:mediaObject];
Carola's avatar
Carola committed
110
111
112
    } else if (isFolder) {
        MLLabel *mediaObject = (MLLabel *)self.mediaObject;
        [self _configureForFolder:mediaObject];
113
114
115
        BOOL forceRefresh = NO;
        if ([keyPath isEqualToString:@"files"] || [keyPath isEqualToString:@"labels"] || !keyPath)
            forceRefresh = YES;
Carola's avatar
Carola committed
116

117
        if (forceRefresh || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
118
            if (mediaObject.files.count == 0)
Carola's avatar
Carola committed
119
                self.thumbnailView.image = [UIImage imageNamed:@"folderIcon"];
120
            else
121
                self.thumbnailView.image = [VLCThumbnailsCache thumbnailForLabel:mediaObject forceRefresh:forceRefresh];
Carola's avatar
Carola committed
122
        }
123
124
    } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
        MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;
125
        [self _configureForAlbum:mediaObject];
126

127
128
129
130
        if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
            MLFile *anyFileFromAnyTrack = [mediaObject.tracks.anyObject files].anyObject;
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyTrack];
        }
131
132
    } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
        MLAlbumTrack *mediaObject = (MLAlbumTrack *)self.mediaObject;
133
        [self _configureForAlbumTrack:mediaObject];
134

135
136
137
138
        if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
            MLFile *anyFileFromTrack = mediaObject.files.anyObject;
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromTrack];
        }
139
140
    } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
        MLShow *mediaObject = (MLShow *)self.mediaObject;
141
        [self _configureForShow:mediaObject];
142
143
144
        BOOL forceRefresh = NO;
        if ([keyPath isEqualToString:@"episodes"])
            forceRefresh = YES;
145

146
147
        if ([keyPath isEqualToString:@"computedThumbnail"] || forceRefresh || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForShow:mediaObject forceRefresh:forceRefresh];
148
        }
149
150
    } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
        MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
151
        [self _configureForShowEpisode:mediaObject];
152

153
        if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
154
            MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
155
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
156
        }
157
    }
158

159
160
161
    [self setNeedsDisplay];
}

162
163
+ (CGFloat)heightOfCell
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
164
    if (SYSTEM_RUNS_IOS7_OR_LATER)
165
        return 90.;
166

167
168
169
    return 80.;
}

170
171
#pragma mark - presentation

172
- (void)_configureForShow:(MLShow *)show
173
174
175
{
    self.titleLabel.text = show.name;
    NSUInteger count = show.episodes.count;
176
177
178
179
    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]];
180
181
182
183
    self.mediaIsUnreadView.hidden = YES;
    self.progressIndicator.hidden = YES;
}

184
- (void)_configureForAlbumTrack:(MLAlbumTrack *)albumTrack
185
{
186
    MLFile *anyFileFromTrack = albumTrack.files.anyObject;
187
    self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@ — %@", albumTrack.artist, [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue], [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
188
    self.titleLabel.text = albumTrack.title;
189
    [self _showPositionOfItem:anyFileFromTrack];
190
191
}

192
- (void)_configureForShowEpisode:(MLShowEpisode *)showEpisode
193
194
195
196
197
{
    self.titleLabel.text = showEpisode.name;

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

203
    [self _showPositionOfItem:anyFileFromEpisode];
204
205
}

206
- (void)_configureForAlbum:(MLAlbum *)album
207
208
209
210
{
    self.titleLabel.text = album.name;
    MLAlbumTrack *anyTrack = [album.tracks anyObject];
    NSUInteger count = album.tracks.count;
211
212
213
214
    NSMutableString *string = [[NSMutableString alloc] init];
    if (anyTrack) {
        [string appendString:anyTrack.artist];
        [string appendString:@" — "];
215
    }
Felix Paul Kühne's avatar
Felix Paul Kühne committed
216
217
218
    [string appendString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count]];
    if (album.releaseYear)
        [string appendFormat:@" — %@", album.releaseYear];
219
    self.subtitleLabel.text = string;
220
221
222
223
    self.mediaIsUnreadView.hidden = YES;
    self.progressIndicator.hidden = YES;
}

Carola's avatar
Carola committed
224
225
226
227
- (void)_configureForFolder:(MLLabel *)label
{
    self.titleLabel.text = label.name;
    NSUInteger count = label.files.count;
228
    self.subtitleLabel.text = [NSString stringWithFormat:(count == 1) ? NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @"") : NSLocalizedString(@"LIBRARY_TRACKS", @""), count];
Carola's avatar
Carola committed
229
230
231
232
    self.mediaIsUnreadView.hidden = YES;
    self.progressIndicator.hidden = YES;
}

233
- (void)_configureForMLFile:(MLFile *)mediaFile
234
{
235
    if (mediaFile.isAlbumTrack) {
236
237
238
239
240
241
        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];
242
243
244
245
    } else
        self.titleLabel.text = mediaFile.title;

    if (self.isEditing)
246
        self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@", [VLCTime timeWithNumber:[mediaFile duration]], [NSByteCountFormatter stringFromByteCount:[mediaFile fileSizeInBytes] countStyle:NSByteCountFormatterCountStyleFile]];
247
248
249
250
251
252
253
254
255
    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];
        }
    }
256
257
258
259
260
261
    [self _showPositionOfItem:mediaFile];
}

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

Felix Paul Kühne's avatar
Felix Paul Kühne committed
263
    if (SYSTEM_RUNS_IOS7_OR_LATER) {
264
        CGFloat duration = mediaItem.duration.floatValue;
265
        if (position > .05f && position < .95f && (duration * position - duration) < -60000) {
266
            [(UITextView*)self.mediaIsUnreadView setText:[NSString stringWithFormat:NSLocalizedString(@"LIBRARY_MINUTES_LEFT", @""), [[VLCTime timeWithInt:(duration * position - duration)] minuteStringValue]]];
267
            self.mediaIsUnreadView.hidden = NO;
268
        } else if (mediaItem.unread.intValue) {
269
270
271
272
273
274
275
276
            [(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];
277
        self.mediaIsUnreadView.hidden = !mediaItem.unread.intValue;
278
    }
279
280
}

281
@end