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

#import "VLCPlaylistCollectionViewCell.h"
#import "VLCPlaylistViewController.h"
18
#import "VLCThumbnailsCache.h"
19

20 21 22 23 24 25 26 27
@interface VLCPlaylistCollectionViewCell ()
{
    UIImage *_checkboxEmptyImage;
    UIImage *_checkboxImage;
}

@end

28 29
@implementation VLCPlaylistCollectionViewCell

30 31 32 33 34 35 36 37 38 39 40
- (void)awakeFromNib
{
    if (SYSTEM_RUNS_IOS7_OR_LATER) {
        _checkboxEmptyImage = [UIImage imageNamed:@"checkboxEmpty"];
        _checkboxImage = [UIImage imageNamed:@"checkbox"];
    } else {
        _checkboxEmptyImage = [UIImage imageNamed:@"checkbox-legacy-empty"];
        _checkboxImage = [UIImage imageNamed:@"checkbox-legacy"];
    }
}

41 42
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
43
    self.isSelectedView.hidden = !editing;
44 45

    if (!([_mediaObject isKindOfClass:[MLFile class]] && [_mediaObject.labels count] > 0))
Carola's avatar
Carola committed
46
        [self shake:editing];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
47
    [self selectionUpdate];
48 49 50
    [self _updatedDisplayedInformationForKeyPath:@"editing"];
}

51 52 53
- (void)selectionUpdate
{
    if (self.selected)
54
        self.isSelectedView.image = _checkboxImage;
55
    else
56
        self.isSelectedView.image = _checkboxEmptyImage;
57 58
}

Carola's avatar
Carola committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
- (void)shake:(BOOL)shake
{
    if (shake) {
        [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"];
    }
}

85 86 87 88 89 90 91 92
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    [self _updatedDisplayedInformationForKeyPath:keyPath];
}

- (void)setMediaObject:(MLFile *)mediaObject
{
    if (_mediaObject != mediaObject) {
93 94 95
        if ([_mediaObject isKindOfClass:[MLLabel class]]) {
            [_mediaObject removeObserver:self forKeyPath:@"files"];
            [_mediaObject removeObserver:self forKeyPath:@"name"];
96
        } else if ([_mediaObject isKindOfClass:[MLShow class]])
97
            [_mediaObject removeObserver:self forKeyPath:@"episodes"];
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        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]]) {
            [_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"];
            [(MLFile*)_mediaObject didHide];
        }
121 122

        _mediaObject = mediaObject;
123 124
        // prevent the cell from recycling the current snap for random contents
        self.thumbnailView.image = nil;
125

126 127 128
        if ([_mediaObject isKindOfClass:[MLLabel class]]) {
            [_mediaObject addObserver:self forKeyPath:@"files" options:0 context:nil];
            [_mediaObject addObserver:self forKeyPath:@"name" options:0 context:nil];
129
        } else if ([_mediaObject isKindOfClass:[MLShow class]])
130
            [_mediaObject addObserver:self forKeyPath:@"episodes" options:0 context:nil];
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        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]]) {
            [_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];
            [(MLFile*)_mediaObject willDisplay];
        }
154 155 156 157 158 159 160
    }

    [self _updatedDisplayedInformationForKeyPath:nil];
}

