VLCCloudStorageTableViewCell.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*****************************************************************************
  2. * VLCCloudStorageTableViewCell.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2019 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. #import "VLCNetworkImageView.h"
  15. @implementation VLCCloudStorageTableViewCell
  16. + (VLCCloudStorageTableViewCell *)cellWithReuseIdentifier:(NSString *)ident
  17. {
  18. NSArray *nibContentArray = [[NSBundle mainBundle] loadNibNamed:@"VLCCloudStorageTableViewCell" owner:nil options:nil];
  19. NSAssert([nibContentArray count] == 1, @"meh");
  20. NSAssert([[nibContentArray lastObject] isKindOfClass:[VLCCloudStorageTableViewCell class]], @"meh meh");
  21. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[nibContentArray lastObject];
  22. cell.titleLabel.hidden = YES;
  23. cell.subtitleLabel.hidden = YES;
  24. cell.folderTitleLabel.hidden = YES;
  25. return cell;
  26. }
  27. - (void)setDropboxFile:(DBFILESMetadata *)dropboxFile
  28. {
  29. if (dropboxFile != _dropboxFile)
  30. _dropboxFile = dropboxFile;
  31. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  32. withObject:nil waitUntilDone:NO];
  33. }
  34. #if TARGET_OS_IOS
  35. - (void)setDriveFile:(GTLRDrive_File *)driveFile
  36. {
  37. if (driveFile != _driveFile)
  38. _driveFile = driveFile;
  39. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  40. withObject:nil waitUntilDone:NO];
  41. }
  42. #endif
  43. - (void)setBoxFile:(BoxItem *)boxFile
  44. {
  45. if (boxFile != _boxFile)
  46. _boxFile = boxFile;
  47. [self performSelectorOnMainThread:@selector(_updatedDisplayedInformation)
  48. withObject:nil waitUntilDone:NO];
  49. }
  50. - (void)updateOneDriveDisplayAsFolder
  51. {
  52. _downloadButton.hidden = YES;
  53. _folderTitleLabel.text = _oneDriveFile.name;
  54. _titleLabel.hidden = _subtitleLabel.hidden = YES;
  55. _folderTitleLabel.hidden = NO;
  56. _thumbnailView.image = [UIImage imageNamed:@"folder"];
  57. }
  58. - (void)loadThumbnail
  59. {
  60. // The onedrive Api has no way to cancel a request and the ODThumbnail has no back reference to it's item
  61. // 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
  62. ODDriveRequestBuilder *drive = [[ODClient loadCurrentClient] drive];
  63. ODThumbnailRequest *request = [[[[drive items:_oneDriveFile.id] thumbnails:@"0"] medium] request];
  64. __weak typeof(self) weakSelf = self;
  65. [request getWithCompletion:^(ODThumbnail *response, NSError *error) {
  66. if (error == nil && response.url) {// we don't care about errors for thumbnails
  67. dispatch_async(dispatch_get_main_queue(), ^{
  68. [weakSelf.thumbnailView setImageWithURL:[NSURL URLWithString:response.url]];
  69. });
  70. }
  71. }];
  72. }
  73. - (void)updateOneDriveDisplayAsItem
  74. {
  75. int64_t duration = 0;
  76. NSString *title = self.oneDriveFile.name;
  77. NSMutableString *subtitle = [[NSMutableString alloc] init];
  78. _downloadButton.hidden = NO;
  79. _titleLabel.text = title;
  80. if (_oneDriveFile.audio) {
  81. _thumbnailView.image = [UIImage imageNamed:@"audio"];
  82. duration = _oneDriveFile.audio.duration;
  83. [self loadThumbnail];
  84. } else if (_oneDriveFile.video) {
  85. _thumbnailView.image = [UIImage imageNamed:@"movie"];
  86. duration = _oneDriveFile.video.duration;
  87. [self loadThumbnail];
  88. } else {
  89. _thumbnailView.image = [UIImage imageNamed:@"blank"];
  90. }
  91. if (duration > 0) {
  92. VLCTime *time = [VLCTime timeWithNumber:[NSNumber numberWithLong:duration]];
  93. [subtitle appendString:[time verboseStringValue]];
  94. }
  95. if (_oneDriveFile.size > 0) {
  96. subtitle = [NSMutableString stringWithString:[NSByteCountFormatter
  97. stringFromByteCount:_oneDriveFile.size
  98. countStyle:NSByteCountFormatterCountStyleFile]];
  99. if (duration > 0) {
  100. VLCTime *time = [VLCTime timeWithNumber:[NSNumber numberWithLong:duration]];
  101. [subtitle appendFormat:@" — %@", [time verboseStringValue]];
  102. }
  103. }
  104. _subtitleLabel.text = subtitle;
  105. _titleLabel.hidden = _subtitleLabel.hidden = NO;
  106. _folderTitleLabel.hidden = YES;
  107. }
  108. - (void)updateOneDriveDisplayedInformation
  109. {
  110. _oneDriveFile.folder ? [self updateOneDriveDisplayAsFolder] : [self updateOneDriveDisplayAsItem];
  111. }
  112. - (void)setOneDriveFile:(ODItem *)oneDriveFile
  113. {
  114. if (oneDriveFile != _oneDriveFile) {
  115. _oneDriveFile = oneDriveFile;
  116. [self updateOneDriveDisplayedInformation];
  117. }
  118. }
  119. - (void)_updatedDisplayedInformation
  120. {
  121. if (_dropboxFile != nil) {
  122. if ([_dropboxFile isKindOfClass:[DBFILESFolderMetadata class]]) {
  123. self.folderTitleLabel.text = self.dropboxFile.name;
  124. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  125. self.folderTitleLabel.hidden = NO;
  126. self.downloadButton.hidden = YES;
  127. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  128. } else if ([_dropboxFile isKindOfClass:[DBFILESFileMetadata class]]) {
  129. DBFILESFileMetadata *file = (DBFILESFileMetadata *)_dropboxFile;
  130. self.titleLabel.text = file.name;
  131. self.subtitleLabel.text = (file.size.integerValue > 0) ? [NSByteCountFormatter stringFromByteCount:file.size.longLongValue countStyle:NSByteCountFormatterCountStyleFile] : @"";
  132. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  133. self.folderTitleLabel.hidden = YES;
  134. self.downloadButton.hidden = NO;
  135. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  136. }
  137. }
  138. #if TARGET_OS_IOS
  139. else if(_driveFile != nil){
  140. BOOL isDirectory = [self.driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"];
  141. if (isDirectory) {
  142. self.folderTitleLabel.text = self.driveFile.name;
  143. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  144. self.folderTitleLabel.hidden = NO;
  145. } else {
  146. NSString *title = self.driveFile.name;
  147. self.titleLabel.text = title;
  148. self.subtitleLabel.text = (self.driveFile.size > 0) ? [NSByteCountFormatter stringFromByteCount:[self.driveFile.size longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
  149. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  150. self.folderTitleLabel.hidden = YES;
  151. if (_driveFile.thumbnailLink != nil) {
  152. [self.thumbnailView setImageWithURL:[NSURL URLWithString:_driveFile.thumbnailLink]];
  153. }
  154. }
  155. NSString *iconName = self.driveFile.iconLink;
  156. if (isDirectory) {
  157. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  158. } else if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_10_audio_list.png"]) {
  159. self.thumbnailView.image = [UIImage imageNamed:@"audio"];
  160. } else if ([iconName isEqualToString:@"https://ssl.gstatic.com/docs/doclist/images/icon_11_video_list.png"]) {
  161. self.thumbnailView.image = [UIImage imageNamed:@"movie"];
  162. } else {
  163. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  164. APLog(@"missing icon for type '%@'", self.driveFile.iconLink);
  165. }
  166. }
  167. #endif
  168. else if(_boxFile != nil) {
  169. BOOL isDirectory = [self.boxFile.type isEqualToString:@"folder"];
  170. if (isDirectory) {
  171. self.folderTitleLabel.text = self.boxFile.name;
  172. self.titleLabel.hidden = self.subtitleLabel.hidden = YES;
  173. self.folderTitleLabel.hidden = NO;
  174. } else {
  175. NSString *title = self.boxFile.name;
  176. self.titleLabel.text = title;
  177. self.subtitleLabel.text = (self.boxFile.size > 0) ? [NSByteCountFormatter stringFromByteCount:[self.boxFile.size longLongValue] countStyle:NSByteCountFormatterCountStyleFile]: @"";
  178. self.titleLabel.hidden = self.subtitleLabel.hidden = NO;
  179. self.folderTitleLabel.hidden = YES;
  180. self.downloadButton.hidden = NO;
  181. }
  182. //TODO: correct thumbnails
  183. // if (_boxFile.modelID != nil) {
  184. // //this request needs a token in the header to work
  185. // 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];
  186. // [self.thumbnailView setImageWithURL:[NSURL URLWithString:thumbnailURLString]];
  187. // }
  188. //TODO:correct icons
  189. if (isDirectory) {
  190. self.thumbnailView.image = [UIImage imageNamed:@"folder"];
  191. } else {
  192. self.thumbnailView.image = [UIImage imageNamed:@"blank"];
  193. APLog(@"missing icon for type '%@'", self.boxFile);
  194. }
  195. } else if(_oneDriveFile != nil) {
  196. [self updateOneDriveDisplayedInformation];
  197. }
  198. [self setNeedsDisplay];
  199. }
  200. - (IBAction)triggerDownload:(id)sender
  201. {
  202. if ([self.delegate respondsToSelector:@selector(triggerDownloadForCell:)])
  203. [self.delegate triggerDownloadForCell:self];
  204. }
  205. + (CGFloat)heightOfCell
  206. {
  207. #if TARGET_OS_IOS
  208. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
  209. return 80.;
  210. return 48.;
  211. #else
  212. return 107.;
  213. #endif
  214. }
  215. - (void)setIsDownloadable:(BOOL)isDownloadable
  216. {
  217. self.downloadButton.hidden = !isDownloadable;
  218. }
  219. - (void)prepareForReuse {
  220. [super prepareForReuse];
  221. _thumbnailView.image = nil;
  222. }
  223. @end