VLCPlaylistTableViewCell.m 16.1 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 24 25 26
@interface VLCPlaylistTableViewCell ()
{
    UILongPressGestureRecognizer *_longPress;
}

@end

27 28 29 30
@implementation VLCPlaylistTableViewCell

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

    return cell;
}

44 45
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
46
    [self _updatedDisplayedInformationForKeyPath:keyPath];
47 48
}

49 50 51
- (void)setMediaObject:(MLFile *)mediaObject
{
    if (_mediaObject != mediaObject) {
52 53 54 55 56
        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"];
57 58 59 60 61 62 63 64 65
        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:[MLFile class]]) {
66 67 68 69 70 71 72 73 74 75 76
            [_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"];
77
            [(MLFile*)_mediaObject didHide];
78
        }
79 80

        _mediaObject = mediaObject;
81 82
        // prevent the cell from recycling the current snap for random contents
        self.thumbnailView.image = nil;
83

84 85 86 87 88
        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];
89 90 91 92 93 94 95 96 97
        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];
        } else if ([_mediaObject isKindOfClass:[MLFile class]]) {
98 99 100 101 102 103 104 105 106 107 108
            [_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];
109
            [(MLFile*)_mediaObject willDisplay];
110
        }
111 112
    }

113
    [self _updatedDisplayedInformationForKeyPath:nil];
114 115
}

116 117 118
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
119
    [self _updatedDisplayedInformationForKeyPath:@"editing"];
120

121 122 123 124 125 126 127 128 129
    if (SYSTEM_RUNS_IOS7_OR_LATER) {
        if (editing) {
            if (_longPress)
                [self removeGestureRecognizer:_longPress];
        } else {
            if (!_longPress)
                _longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTouchGestureAction:)];
            [self addGestureRecognizer:_longPress];
        }
130
    }
131 132
}

133
- (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
134
{
135
    self.thumbnailView.contentMode = UIViewContentModeScaleAspectFill;
Carola's avatar
Carola committed
136

137 138
    if ([self.mediaObject isKindOfClass:[MLFile class]]) {
        MLFile *mediaObject = (MLFile*)self.mediaObject;
139
        [self _configureForMLFile:mediaObject];
140

141
        if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])))
142
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:mediaObject];
143
    } else if ([self.mediaObject isKindOfClass:[MLLabel class]]) {
Carola's avatar
Carola committed
144 145 146
        MLLabel *mediaObject = (MLLabel *)self.mediaObject;
        [self _configureForFolder:mediaObject];

147
        if ([keyPath isEqualToString:@"files"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
148 149
            if (mediaObject.files.count == 0) {
                self.thumbnailView.contentMode = UIViewContentModeScaleAspectFit;
Carola's avatar
Carola committed
150
                self.thumbnailView.image = [UIImage imageNamed:@"folderIcon"];
151
            } else
152
                self.thumbnailView.image = [VLCThumbnailsCache thumbnailForLabel:mediaObject];
Carola's avatar
Carola committed
153
        }
154 155
    } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
        MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;
156
        [self _configureForAlbum:mediaObject];
157

158 159 160 161
        if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
            MLFile *anyFileFromAnyTrack = [mediaObject.tracks.anyObject files].anyObject;
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyTrack];
        }
162 163
    } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
        MLAlbumTrack *mediaObject = (MLAlbumTrack *)self.mediaObject;
164
        [self _configureForAlbumTrack:mediaObject];
165

166
        if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || !self.thumbnailView.image) {
167 168 169
            MLFile *anyFileFromTrack = mediaObject.files.anyObject;
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromTrack];
        }
170 171
    } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
        MLShow *mediaObject = (MLShow *)self.mediaObject;
172
        [self _configureForShow:mediaObject];
173

174
        if ([keyPath isEqualToString:@"computedThumbnail"] || [keyPath isEqualToString:@"episodes"] || !keyPath || !self.thumbnailView.image) {
175
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForShow:mediaObject];
176
        }
177 178
    } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
        MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
179
        [self _configureForShowEpisode:mediaObject];
180

181
        if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || !self.thumbnailView.image) {
182
            MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
183
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
184
        }
185
    }
186

187 188 189
    [self setNeedsDisplay];
}

190 191
+ (CGFloat)heightOfCell
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
192
    if (SYSTEM_RUNS_IOS7_OR_LATER)
193
        return 90.;
194

195 196 197
    return 80.;
}

198 199
#pragma mark - presentation

200
- (void)_configureForShow:(MLShow *)show
201 202 203
{
    self.titleLabel.text = show.name;
    NSUInteger count = show.episodes.count;
204 205 206 207
    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]];
208 209
    self.mediaIsUnreadView.hidden = YES;
    self.progressIndicator.hidden = YES;