- (void)_updatedDisplayedInformationForKeyPath:(NSString *)keyPath
{
161
    self.thumbnailView.contentMode = UIViewContentModeScaleAspectFill;
162 163
    if ([self.mediaObject isKindOfClass:[MLFile class]]) {
        MLFile *mediaObject = self.mediaObject;
164
        [self _configureForMLFile:mediaObject];
165

166
        if (([keyPath isEqualToString:@"computedThumbnail"] || !keyPath) || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"]))
167
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:mediaObject];
Carola's avatar
Carola committed
168 169 170 171
    } else if ([self.mediaObject isKindOfClass:[MLLabel class]]) {
        MLLabel *mediaObject = (MLLabel *)self.mediaObject;
        [self _configureForFolder:mediaObject];

172
        if ([keyPath isEqualToString:@"files"] || [keyPath isEqualToString:@"labels"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
173 174
            if (mediaObject.files.count == 0) {
                self.thumbnailView.contentMode = UIViewContentModeScaleAspectFit;
Carola's avatar
Carola committed
175
                self.thumbnailView.image = [UIImage imageNamed:@"folderIcon"];
176
            } else
177
                self.thumbnailView.image = [VLCThumbnailsCache thumbnailForLabel:mediaObject];
Carola's avatar
Carola committed
178
        }
179 180
    } else if ([self.mediaObject isKindOfClass:[MLAlbum class]]) {
        MLAlbum *mediaObject = (MLAlbum *)self.mediaObject;
181
        [self _configureForAlbum:mediaObject];
182

183 184 185 186
        if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
            MLFile *anyFileFromAnyTrack = [mediaObject.tracks.anyObject files].anyObject;
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromAnyTrack];
        }
187 188
    } else if ([self.mediaObject isKindOfClass:[MLAlbumTrack class]]) {
        MLAlbumTrack *mediaObject = (MLAlbumTrack *)self.mediaObject;
189
        [self _configureForAlbumTrack:mediaObject];
190

191
        if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || !self.thumbnailView.image) {
192 193 194
            MLFile *anyFileFromTrack = mediaObject.files.anyObject;
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromTrack];
        }
195 196
    } else if ([self.mediaObject isKindOfClass:[MLShow class]]) {
        MLShow *mediaObject = (MLShow *)self.mediaObject;
197
        [self _configureForShow:mediaObject];
198

199 200
        if ([keyPath isEqualToString:@"computedThumbnail"] || [keyPath isEqualToString:@"episodes"] || !keyPath || (!self.thumbnailView.image && [keyPath isEqualToString:@"editing"])) {
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForShow:mediaObject];
201 202 203
        }
    } else if ([self.mediaObject isKindOfClass:[MLShowEpisode class]]) {
        MLShowEpisode *mediaObject = (MLShowEpisode *)self.mediaObject;
204
        [self _configureForShowEpisode:mediaObject];
205

206
        if ([keyPath isEqualToString:@"computedThumbnail"] || !keyPath || !self.thumbnailView.image) {
207
            MLFile *anyFileFromEpisode = mediaObject.files.anyObject;
208
            self.thumbnailView.image = [VLCThumbnailsCache thumbnailForMediaFile:anyFileFromEpisode];
209 210 211 212 213 214 215 216
        }
    }

    [self setNeedsDisplay];
}

#pragma mark - presentation

217
- (void)_configureForShow:(MLShow *)show
218 219 220
{
    self.titleLabel.text = show.name;
    NSUInteger count = show.episodes.count;
221 222 223 224
    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]];
225 226
    self.mediaIsUnreadView.hidden = YES;
    self.progressView.hidden = YES;
227 228
    self.folderIconView.image = [UIImage imageNamed:@"PlayingExternally"];
    self.folderIconView.hidden = NO;
229 230
}

231
- (void)_configureForAlbumTrack:(MLAlbumTrack *)albumTrack
232 233 234
{
    MLFile *anyFileFromTrack = albumTrack.files.anyObject;

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

238
    [self _showPositionOfItem:anyFileFromTrack];
239
    self.folderIconView.hidden = YES;
240 241
}

