CCOfflinePageContent.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 ==== DZNEmptyDataSetSource Methods ====
  54. #pragma --------------------------------------------------------------------------------------------
  55. - (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
  56. {
  57. return YES;
  58. }
  59. - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
  60. {
  61. return 0.0f;
  62. }
  63. - (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
  64. {
  65. return - self.navigationController.navigationBar.frame.size.height;
  66. }
  67. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  68. {
  69. return [UIColor whiteColor];
  70. }
  71. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  72. {
  73. if ([self.pageType isEqualToString:pageOfflineOffline])
  74. return [UIImage imageNamed:image_brandOffline];
  75. if ([self.pageType isEqualToString:pageOfflineLocal])
  76. return [UIImage imageNamed:image_brandLocal];
  77. return nil;
  78. }
  79. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  80. {
  81. NSString *text;
  82. if ([self.pageType isEqualToString:pageOfflineOffline])
  83. text = NSLocalizedString(@"_no_files_uploaded_", nil);
  84. if ([self.pageType isEqualToString:pageOfflineLocal])
  85. text = NSLocalizedString(@"_no_files_uploaded_", nil);
  86. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:COLOR_BRAND};
  87. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  88. }
  89. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  90. {
  91. NSString *text;
  92. if ([self.pageType isEqualToString:pageOfflineOffline])
  93. text = NSLocalizedString(@"_tutorial_offline_view_", nil);
  94. if ([self.pageType isEqualToString:pageOfflineLocal])
  95. text = NSLocalizedString(@"_tutorial_local_view_", nil);
  96. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  97. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  98. paragraph.alignment = NSTextAlignmentCenter;
  99. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  100. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  101. }
  102. #pragma --------------------------------------------------------------------------------------------
  103. #pragma mark ==== Table ====
  104. #pragma --------------------------------------------------------------------------------------------
  105. - (void)reloadTable
  106. {
  107. [dataSource removeAllObjects];
  108. if ([_pageType isEqualToString:pageOfflineOffline]) {
  109. if (!_localServerUrl) {
  110. dataSource = (NSMutableArray*)[CCCoreData getHomeOfflineActiveAccount:app.activeAccount directoryUser:app.directoryUser];
  111. } else {
  112. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_localServerUrl activeAccount:app.activeAccount];
  113. NSArray *recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, directoryID] fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  114. CCSectionDataSource *sectionDataSource = [CCSection creataDataSourseSectionTableMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil replaceDateToExifDate:NO activeAccount:app.activeAccount];
  115. for (NSString *key in sectionDataSource.allRecordsDataSource)
  116. [dataSource insertObject:[sectionDataSource.allRecordsDataSource objectForKey:key] atIndex:0 ];
  117. }
  118. }
  119. if ([_pageType isEqualToString:pageOfflineLocal]) {
  120. NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:_localServerUrl error:nil];
  121. for (NSString *subpath in subpaths)
  122. if (![[subpath lastPathComponent] hasPrefix:@"."])
  123. [dataSource addObject:subpath];
  124. }
  125. [self.tableView reloadData];
  126. }
  127. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  128. {
  129. return 60;
  130. }
  131. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  132. {
  133. return 1;
  134. }
  135. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  136. {
  137. return [dataSource count];
  138. }
  139. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  140. {
  141. CCCellOffline *cell = (CCCellOffline *)[tableView dequeueReusableCellWithIdentifier:@"OfflineCell" forIndexPath:indexPath];
  142. CCMetadata *metadata;
  143. // Initialize
  144. cell.statusImageView.image = nil;
  145. cell.offlineImageView.image = nil;
  146. // change color selection
  147. UIView *selectionColor = [[UIView alloc] init];
  148. selectionColor.backgroundColor = COLOR_SELECT_BACKGROUND;
  149. cell.selectedBackgroundView = selectionColor;
  150. // i am in Offline
  151. if ([_pageType isEqualToString:pageOfflineOffline]) {
  152. metadata = [dataSource objectAtIndex:indexPath.row];
  153. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  154. if (_metadata.cryptated) cell.offlineImageView.image = [UIImage imageNamed:image_offlinecrypto];
  155. else cell.offlineImageView.image = [UIImage imageNamed:image_offline];
  156. }
  157. // i am in local
  158. if ([_pageType isEqualToString:pageOfflineLocal]) {
  159. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  160. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  161. metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_localServerUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  162. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/.%@.ico", _localServerUrl, metadata.fileNamePrint]];
  163. if (!cell.fileImageView.image) {
  164. 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]];
  165. if (icon) {
  166. [CCGraphics saveIcoWithFileID:metadata.fileNamePrint image:icon writeToFile:[NSString stringWithFormat:@"%@/.%@.ico", _localServerUrl, metadata.fileNamePrint] copy:NO move:NO fromPath:nil toPath:nil];
  167. cell.fileImageView.image = icon;
  168. }
  169. }
  170. }
  171. // color and font
  172. if (metadata.cryptated) {
  173. cell.labelTitle.textColor = COLOR_ENCRYPTED;
  174. //nameLabel.font = RalewayLight(13.0f);
  175. cell.labelInfoFile.textColor = [UIColor blackColor];
  176. //detailLabel.font = RalewayLight(9.0f);
  177. } else {
  178. cell.labelTitle.textColor = COLOR_CLEAR;
  179. //nameLabel.font = RalewayLight(13.0f);
  180. cell.labelInfoFile.textColor = [UIColor blackColor];
  181. //detailLabel.font = RalewayLight(9.0f);
  182. }
  183. if (metadata.directory) {
  184. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  185. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  186. }
  187. // File name
  188. cell.labelTitle.text = metadata.fileNamePrint;
  189. cell.labelInfoFile.text = @"";
  190. // Immagine del file, se non c'è l'anteprima mettiamo quella standard
  191. if (cell.fileImageView.image == nil)
  192. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  193. // it's encrypted ???
  194. if (metadata.cryptated && [metadata.type isEqualToString:metadataType_model] == NO)
  195. cell.statusImageView.image = [UIImage imageNamed:image_lock];
  196. // it's in download mode
  197. if ([metadata.session length] > 0 && [metadata.session rangeOfString:@"download"].location != NSNotFound)
  198. cell.statusImageView.image = [UIImage imageNamed:image_attention];
  199. // text and length
  200. if (metadata.directory) {
  201. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  202. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  203. } else {
  204. NSString *date = [CCUtility dateDiff:metadata.date];
  205. NSString *length = [CCUtility transformedSize:metadata.size];
  206. if ([metadata.type isEqualToString:metadataType_model])
  207. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", date];
  208. if ([metadata.type isEqualToString:metadataType_file] || [metadata.type isEqualToString:metadataType_local])
  209. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", date, length];
  210. cell.accessoryType = UITableViewCellAccessoryNone;
  211. }
  212. return cell;
  213. }
  214. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  215. {
  216. // deselect row
  217. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  218. if ([_pageType isEqualToString:pageOfflineOffline]) {
  219. NSManagedObject *record = [dataSource objectAtIndex:indexPath.row];
  220. _fileIDPhoto = [record valueForKey:@"fileID"];
  221. _metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", self.fileIDPhoto, app.activeAccount] context:nil];
  222. }
  223. if ([_pageType isEqualToString:pageOfflineLocal]) {
  224. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  225. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  226. _metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_localServerUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  227. _fileIDPhoto = _metadata.fileID;
  228. }
  229. // if is in download [do not touch]
  230. if ([_metadata.session length] > 0 && [_metadata.session rangeOfString:@"download"].location != NSNotFound) return;
  231. if (([_metadata.type isEqualToString:metadataType_file] || [_metadata.type isEqualToString:metadataType_local]) && _metadata.directory == NO) {
  232. if ([self shouldPerformSegue])
  233. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  234. }
  235. //if ([self.metadata.type isEqualToString:metadataType_model]) [self openModel:self.metadata];
  236. if (_metadata.directory)
  237. [self performSegueDirectoryWithControlPasscode];
  238. }
  239. -(void)performSegueDirectoryWithControlPasscode
  240. {
  241. CCOfflinePageContent *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"OfflinePageContentViewController"];
  242. NSString *serverUrl;
  243. if ([_pageType isEqualToString:pageOfflineOffline] && !_localServerUrl) {
  244. serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:app.activeAccount];
  245. } else {
  246. serverUrl = _localServerUrl;
  247. }
  248. vc.localServerUrl = [CCUtility stringAppendServerUrl:serverUrl addServerUrl:_metadata.fileName];
  249. vc.pageType = _pageType;
  250. [self.navigationController pushViewController:vc animated:YES];
  251. }
  252. #pragma --------------------------------------------------------------------------------------------
  253. #pragma mark ===== Navigation ====
  254. #pragma --------------------------------------------------------------------------------------------
  255. - (BOOL)shouldPerformSegue
  256. {
  257. // if i am in background -> exit
  258. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  259. // if i am not window -> exit
  260. if (self.view.window == NO)
  261. return NO;
  262. // Collapsed but i am in detail -> exit
  263. if (self.splitViewController.isCollapsed)
  264. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  265. // Video in run -> exit
  266. if (self.detailViewController.photoBrowser.currentVideoPlayerViewController.isViewLoaded && self.detailViewController.photoBrowser.currentVideoPlayerViewController.view.window) return NO;
  267. return YES;
  268. }
  269. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  270. {
  271. id viewController = segue.destinationViewController;
  272. if ([viewController isKindOfClass:[UINavigationController class]]) {
  273. UINavigationController *nav = viewController;
  274. self.detailViewController = (CCDetail *)nav.topViewController;
  275. } else {
  276. self.detailViewController = segue.destinationViewController;
  277. }
  278. self.detailViewController.metadataDetail = _metadata;
  279. if (app.isLocalStorage) self.detailViewController.sourceDirectory = sorceDirectoryLocal;
  280. else self.detailViewController.sourceDirectory = sorceDirectoryOffline;
  281. self.detailViewController.dateFilterQuery = nil;
  282. self.detailViewController.isCameraUpload = NO;
  283. [self.detailViewController setTitle:_metadata.fileNamePrint];
  284. }
  285. @end