CCOfflinePageContent.m 13 KB

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