210 211
    self.folderIconView.image = [UIImage imageNamed:@"PlayingExternally"];
    self.folderIconView.hidden = NO;
212 213
}

214
- (void)_configureForAlbumTrack:(MLAlbumTrack *)albumTrack
215
{
216
    MLFile *anyFileFromTrack = albumTrack.files.anyObject;
217
    self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@ — %@", albumTrack.artist, [NSString stringWithFormat:NSLocalizedString(@"LIBRARY_TRACK_N", @""), albumTrack.trackNumber.intValue], [VLCTime timeWithNumber:[anyFileFromTrack duration]]];
218
    self.titleLabel.text = albumTrack.title;
219
    [self _showPositionOfItem:anyFileFromTrack];
220
    self.folderIconView.hidden = YES;
221 222
}

223
- (void)_configureForShowEpisode:(MLShowEpisode *)showEpisode
224 225 226 227 228
{
    self.titleLabel.text = showEpisode.name;

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

234
    [self _showPositionOfItem:anyFileFromEpisode];
235
    self.folderIconView.hidden = YES;
236 237
}

238
- (void)_configureForAlbum:(MLAlbum *)album
239 240 241 242
{
    self.titleLabel.text = album.name;
    MLAlbumTrack *anyTrack = [album.tracks anyObject];
    NSUInteger count = album.tracks.count;
243 244
    NSMutableString *string = [[NSMutableString alloc] init];
    if (anyTrack) {
245 246 247 248
        if (anyTrack.artist.length > 0) {
            [string appendString:anyTrack.artist];
            [string appendString:@" — "];
        }
249
    }
Felix Paul Kühne's avatar
Felix Paul Kühne committed
250
    [string appendString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count]];
251
    if (album.releaseYear.length > 0)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
252
        [string appendFormat:@" — %@", album.releaseYear];
253
    self.subtitleLabel.text = string;
254 255
    self.mediaIsUnreadView.hidden = YES;
    self.progressIndicator.hidden = YES;
256
    self.folderIconView.hidden = YES;
257 258
}

Carola's avatar
Carola committed
259 260 261 262
- (void)_configureForFolder:(MLLabel *)label
{
    self.titleLabel.text = label.name;
    NSUInteger count = label.files.count;
263
    self.subtitleLabel.text = [NSString stringWithFormat:(count == 1) ? NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @"") : NSLocalizedString(@"LIBRARY_TRACKS", @""), count];
Carola's avatar
Carola committed
264 265
    self.mediaIsUnreadView.hidden = YES;
    self.progressIndicator.hidden = YES;
266 267
    self.folderIconView.image = [UIImage imageNamed:@"folderIcon"];
    self.folderIconView.hidden = NO;
Carola's avatar
Carola committed
268 269
}

270
- (void)_configureForMLFile:(MLFile *)mediaFile
271
{
272
    if (mediaFile.isAlbumTrack) {
273 274 275 276 277 278
        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];
279 280 281 282
    } else
        self.titleLabel.text = mediaFile.title;

    if (self.isEditing)
283
        self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@", [VLCTime timeWithNumber:[mediaFile duration]], [NSByteCountFormatter stringFromByteCount:[mediaFile fileSizeInBytes] countStyle:NSByteCountFormatterCountStyleFile]];
284 285 286 287 288 289 290 291 292
    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];
        }
    }
293
    [self _showPositionOfItem:mediaFile];
294
    self.folderIconView.hidden = YES;
295 296 297 298 299
}

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

Felix Paul Kühne's avatar
Felix Paul Kühne committed
301
    if (SYSTEM_RUNS_IOS7_OR_LATER) {
302
        CGFloat duration = mediaItem.duration.floatValue;
303
        if (position > .05f && position < .95f && (duration * position - duration) < -60000) {
304
            [(UITextView*)self.mediaIsUnreadView setText:[NSString stringWithFormat:NSLocalizedString(@"LIBRARY_MINUTES_LEFT", @""), [[VLCTime timeWithInt:(duration * position - duration)] minuteStringValue]]];
305
            self.mediaIsUnreadView.hidden = NO;
306
        } else if (mediaItem.unread.intValue) {
307 308 309 310 311 312 313 314
            [(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];
315
        self.mediaIsUnreadView.hidden = !mediaItem.unread.intValue;
316
    }
317 318
}

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
- (void)longTouchGestureAction:(UIGestureRecognizer *)recognizer
{
    CGRect frame = self.frame;
    if (frame.size.height > 90.)
        frame.size.height = 90.;
    else if (recognizer.state == UIGestureRecognizerStateBegan)
        frame.size.height = 180;

    void (^animationBlock)() = ^() {
        self.frame = frame;
    };

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

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

339
@end