CCOfflinePageContent.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //
  2. // CCOfflinePageContent.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 01/02/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. #import "CCOfflinePageContent.h"
  9. #import "AppDelegate.h"
  10. @interface CCOfflinePageContent ()
  11. {
  12. NSMutableArray *dataSource;
  13. }
  14. @end
  15. @implementation CCOfflinePageContent
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. // Custom Cell
  19. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellOffline" bundle:nil] forCellReuseIdentifier:@"OfflineCell"];
  20. // dataSource
  21. dataSource = [NSMutableArray new];
  22. // Metadata
  23. _metadata = [CCMetadata new];
  24. self.tableView.emptyDataSetDelegate = self;
  25. self.tableView.emptyDataSetSource = self;
  26. self.tableView.tableFooterView = [UIView new];
  27. self.tableView.separatorColor = COLOR_SEPARATOR_TABLE;
  28. // Type
  29. if ([self.pageType isEqualToString:pageOfflineOffline] && !_localServerUrl) {
  30. _localServerUrl = nil;
  31. }
  32. if ([self.pageType isEqualToString:pageOfflineLocal] && !_localServerUrl) {
  33. _localServerUrl = [CCUtility getDirectoryLocal];
  34. }
  35. }
  36. // Apparirà
  37. - (void)viewWillAppear:(BOOL)animated
  38. {
  39. [super viewWillAppear:animated];
  40. // Color
  41. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar hidden:NO];
  42. [CCAspect aspectTabBar:self.tabBarController.tabBar hidden:NO];
  43. // Plus Button
  44. [app plusButtonVisibile:true];
  45. [self reloadTable];
  46. }
  47. - (void)didReceiveMemoryWarning {
  48. [super didReceiveMemoryWarning];
  49. }
  50. #pragma --------------------------------------------------------------------------------------------
  51. #pragma mark ==== Table ====
  52. #pragma --------------------------------------------------------------------------------------------
  53. - (void)reloadTable
  54. {
  55. [dataSource removeAllObjects];
  56. if ([_pageType isEqualToString:pageOfflineOffline]) {
  57. if (!_localServerUrl) {
  58. dataSource = (NSMutableArray*)[CCCoreData getHomeOfflineActiveAccount:app.activeAccount directoryUser:app.directoryUser];
  59. } else {
  60. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_localServerUrl activeAccount:app.activeAccount];
  61. NSArray *recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, directoryID] fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  62. CCSectionDataSource *sectionDataSource = [CCSection creataDataSourseSectionTableMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil replaceDateToExifDate:NO activeAccount:app.activeAccount];
  63. for (NSString *key in sectionDataSource.allRecordsDataSource)
  64. [dataSource insertObject:[sectionDataSource.allRecordsDataSource objectForKey:key] atIndex:0 ];
  65. }
  66. }
  67. if ([_pageType isEqualToString:pageOfflineLocal]) {
  68. NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:_localServerUrl error:nil];
  69. for (NSString *subpath in subpaths)
  70. if (![[subpath lastPathComponent] hasPrefix:@"."])
  71. [dataSource addObject:subpath];
  72. }
  73. [self.tableView reloadData];
  74. }
  75. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  76. {
  77. return 60;
  78. }
  79. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  80. {
  81. return 1;
  82. }
  83. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  84. {
  85. return [dataSource count];
  86. }
  87. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  88. {
  89. CCCellOffline *cell = (CCCellOffline *)[tableView dequeueReusableCellWithIdentifier:@"OfflineCell" forIndexPath:indexPath];
  90. CCMetadata *metadata;
  91. // Initialize
  92. cell.statusImageView.image = nil;
  93. cell.offlineImageView.image = nil;
  94. // change color selection
  95. UIView *selectionColor = [[UIView alloc] init];
  96. selectionColor.backgroundColor = COLOR_SELECT_BACKGROUND;
  97. cell.selectedBackgroundView = selectionColor;
  98. // i am in Offline
  99. if ([_pageType isEqualToString:pageOfflineOffline]) {
  100. metadata = [dataSource objectAtIndex:indexPath.row];
  101. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  102. if (_metadata.cryptated) cell.offlineImageView.image = [UIImage imageNamed:image_offlinecrypto];
  103. else cell.offlineImageView.image = [UIImage imageNamed:image_offline];
  104. }
  105. // i am in local
  106. if ([_pageType isEqualToString:pageOfflineLocal]) {
  107. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  108. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  109. metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_localServerUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  110. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/.%@.ico", _localServerUrl, metadata.fileNamePrint]];
  111. if (!cell.fileImageView.image) {
  112. UIImage *icon = [CCGraphics createNewImageFrom:metadata.fileID directoryUser:_localServerUrl fileNameTo:metadata.fileID fileNamePrint:metadata.fileNamePrint size:@"m" imageForUpload:NO typeFile:metadata.typeFile writePreview:NO optimizedFileName:[CCUtility getOptimizedPhoto]];
  113. if (icon) {
  114. [CCGraphics saveIcoWithFileID:metadata.fileNamePrint image:icon writeToFile:[NSString stringWithFormat:@"%@/.%@.ico", _localServerUrl, metadata.fileNamePrint] copy:NO move:NO fromPath:nil toPath:nil];
  115. cell.fileImageView.image = icon;
  116. }
  117. }
  118. }
  119. // color and font
  120. if (metadata.cryptated) {
  121. cell.labelTitle.textColor = COLOR_ENCRYPTED;
  122. //nameLabel.font = RalewayLight(13.0f);
  123. cell.labelInfoFile.textColor = [UIColor blackColor];
  124. //detailLabel.font = RalewayLight(9.0f);
  125. } else {
  126. cell.labelTitle.textColor = COLOR_CLEAR;
  127. //nameLabel.font = RalewayLight(13.0f);
  128. cell.labelInfoFile.textColor = [UIColor blackColor];
  129. //detailLabel.font = RalewayLight(9.0f);
  130. }
  131. if (metadata.directory) {
  132. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  133. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  134. }
  135. // File name
  136. cell.labelTitle.text = metadata.fileNamePrint;
  137. cell.labelInfoFile.text = @"";
  138. // Immagine del file, se non c'è l'anteprima mettiamo quella standard
  139. if (cell.fileImageView.image == nil)
  140. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  141. // it's encrypted ???
  142. if (metadata.cryptated && [metadata.type isEqualToString:metadataType_model] == NO)
  143. cell.statusImageView.image = [UIImage imageNamed:image_lock];
  144. // it's in download mode
  145. if ([metadata.session length] > 0 && [metadata.session rangeOfString:@"download"].location != NSNotFound)
  146. cell.statusImageView.image = [UIImage imageNamed:image_attention];
  147. // text and length
  148. if (metadata.directory) {
  149. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  150. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  151. } else {
  152. NSString *date = [CCUtility dateDiff:metadata.date];
  153. NSString *length = [CCUtility transformedSize:metadata.size];
  154. if ([metadata.type isEqualToString:metadataType_model])
  155. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", date];
  156. if ([metadata.type isEqualToString:metadataType_file] || [metadata.type isEqualToString:metadataType_local])
  157. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", date, length];
  158. cell.accessoryType = UITableViewCellAccessoryNone;
  159. }
  160. return cell;
  161. }
  162. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  163. {
  164. // deselect row
  165. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  166. if ([_pageType isEqualToString:pageOfflineOffline]) {
  167. NSManagedObject *record = [dataSource objectAtIndex:indexPath.row];
  168. _fileIDPhoto = [record valueForKey:@"fileID"];
  169. _metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", self.fileIDPhoto, app.activeAccount] context:nil];
  170. }
  171. if ([_pageType isEqualToString:pageOfflineLocal]) {
  172. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  173. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  174. _metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_localServerUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  175. _fileIDPhoto = _metadata.fileID;
  176. }
  177. // if is in download [do not touch]
  178. if ([_metadata.session length] > 0 && [_metadata.session rangeOfString:@"download"].location != NSNotFound) return;
  179. if (([_metadata.type isEqualToString:metadataType_file] || [_metadata.type isEqualToString:metadataType_local]) && _metadata.directory == NO) {
  180. if ([self shouldPerformSegue])
  181. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  182. }
  183. //if ([self.metadata.type isEqualToString:metadataType_model]) [self openModel:self.metadata];
  184. if (_metadata.directory)
  185. [self performSegueDirectoryWithControlPasscode];
  186. }
  187. -(void)performSegueDirectoryWithControlPasscode
  188. {
  189. CCOfflinePageContent *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"OfflinePageContentViewController"];
  190. NSString *serverUrl;
  191. if ([_pageType isEqualToString:pageOfflineOffline] && !_localServerUrl) {
  192. serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:app.activeAccount];
  193. } else {
  194. serverUrl = _localServerUrl;
  195. }
  196. vc.localServerUrl = [CCUtility stringAppendServerUrl:serverUrl addServerUrl:_metadata.fileName];
  197. vc.pageType = _pageType;
  198. [self.navigationController pushViewController:vc animated:YES];
  199. }
  200. #pragma --------------------------------------------------------------------------------------------
  201. #pragma mark ===== Navigation ====
  202. #pragma --------------------------------------------------------------------------------------------
  203. - (BOOL)shouldPerformSegue
  204. {
  205. // if i am in background -> exit
  206. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  207. // if i am not window -> exit
  208. if (self.view.window == NO)
  209. return NO;
  210. // Collapsed but i am in detail -> exit
  211. if (self.splitViewController.isCollapsed)
  212. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  213. // Video in run -> exit
  214. if (self.detailViewController.photoBrowser.currentVideoPlayerViewController.isViewLoaded && self.detailViewController.photoBrowser.currentVideoPlayerViewController.view.window) return NO;
  215. return YES;
  216. }
  217. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  218. {
  219. id viewController = segue.destinationViewController;
  220. if ([viewController isKindOfClass:[UINavigationController class]]) {
  221. UINavigationController *nav = viewController;
  222. self.detailViewController = (CCDetail *)nav.topViewController;
  223. } else {
  224. self.detailViewController = segue.destinationViewController;
  225. }
  226. self.detailViewController.metadataDetail = _metadata;
  227. if (app.isLocalStorage) self.detailViewController.sourceDirectory = sorceDirectoryLocal;
  228. else self.detailViewController.sourceDirectory = sorceDirectoryOffline;
  229. self.detailViewController.dateFilterQuery = nil;
  230. self.detailViewController.isCameraUpload = NO;
  231. [self.detailViewController setTitle:_metadata.fileNamePrint];
  232. }
  233. @end