VLCCloudStorageTableViewCell.m 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*****************************************************************************
  2. * VLCCloudStorageTableViewCell.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <nitz.carola # googlemail.com>
  9. * Felix Paul Kühne <fkuehne # videolan.org>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCCloudStorageTableViewCell.h"
  14. @interface VLCCloudStorageTableViewCell ()
  15. {
  16. NSURL *_iconURL;
  17. }
  18. @end
  19. @implementation VLCCloudStorageTableViewCell
  20. + (VLCCloudStorageTableViewCell *)cellWithReuseIdentifier:(NSString *)ident
  21. {
  22. NSArray *nibContentArray = [[NSBundle mainBundle] loadNibNamed:@"VLCCloudStorageTableViewCell" owner:nil options:nil];
  23. NSAssert([nibContentArray count] == 1, @"meh");
  24. NSAssert([[nibContentArray lastObject] isKindOfClass:[VLCCloudStorageTableViewCell class]], @"meh meh");
  25. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[nibContentArray lastObject];
  26. return cell;
  27. }
  28. - (void)setDropboxFile:(DBMetadata *)dropboxFile
  29. {
  30. if (dropboxFile != _dropboxFile)
  31. _dropboxFile = dropboxFile;
  32. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  33. withObject:nil waitUntilDone:NO];
  34. }
  35. - (void)setDriveFile:(GTLDriveFile *)driveFile
  36. {
  37. if (driveFile != _driveFile)
  38. _driveFile = driveFile;
  39. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  40. withObject:nil waitUntilDone:NO];
  41. }
  42. - (void)setBoxFile:(BoxItem *)boxFile
  43. {
  44. if (boxFile != _boxFile)
  45. _boxFile = boxFile;
  46. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  47. withObject:nil waitUntilDone:NO];
  48. }
  49. - (void)setOneDriveFile:(VLCOneDriveObject *)oneDriveFile
  50. {
  51. if (oneDriveFile != _oneDriveFile)
  52. _oneDriveFile = oneDriveFile;
  53. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  54. withObject:nil waitUntilDone:NO];
  55. }
  56. - (void)_updatedDisplayedInformation
  57. {
  58. if (_dropboxFile != nil) {
  59. if (self.dropboxFile.isDirectory) {
  60. self.folderTitleLabel.text = self.dropboxFile.filename;
  61. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  62. self.folderTitleLabel.hidden = NO;
  63. } else {
  64. self.titleLabel.text = self.dropboxFile.filename;
  65. self.subtitleLabel.text = (self.dropboxFile.totalBytes > 0) ? self.dropboxFile.humanReadableSize : @"";
  66. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  67. self.folderTitleLabel.hidden = YES;
  68. }
  69. NSString *iconName = self.dropboxFile.icon;
  70. if ([iconName isEqualToString:@"folder_user"] || [iconName isEqualToString:@"folder"] || [iconName isEqualToString:@"folder_public"] || [iconName isEqualToString:@"folder_photos"] || [iconName isEqualToString:@"package"]) {
  71. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  72. self.downloadButton.hidden = YES;
  73. } else if ([iconName isEqualToString:@"page_white"] || [iconName isEqualToString:@"page_white_text"])
  74. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  75. else if ([iconName isEqualToString:@"page_white_film"])
  76. self.thumbnailView.image = [UIImage imageNamed:@"movie"];
  77. else if ([iconName isEqualToString:@"page_white_sound"])
  78. self.thumbnailView.image = [UIImage imageNamed:@"audio"];
  79. else {
  80. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  81. APLog(@"missing icon for type '%@'", self.dropboxFile.icon);
  82. }
  83. } else if(_driveFile != nil){
  84. BOOL isDirectory = [self.driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"];
  85. if (isDirectory) {
  86. self.folderTitleLabel.text = self.driveFile.title;
  87. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  88. self.folderTitleLabel.hidden = NO;
  89. } else {
  90. self.titleLabel.text = self.driveFile.title;
  91. self.subtitleLabel.text = (self.driveFile.fileSize > 0) ? [NSByteCountFormatter stringFromByteCount:[self.driveFile.fileSize longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
  92. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  93. self.folderTitleLabel.hidden = YES;
  94. }
  95. if (_driveFile.thumbnailLink != nil) {
  96. _iconURL = [NSURL URLWithString:_driveFile.thumbnailLink];
  97. [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
  98. }
  99. NSString *iconName = self.driveFile.iconLink;
  100. if (isDirectory) {
  101. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  102. } else if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_10_audio_list.png"]) {
  103. self.thumbnailView.image = [UIImage imageNamed:@"audio"];
  104. } else if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_11_video_list.png"]) {
  105. self.thumbnailView.image = [UIImage imageNamed:@"movie"];
  106. } else {
  107. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  108. APLog(@"missing icon for type '%@'", self.driveFile.iconLink);
  109. }
  110. } else if(_boxFile != nil) {
  111. BOOL isDirectory = [self.boxFile.type isEqualToString:@"folder"];
  112. if (isDirectory) {
  113. self.folderTitleLabel.text = self.boxFile.name;
  114. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  115. self.folderTitleLabel.hidden = NO;
  116. } else {
  117. self.titleLabel.text = self.boxFile.name;
  118. self.subtitleLabel.text = (self.boxFile.size > 0) ? [NSByteCountFormatter stringFromByteCount:[self.boxFile.size longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
  119. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  120. self.folderTitleLabel.hidden = YES;
  121. self.downloadButton.hidden = NO;
  122. }
  123. //TODO: correct thumbnails
  124. // if (_boxFile.modelID != nil) {
  125. // //this request needs a token in the header to work
  126. // 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];
  127. // _iconURL = [NSURL URLWithString:thumbnailURLString];
  128. // [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
  129. // }
  130. //TODO:correct icons
  131. if (isDirectory) {
  132. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  133. } else {
  134. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  135. APLog(@"missing icon for type '%@'", self.boxFile);
  136. }
  137. } else if(_oneDriveFile != nil) {
  138. if (_oneDriveFile.isFolder) {
  139. self.downloadButton.hidden = YES;
  140. self.folderTitleLabel.text = self.oneDriveFile.name;
  141. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  142. self.folderTitleLabel.hidden = NO;
  143. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  144. } else {
  145. self.downloadButton.hidden = NO;
  146. self.titleLabel.text = self.oneDriveFile.name;
  147. NSMutableString *subtitle = [[NSMutableString alloc] init];
  148. if (self.oneDriveFile.size > 0) {
  149. [subtitle appendString:[NSByteCountFormatter stringFromByteCount:[self.oneDriveFile.size longLongValue] countStyle:NSByteCountFormatterCountStyleFile]];
  150. if (self.oneDriveFile.duration > 0) {
  151. VLCTime *time = [VLCTime timeWithNumber:self.oneDriveFile.duration];
  152. [subtitle appendFormat:@" — %@", [time verboseStringValue]];
  153. }
  154. } else if (self.oneDriveFile.duration > 0) {
  155. VLCTime *time = [VLCTime timeWithNumber:self.oneDriveFile.duration];
  156. [subtitle appendString:[time verboseStringValue]];
  157. }
  158. self.subtitleLabel.text = subtitle;
  159. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  160. self.folderTitleLabel.hidden = YES;
  161. if (self.oneDriveFile.isAudio)
  162. self.thumbnailView.image = [UIImage imageNamed:@"audio"];
  163. else if (self.oneDriveFile.isVideo) {
  164. self.thumbnailView.image = [UIImage imageNamed:@"movie"];
  165. NSString *thumbnailURL = _oneDriveFile.thumbnailURL;
  166. if ([thumbnailURL isKindOfClass:[NSString class]]) {
  167. if (thumbnailURL.length > 0) {
  168. _iconURL = [NSURL URLWithString:thumbnailURL];
  169. [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
  170. }
  171. }
  172. } else
  173. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  174. }
  175. }
  176. [self setNeedsDisplay];
  177. }
  178. - (void)_updateIconFromURL
  179. {
  180. NSData *imageData = [[NSData alloc] initWithContentsOfURL:_iconURL];
  181. UIImage *icon = [[UIImage alloc] initWithData:imageData];
  182. if (icon != nil) {
  183. [self performSelectorOnMainThread:@selector(_updateIconOnMainThread:) withObject:icon waitUntilDone:NO];
  184. }
  185. }
  186. - (void)_updateIconOnMainThread:(UIImage *)icon
  187. {
  188. self.thumbnailView.contentMode = UIViewContentModeScaleAspectFit;
  189. self.thumbnailView.image = icon;
  190. }
  191. - (IBAction)triggerDownload:(id)sender
  192. {
  193. if ([self.delegate respondsToSelector:@selector(triggerDownloadForCell:)])
  194. [self.delegate triggerDownloadForCell:self];
  195. }
  196. + (CGFloat)heightOfCell
  197. {
  198. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
  199. return 80.;
  200. return 48.;
  201. }
  202. - (void)setIsDownloadable:(BOOL)isDownloadable
  203. {
  204. self.downloadButton.hidden = !isDownloadable;
  205. }
  206. @end