VLCCloudStorageTableViewCell.m 9.28 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * VLCCloudStorageTableViewCell.m
 * VLC for iOS
 *****************************************************************************
Felix Paul Kühne's avatar
Felix Paul Kühne committed
5
 * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
6 7 8 9 10 11 12
 * $Id$
 *
 * Authors: Carola Nitz <nitz.carola # googlemail.com>
 *          Felix Paul Kühne <fkuehne # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/
13 14

#import "VLCCloudStorageTableViewCell.h"
15 16 17 18 19
@interface VLCCloudStorageTableViewCell ()
{
    NSURL *_iconURL;
}
@end
20 21 22 23 24 25 26 27 28 29 30 31 32

@implementation VLCCloudStorageTableViewCell

+ (VLCCloudStorageTableViewCell *)cellWithReuseIdentifier:(NSString *)ident
{
    NSArray *nibContentArray = [[NSBundle mainBundle] loadNibNamed:@"VLCCloudStorageTableViewCell" owner:nil options:nil];
    NSAssert([nibContentArray count] == 1, @"meh");
    NSAssert([[nibContentArray lastObject] isKindOfClass:[VLCCloudStorageTableViewCell class]], @"meh meh");
    VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[nibContentArray lastObject];

    return cell;
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
33
- (void)setDropboxFile:(DBMetadata *)dropboxFile
34
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
35 36
    if (dropboxFile != _dropboxFile)
        _dropboxFile = dropboxFile;
37 38 39 40 41 42 43 44 45 46 47 48

    [self _updatedDisplayedInformation];
}

- (void)setDriveFile:(GTLDriveFile *)driveFile
{
    if (driveFile != _driveFile)
        _driveFile = driveFile;

    [self _updatedDisplayedInformation];
}

Carola Nitz's avatar
Carola Nitz committed
49 50 51 52 53 54 55 56
- (void)setBoxFile:(BoxItem *)boxFile
{
    if (boxFile != _boxFile)
        _boxFile = boxFile;

    [self _updatedDisplayedInformation];
}

57 58 59 60 61 62 63 64
- (void)setOneDriveFile:(VLCOneDriveObject *)oneDriveFile
{
    if (oneDriveFile != _oneDriveFile)
        _oneDriveFile = oneDriveFile;

    [self _updatedDisplayedInformation];
}

