CCOfflinePageContent.m 12 KB

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