VLCCloudStorageTableViewCell.m 9.96 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
    [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
                           withObject:nil waitUntilDone:NO];
40
41
42
43
44
45
46
}

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

47
48
    [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
                           withObject:nil waitUntilDone:NO];
49
50
}

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

56
57
    [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
                           withObject:nil waitUntilDone:NO];
Carola's avatar
Carola committed
58
59
}

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

65
66
    [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
                           withObject:nil waitUntilDone:NO];
67
68
}

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

Felix Paul Kühne's avatar
Felix Paul Kühne committed
83
        NSString *iconName = self.dropboxFile.icon;
84
85
86
87
88
89
90
        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"];
91
92
        else if ([iconName isEqualToString:@"page_white_sound"])
            self.thumbnailView.image = [UIImage imageNamed:@"audio"];
Carola's avatar
Carola committed
93
94
        else {
            self.thumbnailView.image = [UIImage imageNamed:@"blank"];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
95
            APLog(@"missing icon for type '%@'", self.dropboxFile.icon);
Carola's avatar
Carola committed
96
        }
97
98
99
100
101

    } else if(_driveFile != nil){
        BOOL isDirectory = [self.driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"];
        if (isDirectory) {
            self.folderTitleLabel.text = self.driveFile.title;
102
103
            self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
            self.folderTitleLabel.hidden = NO;
104
105
106
        } else {
            self.titleLabel.text = self.driveFile.title;
            self.subtitleLabel.text = (self.driveFile.fileSize > 0) ? [NSByteCountFormatter stringFromByteCount:[self.driveFile.fileSize longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
107
108
            self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
            self.folderTitleLabel.hidden = YES;
109
        }
110
111
112
113
        if (_driveFile.thumbnailLink != nil) {
            _iconURL = [NSURL URLWithString:_driveFile.thumbnailLink];
            [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
        }
114
        NSString *iconName = self.driveFile.iconLink;
115
        if (isDirectory) {
116
117
            self.thumbnailView.image = [UIImage imageNamed:@"folder"];
        } else if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_10_audio_list.png"]) {
118
            self.thumbnailView.image = [UIImage imageNamed:@"audio"];
119
120
121
122
123
124
        } 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's avatar
Carola committed
125
126
127
128
129
130
131
132
133
134
135
    } 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;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
136
            self.downloadButton.hidden = NO;
Carola's avatar
Carola committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
        }
        //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);
        }
152
153
    } else if(_oneDriveFile != nil) {
        if (_oneDriveFile.isFolder) {
154
            self.downloadButton.hidden = YES;
155
156
157
158
159
            self.folderTitleLabel.text = self.oneDriveFile.name;
            self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
            self.folderTitleLabel.hidden = NO;
            self.thumbnailView.image = [UIImage imageNamed:@"folder"];
        } else {
160
            self.downloadButton.hidden = NO;
161
            self.titleLabel.text = self.oneDriveFile.name;
162
163
164
165
166
167
168
169
170
171
172
173
            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;
174
175
            self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
            self.folderTitleLabel.hidden = YES;
176
177
            if (self.oneDriveFile.isAudio)
                self.thumbnailView.image = [UIImage imageNamed:@"audio"];
178
            else if (self.oneDriveFile.isVideo) {
179
                self.thumbnailView.image = [UIImage imageNamed:@"movie"];
180
                NSString *thumbnailURL = _oneDriveFile.thumbnailURL;
181
                if ([thumbnailURL isKindOfClass:[NSString class]]) {
182
183
                    if (thumbnailURL.length > 0) {
                        _iconURL = [NSURL URLWithString:thumbnailURL];
184
185
                        [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
                    }
186
187
                }
            } else
188
                self.thumbnailView.image = [UIImage imageNamed:@"blank"];
189
        }
190
    }
191

192
193
194
    [self setNeedsDisplay];
}

195
196
- (void)_updateIconFromURL
{
197
    NSData *imageData = [[NSData alloc] initWithContentsOfURL:_iconURL];
198
199
200
    UIImage *icon = [[UIImage alloc] initWithData:imageData];
    if (icon != nil) {
        [self performSelectorOnMainThread:@selector(_updateIconOnMainThread:) withObject:icon waitUntilDone:NO];
Carola's avatar
Carola committed
201
    }
202
203
}

204
205
206
207
208
209
- (void)_updateIconOnMainThread:(UIImage *)icon
{
    self.thumbnailView.contentMode = UIViewContentModeScaleAspectFit;
    self.thumbnailView.image = icon;
}

210
211
212
213
214
215
216
217
218
219
220
221
222
223
- (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.;
}

224
225
226
227
228
- (void)setIsDownloadable:(BOOL)isDownloadable
{
    self.downloadButton.hidden = !isDownloadable;
}

229
@end