CCOfflinePageContent.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. NSArray *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. // calculate _localServerUrl
  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 = _titleViewControl;
  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. // E' arrivato
  50. - (void)viewDidAppear:(BOOL)animated
  51. {
  52. [super viewDidAppear:animated];
  53. // cancell Progress
  54. [self.navigationController cancelCCProgress];
  55. }
  56. - (void)didReceiveMemoryWarning {
  57. [super didReceiveMemoryWarning];
  58. }
  59. #pragma --------------------------------------------------------------------------------------------
  60. #pragma mark ==== DZNEmptyDataSetSource Methods ====
  61. #pragma --------------------------------------------------------------------------------------------
  62. - (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
  63. {
  64. // only for root
  65. if (!_localServerUrl || [_localServerUrl isEqualToString:[CCUtility getDirectoryLocal]])
  66. return YES;
  67. else
  68. return NO;
  69. }
  70. - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
  71. {
  72. return 0.0f;
  73. }
  74. - (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
  75. {
  76. return - self.navigationController.navigationBar.frame.size.height;
  77. }
  78. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  79. {
  80. return [UIColor whiteColor];
  81. }
  82. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  83. {
  84. if ([self.pageType isEqualToString:pageOfflineOffline])
  85. return [UIImage imageNamed:image_brandOffline];
  86. if ([self.pageType isEqualToString:pageOfflineLocal])
  87. return [UIImage imageNamed:image_brandLocal];
  88. return nil;
  89. }
  90. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  91. {
  92. NSString *text;
  93. if ([self.pageType isEqualToString:pageOfflineOffline])
  94. text = NSLocalizedString(@"_no_files_uploaded_", nil);
  95. if ([self.pageType isEqualToString:pageOfflineLocal])
  96. text = NSLocalizedString(@"_no_files_uploaded_", nil);
  97. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:COLOR_BRAND};
  98. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  99. }
  100. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  101. {
  102. NSString *text;
  103. if ([self.pageType isEqualToString:pageOfflineOffline])
  104. text = NSLocalizedString(@"_tutorial_offline_view_", nil);
  105. if ([self.pageType isEqualToString:pageOfflineLocal])
  106. text = NSLocalizedString(@"_tutorial_local_view_", nil);
  107. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  108. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  109. paragraph.alignment = NSTextAlignmentCenter;
  110. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  111. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  112. }
  113. #pragma --------------------------------------------------------------------------------------------
  114. #pragma mark ===== UIDocumentInteractionControllerDelegate =====
  115. #pragma --------------------------------------------------------------------------------------------
  116. - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
  117. {
  118. // evitiamo il rimando della eventuale photo e/o video
  119. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) {
  120. [CCCoreData setCameraUploadDatePhoto:[NSDate date]];
  121. [CCCoreData setCameraUploadDateVideo:[NSDate date]];
  122. }
  123. }
  124. #pragma --------------------------------------------------------------------------------------------
  125. #pragma mark ==== Table ====
  126. #pragma --------------------------------------------------------------------------------------------
  127. - (void)reloadTable
  128. {
  129. if ([_pageType isEqualToString:pageOfflineOffline]) {
  130. if (!_localServerUrl) {
  131. dataSource = [CCCoreData getHomeOfflineActiveAccount:app.activeAccount directoryUser:app.directoryUser];
  132. dispatch_async(dispatch_get_main_queue(), ^{
  133. [self.tableView reloadData];
  134. });
  135. } else {
  136. NSMutableArray *metadatas = [NSMutableArray new];
  137. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_localServerUrl activeAccount:app.activeAccount];
  138. NSArray *recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, directoryID] fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  139. CCSectionDataSource *sectionDataSource = [CCSection creataDataSourseSectionTableMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil replaceDateToExifDate:NO activeAccount:app.activeAccount];
  140. for (NSString *key in sectionDataSource.allRecordsDataSource)
  141. [metadatas insertObject:[sectionDataSource.allRecordsDataSource objectForKey:key] atIndex:0 ];
  142. dataSource = [NSArray arrayWithArray:metadatas];
  143. dispatch_async(dispatch_get_main_queue(), ^{
  144. [self.tableView reloadData];
  145. });
  146. }
  147. }
  148. if ([_pageType isEqualToString:pageOfflineLocal]) {
  149. NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:_localServerUrl error:nil];
  150. NSMutableArray *metadatas = [NSMutableArray new];
  151. for (NSString *subpath in subpaths)
  152. if (![[subpath lastPathComponent] hasPrefix:@"."])
  153. [metadatas addObject:subpath];
  154. dataSource = [NSArray arrayWithArray:metadatas];
  155. dispatch_async(dispatch_get_main_queue(), ^{
  156. [self.tableView reloadData];
  157. });
  158. }
  159. }
  160. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  161. {
  162. return 60;
  163. }
  164. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  165. {
  166. return 1;
  167. }
  168. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  169. {
  170. return [dataSource count];
  171. }
  172. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  173. {
  174. CCCellOffline *cell = (CCCellOffline *)[tableView dequeueReusableCellWithIdentifier:@"OfflineCell" forIndexPath:indexPath];
  175. CCMetadata *metadata;
  176. // Initialize
  177. cell.statusImageView.image = nil;
  178. cell.offlineImageView.image = nil;
  179. // change color selection
  180. UIView *selectionColor = [[UIView alloc] init];
  181. selectionColor.backgroundColor = COLOR_SELECT_BACKGROUND;
  182. cell.selectedBackgroundView = selectionColor;
  183. // i am in Offline
  184. if ([_pageType isEqualToString:pageOfflineOffline]) {
  185. metadata = [dataSource objectAtIndex:indexPath.row];
  186. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  187. if (metadata.cryptated)
  188. cell.offlineImageView.image = [UIImage imageNamed:image_offlinecrypto];
  189. else
  190. cell.offlineImageView.image = [UIImage imageNamed:image_offline];
  191. }
  192. // i am in local
  193. if ([_pageType isEqualToString:pageOfflineLocal]) {
  194. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  195. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  196. metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_localServerUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  197. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/.%@.ico", _localServerUrl, metadata.fileNamePrint]];
  198. if (!cell.fileImageView.image) {
  199. 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]];
  200. if (icon) {
  201. [CCGraphics saveIcoWithFileID:metadata.fileNamePrint image:icon writeToFile:[NSString stringWithFormat:@"%@/.%@.ico", _localServerUrl, metadata.fileNamePrint] copy:NO move:NO fromPath:nil toPath:nil];
  202. cell.fileImageView.image = icon;
  203. }
  204. }
  205. }
  206. // color and font
  207. if (metadata.cryptated) {
  208. cell.labelTitle.textColor = COLOR_ENCRYPTED;
  209. //nameLabel.font = RalewayLight(13.0f);
  210. cell.labelInfoFile.textColor = [UIColor blackColor];
  211. //detailLabel.font = RalewayLight(9.0f);
  212. } else {
  213. cell.labelTitle.textColor = COLOR_CLEAR;
  214. //nameLabel.font = RalewayLight(13.0f);
  215. cell.labelInfoFile.textColor = [UIColor blackColor];
  216. //detailLabel.font = RalewayLight(9.0f);
  217. }
  218. if (metadata.directory) {
  219. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  220. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  221. }
  222. // File name
  223. cell.labelTitle.text = metadata.fileNamePrint;
  224. cell.labelInfoFile.text = @"";
  225. // Immagine del file, se non c'è l'anteprima mettiamo quella standard
  226. if (cell.fileImageView.image == nil)
  227. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  228. // it's encrypted ???
  229. if (metadata.cryptated && [metadata.type isEqualToString:metadataType_model] == NO)
  230. cell.statusImageView.image = [UIImage imageNamed:image_lock];
  231. // it's in download mode
  232. if ([metadata.session length] > 0 && [metadata.session rangeOfString:@"download"].location != NSNotFound)
  233. cell.statusImageView.image = [UIImage imageNamed:image_attention];
  234. // text and length
  235. if (metadata.directory) {
  236. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  237. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  238. } else {
  239. NSString *date = [CCUtility dateDiff:metadata.date];
  240. NSString *length = [CCUtility transformedSize:metadata.size];
  241. if ([metadata.type isEqualToString:metadataType_model])
  242. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", date];
  243. if ([metadata.type isEqualToString:metadataType_file] || [metadata.type isEqualToString:metadataType_local])
  244. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", date, length];
  245. cell.accessoryType = UITableViewCellAccessoryNone;
  246. }
  247. return cell;
  248. }
  249. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  250. {
  251. // deselect row
  252. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  253. if ([_pageType isEqualToString:pageOfflineOffline]) {
  254. NSManagedObject *record = [dataSource objectAtIndex:indexPath.row];
  255. _metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", [record valueForKey:@"fileID"], app.activeAccount] context:nil];
  256. }
  257. if ([_pageType isEqualToString:pageOfflineLocal]) {
  258. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  259. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  260. _metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_localServerUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  261. }
  262. // if is in download [do not touch]
  263. if ([_metadata.session length] > 0 && [_metadata.session rangeOfString:@"download"].location != NSNotFound) return;
  264. if (([_metadata.type isEqualToString:metadataType_file] || [_metadata.type isEqualToString:metadataType_local]) && _metadata.directory == NO) {
  265. if ([self shouldPerformSegue])
  266. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  267. }
  268. if ([self.metadata.type isEqualToString:metadataType_model])
  269. [self openModel:self.metadata];
  270. if (_metadata.directory)
  271. [self performSegueDirectoryWithControlPasscode];
  272. }
  273. -(void)performSegueDirectoryWithControlPasscode
  274. {
  275. CCOfflinePageContent *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"OfflinePageContentViewController"];
  276. NSString *serverUrl;
  277. if ([_pageType isEqualToString:pageOfflineOffline] && !_localServerUrl) {
  278. serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:app.activeAccount];
  279. } else {
  280. serverUrl = _localServerUrl;
  281. }
  282. vc.localServerUrl = [CCUtility stringAppendServerUrl:serverUrl addServerUrl:_metadata.fileNameData];
  283. vc.pageType = _pageType;
  284. vc.titleViewControl = _metadata.fileNamePrint;
  285. [self.navigationController pushViewController:vc animated:YES];
  286. }
  287. #pragma --------------------------------------------------------------------------------------------
  288. #pragma mark ===== Navigation ====
  289. #pragma --------------------------------------------------------------------------------------------
  290. - (void)openModel:(CCMetadata *)metadata
  291. {
  292. UIViewController *viewController;
  293. BOOL isLocal = NO;
  294. if ([self.pageType isEqualToString:pageOfflineLocal])
  295. isLocal = YES;
  296. if ([metadata.model isEqualToString:@"cartadicredito"])
  297. viewController = [[CCCartaDiCredito alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  298. if ([metadata.model isEqualToString:@"bancomat"])
  299. viewController = [[CCBancomat alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  300. if ([metadata.model isEqualToString:@"contocorrente"])
  301. viewController = [[CCContoCorrente alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  302. if ([metadata.model isEqualToString:@"accountweb"])
  303. viewController = [[CCAccountWeb alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  304. if ([metadata.model isEqualToString:@"patenteguida"])
  305. viewController = [[CCPatenteGuida alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  306. if ([metadata.model isEqualToString:@"cartaidentita"])
  307. viewController = [[CCCartaIdentita alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  308. if ([metadata.model isEqualToString:@"passaporto"])
  309. viewController = [[CCPassaporto alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  310. if ([metadata.model isEqualToString:@"note"]) {
  311. viewController = [[CCNote alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  312. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  313. [self presentViewController:navigationController animated:YES completion:nil];
  314. } else {
  315. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  316. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  317. [self presentViewController:navigationController animated:YES completion:nil];
  318. }
  319. }
  320. - (BOOL)shouldPerformSegue
  321. {
  322. // if i am in background -> exit
  323. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  324. // if i am not window -> exit
  325. if (self.view.window == NO)
  326. return NO;
  327. // Collapsed but i am in detail -> exit
  328. if (self.splitViewController.isCollapsed)
  329. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  330. // Video in run -> exit
  331. if (self.detailViewController.photoBrowser.currentVideoPlayerViewController.isViewLoaded && self.detailViewController.photoBrowser.currentVideoPlayerViewController.view.window) return NO;
  332. return YES;
  333. }
  334. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  335. {
  336. id viewController = segue.destinationViewController;
  337. if ([viewController isKindOfClass:[UINavigationController class]]) {
  338. UINavigationController *nav = viewController;
  339. _detailViewController = (CCDetail *)nav.topViewController;
  340. } else {
  341. _detailViewController = segue.destinationViewController;
  342. }
  343. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  344. if ([self.pageType isEqualToString:pageOfflineOffline]) {
  345. for (CCMetadata *metadata in dataSource) {
  346. if ([metadata.typeFile isEqualToString:metadataTypeFile_image] || [metadata.typeFile isEqualToString:metadataTypeFile_video])
  347. [allRecordsDataSourceImagesVideos addObject:metadata];
  348. }
  349. }
  350. if ([self.pageType isEqualToString:pageOfflineLocal]) {
  351. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  352. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  353. for (NSString *fileName in dataSource) {
  354. CCMetadata *metadata = [CCMetadata new];
  355. metadata = [CCUtility insertFileSystemInMetadata:fileName directory:_localServerUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  356. if ([metadata.typeFile isEqualToString:metadataTypeFile_image] || [metadata.typeFile isEqualToString:metadataTypeFile_video])
  357. [allRecordsDataSourceImagesVideos addObject:metadata];
  358. }
  359. _detailViewController.sourceDirectoryLocal = YES;
  360. }
  361. _detailViewController.metadataDetail = _metadata;
  362. _detailViewController.dateFilterQuery = nil;
  363. _detailViewController.isCameraUpload = NO;
  364. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  365. [_detailViewController setTitle:_metadata.fileNamePrint];
  366. }
  367. @end