65 66
- (void)_updatedDisplayedInformation
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
67 68 69
    if (_dropboxFile != nil) {
        if (self.dropboxFile.isDirectory) {
            self.folderTitleLabel.text = self.dropboxFile.filename;
70 71
            self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
            self.folderTitleLabel.hidden = NO;
72
        } else {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
73 74
            self.titleLabel.text = self.dropboxFile.filename;
            self.subtitleLabel.text = (self.dropboxFile.totalBytes > 0) ? self.dropboxFile.humanReadableSize : @"";
75 76
            self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
            self.folderTitleLabel.hidden = YES;
77 78
        }

Felix Paul Kühne's avatar
Felix Paul Kühne committed
79
        NSString *iconName = self.dropboxFile.icon;
80 81 82 83 84 85 86
        if ([iconName isEqualToString:@"folder_user"] || [iconName isEqualToString:@"folder"] || [iconName isEqualToString:@"folder_public"] || [iconName isEqualToString:@"folder_photos"] || [iconName isEqualToString:@"package"]) {
            self.thumbnailView.image = [UIImage imageNamed:@"folder"];
            self.downloadButton.hidden = YES;
        } else if ([iconName isEqualToString:@"page_white"] || [iconName isEqualToString:@"page_white_text"])
            self.thumbnailView.image = [UIImage imageNamed:@"blank"];
        else if ([iconName isEqualToString:@"page_white_film"])
            self.thumbnailView.image = [UIImage imageNamed:@"movie"];
87 88
        else if ([iconName isEqualToString:@"page_white_sound"])
            self.thumbnailView.image = [UIImage imageNamed:@"audio"];
Carola Nitz's avatar
Carola Nitz committed
89 90
        else {
            self.thumbnailView.image = [UIImage imageNamed:@"blank"];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
91
            APLog(@"missing icon for type '%@'", self.dropboxFile.icon);
Carola Nitz's avatar
Carola Nitz committed
92
        }
93 94 95 96 97

    } else if(_driveFile != nil){
        BOOL isDirectory = [self.driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"];
        if (isDirectory) {
            self.folderTitleLabel.text = self.driveFile.title;
98 99
            self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
            self.folderTitleLabel.hidden = NO;
100 101 102
        } else {
            self.titleLabel.text = self.driveFile.title;
            self.subtitleLabel.text = (self.driveFile.fileSize > 0) ? [NSByteCountFormatter stringFromByteCount:[self.driveFile.fileSize longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
103 104
            self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
            self.folderTitleLabel.hidden = YES;
105
        }
106 107 108 109
        if (_driveFile.thumbnailLink != nil) {
            _iconURL = [NSURL URLWithString:_driveFile.thumbnailLink];
            [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
        }
110
        NSString *iconName = self.driveFile.iconLink;
111
        if (isDirectory) {
112 113
            self.thumbnailView.image = [UIImage imageNamed:@"folder"];
        } else if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_10_audio_list.png"]) {
114
            self.thumbnailView.image = [UIImage imageNamed:@"audio"];
115 116 117 118 119 120
        } else if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_11_video_list.png"]) {
            self.thumbnailView.image = [UIImage imageNamed:@"movie"];
        } else {
            self.thumbnailView.image = [UIImage imageNamed:@"blank"];
            APLog(@"missing icon for type '%@'", self.driveFile.iconLink);
        }
Carola Nitz's avatar
Carola Nitz committed
121 122 123 124 125 126 127 128 129 130 131
    } else if(_boxFile != nil) {
        BOOL isDirectory = [self.boxFile.type isEqualToString:@"folder"];
        if (isDirectory) {
            self.folderTitleLabel.text = self.boxFile.name;
            self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
            self.folderTitleLabel.hidden = NO;
        } else {
            self.titleLabel.text = self.boxFile.name;
            self.subtitleLabel.text = (self.boxFile.size > 0) ? [NSByteCountFormatter stringFromByteCount:[self.boxFile.size longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
            self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
            self.folderTitleLabel.hidden = YES;
132
            self.downloadButton.hidden = NO;
Carola Nitz's avatar
Carola Nitz committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        }
        //TODO: correct thumbnails
//        if (_boxFile.modelID != nil) {
//            //this request needs a token in the header to work
//            NSString *thumbnailURLString = [NSString stringWithFormat:@"https://api.box.com/2.0/files/%@/thumbnail.png?min_height=32&min_width=32&max_height=64&max_width=64", _boxFile.modelID];
//            _iconURL = [NSURL URLWithString:thumbnailURLString];
//            [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
//        }
        //TODO:correct icons
        if (isDirectory) {
            self.thumbnailView.image = [UIImage imageNamed:@"folder"];
        } else {
            self.thumbnailView.image = [UIImage imageNamed:@"blank"];
            APLog(@"missing icon for type '%@'", self.boxFile);
        }
148 149
    } else if(_oneDriveFile != nil) {
        if (_oneDriveFile.isFolder) {
150
            self.downloadButton.hidden = YES;
151 152 153 154 155
            self.folderTitleLabel.text = self.oneDriveFile.name;
            self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
            self.folderTitleLabel.hidden = NO;
            self.thumbnailView.image = [UIImage imageNamed:@"folder"];
        } else {
156
            self.downloadButton.hidden = NO;
157
            self.titleLabel.text = self.oneDriveFile.name;
158 159 160 161 162 163 164 165 166 167 168 169
            NSMutableString *subtitle = [[NSMutableString alloc] init];
            if (self.oneDriveFile.size > 0) {
                [subtitle appendString:[NSByteCountFormatter stringFromByteCount:[self.oneDriveFile.size longLongValue] countStyle:NSByteCountFormatterCountStyleFile]];
                if (self.oneDriveFile.duration > 0) {
                    VLCTime *time = [VLCTime timeWithNumber:self.oneDriveFile.duration];
                    [subtitle appendFormat:@" — %@", [time verboseStringValue]];
                }
            } else if (self.oneDriveFile.duration > 0) {
                VLCTime *time = [VLCTime timeWithNumber:self.oneDriveFile.duration];
                [subtitle appendString:[time verboseStringValue]];
            }
            self.subtitleLabel.text = subtitle;
170 171
            self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
            self.folderTitleLabel.hidden = YES;
172 173
            if (self.oneDriveFile.isAudio)
                self.thumbnailView.image = [UIImage imageNamed:@"audio"];
174
            else if (self.oneDriveFile.isVideo) {
175
                self.thumbnailView.image = [UIImage imageNamed:@"movie"];
176 177 178 179 180
                if (_oneDriveFile.thumbnailURL != nil) {
                    _iconURL = [NSURL URLWithString:_oneDriveFile.thumbnailURL];
                    [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
                }
            } else
181
                self.thumbnailView.image = [UIImage imageNamed:@"blank"];
182
        }
183
    }
184

185 186 187
    [self setNeedsDisplay];
}

188 189
- (void)_updateIconFromURL
{
190 191
    NSData *imageData = [[NSData alloc] initWithContentsOfURL:_iconURL];
    UIImage *image = [[UIImage alloc] initWithData:imageData];
Carola Nitz's avatar
Carola Nitz committed
192
    if (image != nil) {
193
        self.thumbnailView.contentMode = UIViewContentModeScaleAspectFit;
Carola Nitz's avatar
Carola Nitz committed
194 195
        self.thumbnailView.image = image;
    }
196 197
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211
- (IBAction)triggerDownload:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(triggerDownloadForCell:)])
        [self.delegate triggerDownloadForCell:self];
}

+ (CGFloat)heightOfCell
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
        return 80.;

    return 48.;
}

212 213 214 215 216
- (void)setIsDownloadable:(BOOL)isDownloadable
{
    self.downloadButton.hidden = !isDownloadable;
}

217
@end