forked from videolan/vlc-ios
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVLCCloudStorageTableViewCell.m
252 lines (223 loc) · 9.75 KB
/
VLCCloudStorageTableViewCell.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*****************************************************************************
* VLCCloudStorageTableViewCell.m
* VLC for iOS
*****************************************************************************
* Copyright (c) 2013-2019 VideoLAN. All rights reserved.
* $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.
*****************************************************************************/
#import "VLCCloudStorageTableViewCell.h"
#import "VLCNetworkImageView.h"
@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];
cell.titleLabel.hidden = YES;
cell.subtitleLabel.hidden = YES;
cell.folderTitleLabel.hidden = YES;
return cell;
}
- (void)setDropboxFile:(DBFILESMetadata *)dropboxFile
{
if (dropboxFile != _dropboxFile)
_dropboxFile = dropboxFile;
[self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
withObject:nil waitUntilDone:NO];
}
#if TARGET_OS_IOS
- (void)setDriveFile:(GTLRDrive_File *)driveFile
{
if (driveFile != _driveFile)
_driveFile = driveFile;
[self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
withObject:nil waitUntilDone:NO];
}
#endif
- (void)setBoxFile:(BoxItem *)boxFile
{
if (boxFile != _boxFile)
_boxFile = boxFile;
[self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
withObject:nil waitUntilDone:NO];
}
- (void)updateOneDriveDisplayAsFolder
{
_downloadButton.hidden = YES;
_folderTitleLabel.text = _oneDriveFile.name;
_titleLabel.hidden = _subtitleLabel.hidden = YES;
_folderTitleLabel.hidden = NO;
_thumbnailView.image = [UIImage imageNamed:@"folder"];
}
- (void)loadThumbnail
{
// The onedrive Api has no way to cancel a request and the ODThumbnail has no back reference to it's item
// so this might lead to wrong thumbnails if the cell is reused since we have no way of cancelling requests or check if the completion is still for the set item
ODDriveRequestBuilder *drive = [[ODClient loadCurrentClient] drive];
ODThumbnailRequest *request = [[[[drive items:_oneDriveFile.id] thumbnails:@"0"] medium] request];
__weak typeof(self) weakSelf = self;
[request getWithCompletion:^(ODThumbnail *response, NSError *error) {
if (error == nil && response.url) {// we don't care about errors for thumbnails
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.thumbnailView setImageWithURL:[NSURL URLWithString:response.url]];
});
}
}];
}
- (void)updateOneDriveDisplayAsItem
{
int64_t duration = 0;
NSString *title = self.oneDriveFile.name;
NSMutableString *subtitle = [[NSMutableString alloc] init];
_downloadButton.hidden = NO;
_titleLabel.text = title;
if (_oneDriveFile.audio) {
_thumbnailView.image = [UIImage imageNamed:@"audio"];
duration = _oneDriveFile.audio.duration;
[self loadThumbnail];
} else if (_oneDriveFile.video) {
_thumbnailView.image = [UIImage imageNamed:@"movie"];
duration = _oneDriveFile.video.duration;
[self loadThumbnail];
} else {
_thumbnailView.image = [UIImage imageNamed:@"blank"];
}
if (duration > 0) {
VLCTime *time = [VLCTime timeWithNumber:[NSNumber numberWithLong:duration]];
[subtitle appendString:[time verboseStringValue]];
}
if (_oneDriveFile.size > 0) {
subtitle = [NSMutableString stringWithString:[NSByteCountFormatter
stringFromByteCount:_oneDriveFile.size
countStyle:NSByteCountFormatterCountStyleFile]];
if (duration > 0) {
VLCTime *time = [VLCTime timeWithNumber:[NSNumber numberWithLong:duration]];
[subtitle appendFormat:@" — %@", [time verboseStringValue]];
}
}
_subtitleLabel.text = subtitle;
_titleLabel.hidden = _subtitleLabel.hidden = NO;
_folderTitleLabel.hidden = YES;
}
- (void)updateOneDriveDisplayedInformation
{
_oneDriveFile.folder ? [self updateOneDriveDisplayAsFolder] : [self updateOneDriveDisplayAsItem];
}
- (void)setOneDriveFile:(ODItem *)oneDriveFile
{
if (oneDriveFile != _oneDriveFile) {
_oneDriveFile = oneDriveFile;
[self updateOneDriveDisplayedInformation];
}
}
- (void)_updatedDisplayedInformation
{
if (_dropboxFile != nil) {
if ([_dropboxFile isKindOfClass:[DBFILESFolderMetadata class]]) {
self.folderTitleLabel.text = self.dropboxFile.name;
self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
self.folderTitleLabel.hidden = NO;
self.downloadButton.hidden = YES;
self.thumbnailView.image = [UIImage imageNamed:@"folder"];
} else if ([_dropboxFile isKindOfClass:[DBFILESFileMetadata class]]) {
DBFILESFileMetadata *file = (DBFILESFileMetadata *)_dropboxFile;
self.titleLabel.text = file.name;
self.subtitleLabel.text = (file.size.integerValue > 0) ? [NSByteCountFormatter stringFromByteCount:file.size.longLongValue countStyle:NSByteCountFormatterCountStyleFile] : @"";
self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
self.folderTitleLabel.hidden = YES;
self.downloadButton.hidden = NO;
self.thumbnailView.image = [UIImage imageNamed:@"blank"];
}
}
#if TARGET_OS_IOS
else if(_driveFile != nil){
BOOL isDirectory = [self.driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"];
if (isDirectory) {
self.folderTitleLabel.text = self.driveFile.name;
self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
self.folderTitleLabel.hidden = NO;
} else {
NSString *title = self.driveFile.name;
self.titleLabel.text = title;
self.subtitleLabel.text = (self.driveFile.size > 0) ? [NSByteCountFormatter stringFromByteCount:[self.driveFile.size longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
self.folderTitleLabel.hidden = YES;
if (_driveFile.thumbnailLink != nil) {
[self.thumbnailView setImageWithURL:[NSURL URLWithString:_driveFile.thumbnailLink]];
}
}
NSString *iconName = self.driveFile.iconLink;
if (isDirectory) {
self.thumbnailView.image = [UIImage imageNamed:@"folder"];
} else if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_10_audio_list.png"]) {
self.thumbnailView.image = [UIImage imageNamed:@"audio"];
} 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);
}
}
#endif
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 {
NSString *title = self.boxFile.name;
self.titleLabel.text = title;
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;
self.downloadButton.hidden = NO;
}
//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];
// [self.thumbnailView setImageWithURL:[NSURL URLWithString:thumbnailURLString]];
// }
//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);
}
} else if(_oneDriveFile != nil) {
[self updateOneDriveDisplayedInformation];
}
[self setNeedsDisplay];
}
- (IBAction)triggerDownload:(id)sender
{
if ([self.delegate respondsToSelector:@selector(triggerDownloadForCell:)])
[self.delegate triggerDownloadForCell:self];
}
+ (CGFloat)heightOfCell
{
#if TARGET_OS_IOS
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
return 80.;
return 48.;
#else
return 107.;
#endif
}
- (void)setIsDownloadable:(BOOL)isDownloadable
{
self.downloadButton.hidden = !isDownloadable;
}
- (void)prepareForReuse {
[super prepareForReuse];
_thumbnailView.image = nil;
}
@end