242
- (void)_configureForShowEpisode:(MLShowEpisode *)showEpisode
243 244
{
    self.titleLabel.text = showEpisode.name;
245 246

    MLFile *anyFileFromEpisode = showEpisode.files.anyObject;
247
    if (self.titleLabel.text.length < 1) {
248
        self.titleLabel.text = [NSString stringWithFormat:@"S%02dE%02d", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue];
249 250
        self.subtitleLabel.text = [NSString stringWithFormat:@"%@", [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
    } else
251
        self.subtitleLabel.text = [NSString stringWithFormat:@"S%02dE%02d — %@", showEpisode.seasonNumber.intValue, showEpisode.episodeNumber.intValue, [VLCTime timeWithNumber:[anyFileFromEpisode duration]]];
252
    [self _showPositionOfItem:anyFileFromEpisode];
253
    self.folderIconView.hidden = YES;
254 255
}

256 257 258 259 260
- (void)_configureForAlbum:(MLAlbum *)album
{
    self.titleLabel.text = album.name;
    MLAlbumTrack *anyTrack = [album.tracks anyObject];
    NSUInteger count = album.tracks.count;
261 262
    NSMutableString *string = [[NSMutableString alloc] init];
    if (anyTrack) {
263 264 265 266
        if (anyTrack.artist.length > 0) {
            [string appendString:anyTrack.artist];
            [string appendString:@" — "];
        }
267
    }
Felix Paul Kühne's avatar
Felix Paul Kühne committed
268
    [string appendString:[NSString stringWithFormat:(count > 1) ? NSLocalizedString(@"LIBRARY_TRACKS", @"") : NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @""), count]];
269
    if (album.releaseYear.length > 0)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
270
        [string appendFormat:@" — %@", album.releaseYear];
271
    self.subtitleLabel.text = string;
272 273
    self.mediaIsUnreadView.hidden = YES;
    self.progressView.hidden = YES;
274
    self.folderIconView.hidden = YES;
275 276
}

Carola's avatar
Carola committed
277 278 279 280
- (void)_configureForFolder:(MLLabel *)label
{
    self.titleLabel.text = label.name;
    NSUInteger count = label.files.count;
281
    self.subtitleLabel.text = [NSString stringWithFormat:(count == 1) ? NSLocalizedString(@"LIBRARY_SINGLE_TRACK", @"") : NSLocalizedString(@"LIBRARY_TRACKS", @""), count];
Carola's avatar
Carola committed
282 283
    self.mediaIsUnreadView.hidden = YES;
    self.progressView.hidden = YES;
284 285
    self.folderIconView.image = [UIImage imageNamed:@"folderIcon"];
    self.folderIconView.hidden = NO;
Carola's avatar
Carola committed
286 287
}

288
- (void)_configureForMLFile:(MLFile *)mediaFile
289
{
290
    if (mediaFile.isAlbumTrack) {
291 292 293 294 295 296
        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];
297 298 299 300 301 302
    } else
        self.titleLabel.text = mediaFile.title;

    VLCPlaylistViewController *delegate = (VLCPlaylistViewController*)self.collectionView.delegate;

    if (delegate.isEditing)
303
        self.subtitleLabel.text = [NSString stringWithFormat:@"%@ — %@", [VLCTime timeWithNumber:[mediaFile duration]], [NSByteCountFormatter stringFromByteCount:[mediaFile fileSizeInBytes] countStyle:NSByteCountFormatterCountStyleFile]];
304 305 306 307 308 309 310 311 312
    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];
        }
    }
313
    [self _showPositionOfItem:mediaFile];
314
    self.folderIconView.hidden = YES;
315 316 317 318 319
}

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

    if (SYSTEM_RUNS_IOS7_OR_LATER) {
        CGFloat duration = mediaItem.duration.floatValue;
323
        if (position > .05f && position < .95f && (duration * position - duration) < -60000) {
324 325 326 327 328 329 330 331 332 333 334 335 336
            [(UITextView*)self.mediaIsUnreadView setText:[NSString stringWithFormat:NSLocalizedString(@"LIBRARY_MINUTES_LEFT", @""), [[VLCTime timeWithInt:(duration * position - duration)] minuteStringValue]]];
            self.mediaIsUnreadView.hidden = NO;
        } else if (mediaItem.unread.intValue) {
            [(UILabel *)self.mediaIsUnreadView setText:[NSLocalizedString(@"NEW", @"") capitalizedStringWithLocale:[NSLocale currentLocale]]];
            self.mediaIsUnreadView.hidden = NO;
        } else
            self.mediaIsUnreadView.hidden = YES;
    } else {
        self.progressView.progress = position;
        self.progressView.hidden = ((position < .1f) || (position > .95f)) ? YES : NO;
        [self.progressView setNeedsDisplay];
        self.mediaIsUnreadView.hidden = !mediaItem.unread.intValue;
    }
337 338 339
}

@end