CCOfflinePageContent.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. // 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. [dataSource removeAllObjects];
  130. if ([_pageType isEqualToString:pageOfflineOffline]) {
  131. if (!_localServerUrl) {
  132. dataSource = (NSMutableArray*)[CCCoreData getHomeOfflineActiveAccount:app.activeAccount directoryUser:app.directoryUser];
  133. } else {
  134. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_localServerUrl activeAccount:app.activeAccount];
  135. NSArray *recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, directoryID] fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  136. CCSectionDataSource *sectionDataSource = [CCSection creataDataSourseSectionTableMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil replaceDateToExifDate:NO activeAccount:app.activeAccount];
  137. for (NSString *key in sectionDataSource.allRecordsDataSource)
  138. [dataSource insertObject:[sectionDataSource.allRecordsDataSource objectForKey:key] atIndex:0 ];
  139. }
  140. }
  141. if ([_pageType isEqualToString:pageOfflineLocal]) {
  142. NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:_localServerUrl error:nil];
  143. for (NSString *subpath in subpaths)
  144. if (![[subpath lastPathComponent] hasPrefix:@"."])
  145. [dataSource addObject:subpath];
  146. }
  147. [self.tableView reloadData];
  148. }
  149. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  150. {
  151. return 60;
  152. }
  153. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  154. {
  155. return 1;
  156. }
  157. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  158. {
  159. return [dataSource count];
  160. }
  161. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  162. {
  163. CCCellOffline *cell = (CCCellOffline *)[tableView dequeueReusableCellWithIdentifier:@"OfflineCell" forIndexPath:indexPath];
  164. CCMetadata *metadata;
  165. // Initialize
  166. cell.statusImageView.image = nil;
  167. cell.offlineImageView.image = nil;
  168. // change color selection
  169. UIView *selectionColor = [[UIView alloc] init];
  170. selectionColor.backgroundColor = COLOR_SELECT_BACKGROUND;
  171. cell.selectedBackgroundView = selectionColor;
  172. // i am in Offline
  173. if ([_pageType isEqualToString:pageOfflineOffline]) {
  174. metadata = [dataSource objectAtIndex:indexPath.row];
  175. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  176. if (metadata.cryptated)
  177. cell.offlineImageView.image = [UIImage imageNamed:image_offlinecrypto];
  178. else
  179. cell.offlineImageView.image = [UIImage imageNamed:image_offline];
  180. }
  181. // i am in local
  182. if ([_pageType isEqualToString:pageOfflineLocal]) {
  183. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  184. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  185. metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_localServerUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  186. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/.%@.ico", _localServerUrl, metadata.fileNamePrint]];
  187. if (!cell.fileImageView.image) {
  188. 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]];
  189. if (icon) {
  190. [CCGraphics saveIcoWithFileID:metadata.fileNamePrint image:icon writeToFile:[NSString stringWithFormat:@"%@/.%@.ico", _localServerUrl, metadata.fileNamePrint] copy:NO move:NO fromPath:nil toPath:nil];
  191. cell.fileImageView.image = icon;
  192. }
  193. }
  194. }
  195. // color and font
  196. if (metadata.cryptated) {
  197. cell.labelTitle.textColor = COLOR_ENCRYPTED;
  198. //nameLabel.font = RalewayLight(13.0f);
  199. cell.labelInfoFile.textColor = [UIColor blackColor];
  200. //detailLabel.font = RalewayLight(9.0f);
  201. } else {
  202. cell.labelTitle.textColor = COLOR_CLEAR;
  203. //nameLabel.font = RalewayLight(13.0f);
  204. cell.labelInfoFile.textColor = [UIColor blackColor];
  205. //detailLabel.font = RalewayLight(9.0f);
  206. }
  207. if (metadata.directory) {
  208. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  209. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  210. }
  211. // File name
  212. cell.labelTitle.text = metadata.fileNamePrint;
  213. cell.labelInfoFile.text = @"";
  214. // Immagine del file, se non c'è l'anteprima mettiamo quella standard
  215. if (cell.fileImageView.image == nil)
  216. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  217. // it's encrypted ???
  218. if (metadata.cryptated && [metadata.type isEqualToString:metadataType_model] == NO)
  219. cell.statusImageView.image = [UIImage imageNamed:image_lock];
  220. // it's in download mode
  221. if ([metadata.session length] > 0 && [metadata.session rangeOfString:@"download"].location != NSNotFound)
  222. cell.statusImageView.image = [UIImage imageNamed:image_attention];
  223. // text and length
  224. if (metadata.directory) {
  225. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  226. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  227. } else {
  228. NSString *date = [CCUtility dateDiff:metadata.date];
  229. NSString *length = [CCUtility transformedSize:metadata.size];
  230. if ([metadata.type isEqualToString:metadataType_model])
  231. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", date];
  232. if ([metadata.type isEqualToString:metadataType_file] || [metadata.type isEqualToString:metadataType_local])
  233. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@, %@", date, length];
  234. cell.accessoryType = UITableViewCellAccessoryNone;
  235. }
  236. return cell;
  237. }
  238. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  239. {
  240. // deselect row
  241. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  242. if ([_pageType isEqualToString:pageOfflineOffline]) {
  243. NSManagedObject *record = [dataSource objectAtIndex:indexPath.row];
  244. _metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", [record valueForKey:@"fileID"], app.activeAccount] context:nil];
  245. }
  246. if ([_pageType isEqualToString:pageOfflineLocal]) {
  247. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  248. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  249. _metadata = [CCUtility insertFileSystemInMetadata:[dataSource objectAtIndex:indexPath.row] directory:_localServerUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  250. }
  251. // if is in download [do not touch]
  252. if ([_metadata.session length] > 0 && [_metadata.session rangeOfString:@"download"].location != NSNotFound) return;
  253. if (([_metadata.type isEqualToString:metadataType_file] || [_metadata.type isEqualToString:metadataType_local]) && _metadata.directory == NO) {
  254. if ([self shouldPerformSegue])
  255. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  256. }
  257. if ([self.metadata.type isEqualToString:metadataType_model])
  258. [self openModel:self.metadata];
  259. if (_metadata.directory)
  260. [self performSegueDirectoryWithControlPasscode];
  261. }
  262. -(void)performSegueDirectoryWithControlPasscode
  263. {
  264. CCOfflinePageContent *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"OfflinePageContentViewController"];
  265. NSString *serverUrl;
  266. if ([_pageType isEqualToString:pageOfflineOffline] && !_localServerUrl) {
  267. serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:app.activeAccount];
  268. } else {
  269. serverUrl = _localServerUrl;
  270. }
  271. vc.localServerUrl = [CCUtility stringAppendServerUrl:serverUrl addServerUrl:_metadata.fileNameData];
  272. vc.pageType = _pageType;
  273. vc.titleViewControl = _metadata.fileNamePrint;
  274. [self.navigationController pushViewController:vc animated:YES];
  275. }
  276. #pragma --------------------------------------------------------------------------------------------
  277. #pragma mark ===== Navigation ====
  278. #pragma --------------------------------------------------------------------------------------------
  279. - (void)openModel:(CCMetadata *)metadata
  280. {
  281. UIViewController *viewController;
  282. BOOL isLocal = NO;
  283. if ([self.pageType isEqualToString:pageOfflineLocal])
  284. isLocal = YES;
  285. if ([metadata.model isEqualToString:@"cartadicredito"])
  286. viewController = [[CCCartaDiCredito alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  287. if ([metadata.model isEqualToString:@"bancomat"])
  288. viewController = [[CCBancomat alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  289. if ([metadata.model isEqualToString:@"contocorrente"])
  290. viewController = [[CCContoCorrente alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  291. if ([metadata.model isEqualToString:@"accountweb"])
  292. viewController = [[CCAccountWeb alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  293. if ([metadata.model isEqualToString:@"patenteguida"])
  294. viewController = [[CCPatenteGuida alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  295. if ([metadata.model isEqualToString:@"cartaidentita"])
  296. viewController = [[CCCartaIdentita alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  297. if ([metadata.model isEqualToString:@"passaporto"])
  298. viewController = [[CCPassaporto alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  299. if ([metadata.model isEqualToString:@"note"]) {
  300. viewController = [[CCNote alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid rev:metadata.rev fileID:metadata.fileID modelReadOnly:true isLocal:isLocal];
  301. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  302. [self presentViewController:navigationController animated:YES completion:nil];
  303. } else {
  304. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  305. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  306. [self presentViewController:navigationController animated:YES completion:nil];
  307. }
  308. }
  309. - (BOOL)shouldPerformSegue
  310. {
  311. // if i am in background -> exit
  312. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  313. // if i am not window -> exit
  314. if (self.view.window == NO)
  315. return NO;
  316. // Collapsed but i am in detail -> exit
  317. if (self.splitViewController.isCollapsed)
  318. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  319. // Video in run -> exit
  320. if (self.detailViewController.photoBrowser.currentVideoPlayerViewController.isViewLoaded && self.detailViewController.photoBrowser.currentVideoPlayerViewController.view.window) return NO;
  321. return YES;
  322. }
  323. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  324. {
  325. id viewController = segue.destinationViewController;
  326. if ([viewController isKindOfClass:[UINavigationController class]]) {
  327. UINavigationController *nav = viewController;
  328. _detailViewController = (CCDetail *)nav.topViewController;
  329. } else {
  330. _detailViewController = segue.destinationViewController;
  331. }
  332. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  333. if ([self.pageType isEqualToString:pageOfflineOffline]) {
  334. for (CCMetadata *metadata in dataSource) {
  335. if ([metadata.typeFile isEqualToString:metadataTypeFile_image] || [metadata.typeFile isEqualToString:metadataTypeFile_video])
  336. [allRecordsDataSourceImagesVideos addObject:metadata];
  337. }
  338. }
  339. if ([self.pageType isEqualToString:pageOfflineLocal]) {
  340. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:app.activeAccount];
  341. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud];
  342. for (NSString *fileName in dataSource) {
  343. CCMetadata *metadata = [CCMetadata new];
  344. metadata = [CCUtility insertFileSystemInMetadata:fileName directory:_localServerUrl activeAccount:app.activeAccount cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath];
  345. if ([metadata.typeFile isEqualToString:metadataTypeFile_image] || [metadata.typeFile isEqualToString:metadataTypeFile_video])
  346. [allRecordsDataSourceImagesVideos addObject:metadata];
  347. }
  348. _detailViewController.sourceDirectoryLocal = YES;
  349. }
  350. _detailViewController.metadataDetail = _metadata;
  351. _detailViewController.dateFilterQuery = nil;
  352. _detailViewController.isCameraUpload = NO;
  353. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  354. [_detailViewController setTitle:_metadata.fileNamePrint];
  355. }
  356. @end