VLCCloudStorageTableViewCell.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*****************************************************************************
  2. * VLCCloudStorageTableViewCell.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013 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)setFileMetadata:(DBMetadata *)fileMetadata
  29. {
  30. if (fileMetadata != _fileMetadata)
  31. _fileMetadata = fileMetadata;
  32. [self _updatedDisplayedInformation];
  33. }
  34. - (void)setDriveFile:(GTLDriveFile *)driveFile
  35. {
  36. if (driveFile != _driveFile)
  37. _driveFile = driveFile;
  38. [self _updatedDisplayedInformation];
  39. }
  40. - (void)setBoxFile:(BoxItem *)boxFile
  41. {
  42. if (boxFile != _boxFile)
  43. _boxFile = boxFile;
  44. [self _updatedDisplayedInformation];
  45. }
  46. - (void)setOneDriveFile:(VLCOneDriveObject *)oneDriveFile
  47. {
  48. if (oneDriveFile != _oneDriveFile)
  49. _oneDriveFile = oneDriveFile;
  50. [self _updatedDisplayedInformation];
  51. }
  52. - (void)_updatedDisplayedInformation
  53. {
  54. if (_fileMetadata != nil) {
  55. if (self.fileMetadata.isDirectory) {
  56. self.folderTitleLabel.text = self.fileMetadata.filename;
  57. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  58. self.folderTitleLabel.hidden = NO;
  59. } else {
  60. self.titleLabel.text = self.fileMetadata.filename;
  61. self.subtitleLabel.text = (self.fileMetadata.totalBytes > 0) ? self.fileMetadata.humanReadableSize : @"";
  62. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  63. self.folderTitleLabel.hidden = YES;
  64. }
  65. NSString *iconName = self.fileMetadata.icon;
  66. if ([iconName isEqualToString:@"folder_user"] || [iconName isEqualToString:@"folder"] || [iconName isEqualToString:@"folder_public"] || [iconName isEqualToString:@"folder_photos"] || [iconName isEqualToString:@"package"]) {
  67. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  68. self.downloadButton.hidden = YES;
  69. } else if ([iconName isEqualToString:@"page_white"] || [iconName isEqualToString:@"page_white_text"])
  70. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  71. else if ([iconName isEqualToString:@"page_white_film"])
  72. self.thumbnailView.image = [UIImage imageNamed:@"movie"];
  73. else if ([iconName isEqualToString:@"page_white_sound"])
  74. self.thumbnailView.image = [UIImage imageNamed:@"audio"];
  75. else {
  76. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  77. APLog(@"missing icon for type '%@'", self.fileMetadata.icon);
  78. }
  79. } else if(_driveFile != nil){
  80. BOOL isDirectory = [self.driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"];
  81. if (isDirectory) {
  82. self.folderTitleLabel.text = self.driveFile.title;
  83. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  84. self.folderTitleLabel.hidden = NO;
  85. } else {
  86. self.titleLabel.text = self.driveFile.title;
  87. self.subtitleLabel.text = (self.driveFile.fileSize > 0) ? [NSByteCountFormatter stringFromByteCount:[self.driveFile.fileSize longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
  88. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  89. self.folderTitleLabel.hidden = YES;
  90. }
  91. if (_driveFile.thumbnailLink != nil) {
  92. _iconURL = [NSURL URLWithString:_driveFile.thumbnailLink];
  93. [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
  94. }
  95. NSString *iconName = self.driveFile.iconLink;
  96. if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_11_shared_collection_list.png"] || [iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_11_collection_list.png"]) {
  97. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  98. } else if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_10_audio_list.png"]) {
  99. self.thumbnailView.image = [UIImage imageNamed:@"audio"];
  100. } else if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_11_video_list.png"]) {
  101. self.thumbnailView.image = [UIImage imageNamed:@"movie"];
  102. } else {
  103. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  104. APLog(@"missing icon for type '%@'", self.driveFile.iconLink);
  105. }
  106. } else if(_boxFile != nil) {
  107. BOOL isDirectory = [self.boxFile.type isEqualToString:@"folder"];
  108. if (isDirectory) {
  109. self.folderTitleLabel.text = self.boxFile.name;
  110. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  111. self.folderTitleLabel.hidden = NO;
  112. } else {
  113. self.titleLabel.text = self.boxFile.name;
  114. self.subtitleLabel.text = (self.boxFile.size > 0) ? [NSByteCountFormatter stringFromByteCount:[self.boxFile.size longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
  115. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  116. self.folderTitleLabel.hidden = YES;
  117. self.downloadButton.hidden = NO;
  118. }
  119. //TODO: correct thumbnails
  120. // if (_boxFile.modelID != nil) {
  121. // //this request needs a token in the header to work
  122. // 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];
  123. // _iconURL = [NSURL URLWithString:thumbnailURLString];
  124. // [self performSelectorInBackground:@selector(_updateIconFromURL) withObject:@""];
  125. // }
  126. //TODO:correct icons
  127. if (isDirectory) {
  128. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  129. } else {
  130. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  131. APLog(@"missing icon for type '%@'", self.boxFile);
  132. }
  133. } else if(_oneDriveFile != nil) {
  134. if (_oneDriveFile.isFolder) {
  135. self.downloadButton.hidden = YES;
  136. self.folderTitleLabel.text = self.oneDriveFile.name;
  137. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  138. self.folderTitleLabel.hidden = NO;
  139. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  140. } else {
  141. self.downloadButton.hidden = NO;
  142. self.titleLabel.text = self.oneDriveFile.name;
  143. NSMutableString *subtitle = [[NSMutableString alloc] init];
  144. if (self.oneDriveFile.size > 0) {
  145. [subtitle appendString:[NSByteCountFormatter stringFromByteCount:[self.oneDriveFile.size longLongValue] countStyle:NSByteCountFormatterCountStyleFile]];
  146. if (self.oneDriveFile.duration > 0) {
  147. VLCTime *time = [VLCTime timeWithNumber:self.oneDriveFile.duration];
  148. [subtitle appendFormat:@" — %@", [time verboseStringValue]];
  149. }
  150. } else if (self.oneDriveFile.duration > 0) {
  151. VLCTime *time = [VLCTime timeWithNumber:self.oneDriveFile.duration];
  152. [subtitle appendString:[time verboseStringValue]];
  153. }
  154. self.subtitleLabel.text = subtitle;
  155. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  156. self.folderTitleLabel.hidden = YES;
  157. if (self.oneDriveFile.isAudio)
  158. self.thumbnailView.image = [UIImage imageNamed:@"audio"];
  159. else if (self.oneDriveFile.isVideo)
  160. self.thumbnailView.image = [UIImage imageNamed:@"movie"];
  161. else
  162. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  163. }
  164. }
  165. [self setNeedsDisplay];
  166. }
  167. - (void)_updateIconFromURL
  168. {
  169. NSData* imageData = [[NSData alloc]initWithContentsOfURL:_iconURL];
  170. UIImage* image = [[UIImage alloc] initWithData:imageData];
  171. if (image != nil) {
  172. self.thumbnailView.image = image;
  173. }
  174. }
  175. - (IBAction)triggerDownload:(id)sender
  176. {
  177. if ([self.delegate respondsToSelector:@selector(triggerDownloadForCell:)])
  178. [self.delegate triggerDownloadForCell:self];
  179. }
  180. + (CGFloat)heightOfCell
  181. {
  182. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
  183. return 80.;
  184. return 48.;
  185. }
  186. @end