CCFavorites.m 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. //
  2. // CCFavorites.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 16/01/17.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCFavorites.h"
  24. #import "AppDelegate.h"
  25. #import "CCSynchronize.h"
  26. #import "NCBridgeSwift.h"
  27. @interface CCFavorites ()
  28. {
  29. AppDelegate *appDelegate;
  30. // Automatic Upload Folder
  31. NSString *autoUploadFileName;
  32. NSString *autoUploadDirectory;
  33. // Datasource
  34. CCSectionDataSourceMetadata *sectionDataSource;
  35. }
  36. @end
  37. @implementation CCFavorites
  38. #pragma --------------------------------------------------------------------------------------------
  39. #pragma mark ===== Init =====
  40. #pragma --------------------------------------------------------------------------------------------
  41. - (id)initWithCoder:(NSCoder *)aDecoder
  42. {
  43. if (self = [super initWithCoder:aDecoder]) {
  44. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  45. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  46. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  47. appDelegate.activeFavorites = self;
  48. }
  49. return self;
  50. }
  51. - (void)viewDidLoad
  52. {
  53. [super viewDidLoad];
  54. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellMain" bundle:nil] forCellReuseIdentifier:@"CellMain"];
  55. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellMainTransfer" bundle:nil] forCellReuseIdentifier:@"CellMainTransfer"];
  56. // Metadata
  57. self.metadata = [tableMetadata new];
  58. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  59. self.tableView.separatorColor = [NCBrandColor sharedInstance].seperator;
  60. self.tableView.emptyDataSetDelegate = self;
  61. self.tableView.emptyDataSetSource = self;
  62. self.tableView.delegate = self;
  63. // calculate _serverUrl
  64. if (!_serverUrl)
  65. _serverUrl = nil;
  66. // Title
  67. if (_titleViewControl)
  68. self.title = _titleViewControl;
  69. else
  70. self.title = NSLocalizedString(@"_favorites_", nil);
  71. }
  72. // Apparirà
  73. - (void)viewWillAppear:(BOOL)animated
  74. {
  75. [super viewWillAppear:animated];
  76. // Color
  77. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  78. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  79. // Plus Button
  80. [appDelegate plusButtonVisibile:true];
  81. [self reloadDatasource];
  82. }
  83. // E' arrivato
  84. - (void)viewDidAppear:(BOOL)animated
  85. {
  86. [super viewDidAppear:animated];
  87. // Active Main
  88. appDelegate.activeFavorites = self;
  89. }
  90. - (void)changeTheming
  91. {
  92. if (self.isViewLoaded && self.view.window)
  93. [appDelegate changeTheming:self];
  94. // Reload Table View
  95. [self.tableView reloadData];
  96. }
  97. #pragma --------------------------------------------------------------------------------------------
  98. #pragma mark ==== DZNEmptyDataSetSource ====
  99. #pragma --------------------------------------------------------------------------------------------
  100. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  101. {
  102. return [NCBrandColor sharedInstance].backgroundView;
  103. }
  104. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  105. {
  106. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favoriteNoFiles"] multiplier:2 color:[NCBrandColor sharedInstance].yellowFavorite];
  107. }
  108. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  109. {
  110. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_favorite_no_files_", nil)];
  111. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  112. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  113. }
  114. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  115. {
  116. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_favorite_view_", nil)];
  117. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  118. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  119. paragraph.alignment = NSTextAlignmentCenter;
  120. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  121. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  122. }
  123. #pragma --------------------------------------------------------------------------------------------
  124. #pragma mark ===== Delete =====
  125. #pragma--------------------------------------------------------------------------------------------
  126. - (void)deleteFile:(NSArray *)metadatas e2ee:(BOOL)e2ee
  127. {
  128. NSInteger numDelete = metadatas.count;
  129. __block NSInteger cont = 0;
  130. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  131. for (tableMetadata *metadata in metadatas) {
  132. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  133. if (!serverUrl)
  134. continue;
  135. [ocNetworking deleteFileOrFolder:metadata.fileName serverUrl:serverUrl completion:^(NSString *message, NSInteger errorCode) {
  136. if (errorCode == 0 || errorCode == 404) {
  137. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID] error:nil];
  138. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID] clearDateReadDirectoryID:metadata.directoryID];
  139. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  140. [[NCManageDatabase sharedInstance] deletePhotosWithFileID:metadata.fileID];
  141. [[appDelegate activePhotos].fileIDHide addObject:metadata.fileID];
  142. // Directory ?
  143. if (metadata.directory) {
  144. [[NCManageDatabase sharedInstance] deleteDirectoryAndSubDirectoryWithServerUrl:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName]];
  145. }
  146. // E2EE (if exists the record)
  147. if (e2ee) {
  148. [[NCManageDatabase sharedInstance] deleteE2eEncryptionWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileNameIdentifier == %@", metadata.account, serverUrl, metadata.fileName]];
  149. }
  150. }
  151. if (++cont == numDelete) {
  152. if (e2ee) {
  153. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  154. [[NCNetworkingEndToEnd sharedManager] rebuildAndSendEndToEndMetadataOnServerUrl:serverUrl account:appDelegate.activeAccount user:appDelegate.activeUser userID:appDelegate.activeUserID password:appDelegate.activePassword url:appDelegate.activeUrl];
  155. dispatch_async(dispatch_get_main_queue(), ^{
  156. // ONLY for this View
  157. [self reloadDatasource];
  158. });
  159. });
  160. } else {
  161. // ONLY for this View
  162. [self reloadDatasource];;
  163. }
  164. }
  165. }];
  166. }
  167. }
  168. #pragma --------------------------------------------------------------------------------------------
  169. #pragma mark ===== Favorite =====
  170. #pragma--------------------------------------------------------------------------------------------
  171. - (void)addFavoriteFolder:(NSString *)serverUrl
  172. {
  173. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  174. if (!directoryID) return;
  175. NSString *selector;
  176. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  177. metadataNet.action = actionReadFolder;
  178. metadataNet.depth = @"1";
  179. metadataNet.directoryID = directoryID;
  180. if ([CCUtility getFavoriteOffline])
  181. selector = selectorReadFolderWithDownload;
  182. else
  183. selector = selectorReadFolder;
  184. metadataNet.selector = selector;
  185. metadataNet.serverUrl = serverUrl;
  186. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:[CCSynchronize sharedSynchronize] metadataNet:metadataNet];
  187. }
  188. - (void)settingFavorite:(tableMetadata *)metadata favorite:(BOOL)favorite
  189. {
  190. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  191. if(!serverUrl)
  192. return;
  193. NSString *fileNameServerUrl = [CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:serverUrl activeUrl:appDelegate.activeUrl];
  194. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  195. [ocNetworking settingFavorite:fileNameServerUrl favorite:favorite completion:^(NSString *message, NSInteger errorCode) {
  196. if (errorCode == 0) {
  197. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadata.fileID favorite:favorite];
  198. [self reloadDatasource];
  199. } else {
  200. if (errorCode == kOCErrorServerUnauthorized)
  201. [appDelegate openLoginView:self loginType:k_login_Modify_Password selector:k_intro_login];
  202. }
  203. }];
  204. }
  205. #pragma --------------------------------------------------------------------------------------------
  206. #pragma mark ===== listingFavorites =====
  207. #pragma--------------------------------------------------------------------------------------------
  208. - (void)listingFavorites
  209. {
  210. // test
  211. if (appDelegate.activeAccount.length == 0)
  212. return;
  213. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  214. [ocNetworking listingFavorites:@"" account:appDelegate.activeAccount success:^(NSArray *metadatas) {
  215. NSString *father = @"";
  216. NSMutableArray *filesEtag = [NSMutableArray new];
  217. for (tableMetadata *metadata in metadatas) {
  218. // insert for test NOT favorite
  219. [filesEtag addObject:metadata.fileID];
  220. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  221. if (!serverUrl)
  222. continue;
  223. NSString *serverUrlSon = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName];
  224. if (![serverUrlSon containsString:father]) {
  225. if (metadata.directory) {
  226. if ([CCUtility getFavoriteOffline])
  227. [[CCSynchronize sharedSynchronize] readFileForFolder:metadata.fileName serverUrl:serverUrl selector:selectorReadFileFolderWithDownload];
  228. else
  229. [[CCSynchronize sharedSynchronize] readFileForFolder:metadata.fileName serverUrl:serverUrl selector:selectorReadFileFolder];
  230. } else {
  231. if ([CCUtility getFavoriteOffline])
  232. [[CCSynchronize sharedSynchronize] readFile:metadata selector:selectorReadFileWithDownload];
  233. else
  234. [[CCSynchronize sharedSynchronize] readFile:metadata selector:selectorReadFile];
  235. }
  236. father = serverUrlSon;
  237. }
  238. }
  239. // Verify remove favorite
  240. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", appDelegate.activeAccount] sorted:nil ascending:NO];
  241. for (tableMetadata *metadata in allRecordFavorite)
  242. if (![filesEtag containsObject:metadata.fileID])
  243. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadata.fileID favorite:NO];
  244. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"clearDateReadDataSource" object:nil];
  245. } failure:^(NSString *message, NSInteger errorCode) {
  246. NSLog(@"[LOG] Listing Favorites failure error %d, %@", (int)errorCode, message);
  247. }];
  248. }
  249. #pragma --------------------------------------------------------------------------------------------
  250. #pragma mark ==== Download Thumbnail ====
  251. #pragma --------------------------------------------------------------------------------------------
  252. - (void)downloadThumbnail:(tableMetadata *)metadata serverUrl:(NSString *)serverUrl indexPath:(NSIndexPath *)indexPath
  253. {
  254. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  255. [ocNetworking downloadThumbnailWithDimOfThumbnail:@"m" fileID:metadata.fileID fileNamePath:[CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:serverUrl activeUrl:appDelegate.activeUrl] fileNameView:metadata.fileNameView completion:^(NSString *message, NSInteger errorCode) {
  256. if(errorCode == 0 && [[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]] && [[NCMainCommon sharedInstance] isValidIndexPath:indexPath tableView:self.tableView]) {
  257. [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  258. }
  259. }];
  260. }
  261. #pragma --------------------------------------------------------------------------------------------
  262. #pragma mark ==== Download <Delegate> ====
  263. #pragma --------------------------------------------------------------------------------------------
  264. - (void)downloadStart:(NSString *)fileID account:(NSString *)account task:(NSURLSessionDownloadTask *)task serverUrl:(NSString *)serverUrl
  265. {
  266. [self reloadDatasource];
  267. [appDelegate updateApplicationIconBadgeNumber];
  268. }
  269. - (void)downloadFileSuccessFailure:(NSString *)fileName fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector errorMessage:(NSString *)errorMessage errorCode:(NSInteger)errorCode
  270. {
  271. if (errorCode == 0) {
  272. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  273. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_compress]) {
  274. [self openWith:metadata];
  275. } else if ([metadata.typeFile isEqualToString: k_metadataTypeFile_unknown]) {
  276. [self openWith:metadata];
  277. } else {
  278. if ([self shouldPerformSegue])
  279. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  280. }
  281. } else {
  282. // File do not exists on server, remove in local
  283. if (errorCode == kOCErrorServerPathNotFound || errorCode == kCFURLErrorBadServerResponse) {
  284. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageFileID:fileID] error:nil];
  285. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID] clearDateReadDirectoryID:nil];
  286. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  287. [[NCManageDatabase sharedInstance] deletePhotosWithFileID:fileID];
  288. [appDelegate.activePhotos.fileIDHide addObject:fileID];
  289. }
  290. [self reloadDatasource];
  291. [appDelegate.activeTransfers reloadDatasource];
  292. }
  293. }
  294. - (void)openWith:(tableMetadata *)metadata
  295. {
  296. if ([CCUtility fileProviderStorageExists:metadata.fileID fileName:metadata.fileNameView]) {
  297. NSURL *url = [NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileName:metadata.fileNameView]];
  298. _docController = [UIDocumentInteractionController interactionControllerWithURL:url];
  299. _docController.delegate = self;
  300. [_docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  301. }
  302. }
  303. - (void)tapActionConnectionMounted:(UITapGestureRecognizer *)tapGesture
  304. {
  305. CGPoint location = [tapGesture locationInView:self.tableView];
  306. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  307. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  308. if (metadata)
  309. [appDelegate.activeMain openWindowShare:metadata];
  310. }
  311. #pragma --------------------------------------------------------------------------------------------
  312. #pragma mark ===== Progress & Task Button =====
  313. #pragma --------------------------------------------------------------------------------------------
  314. - (void)triggerProgressTask:(NSNotification *)notification
  315. {
  316. [[NCMainCommon sharedInstance] triggerProgressTask:notification sectionDataSourceFileIDIndexPath:sectionDataSource.fileIDIndexPath tableView:self.tableView];
  317. }
  318. - (void)cancelTaskButton:(id)sender withEvent:(UIEvent *)event
  319. {
  320. UITouch *touch = [[event allTouches] anyObject];
  321. CGPoint location = [touch locationInView:self.tableView];
  322. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  323. if ([[NCMainCommon sharedInstance] isValidIndexPath:indexPath tableView:self.tableView]) {
  324. tableMetadata *metadataSection = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  325. if (metadataSection) {
  326. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadataSection.fileID]];
  327. if (metadata)
  328. [[NCMainCommon sharedInstance] cancelTransferMetadata:metadata reloadDatasource:true];
  329. }
  330. }
  331. }
  332. - (void)cancelAllTask:(id)sender
  333. {
  334. CGPoint location = [sender locationInView:self.tableView];
  335. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  336. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  337. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_all_task_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  338. [[NCMainCommon sharedInstance] cancelAllTransfer];
  339. }]];
  340. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]];
  341. alertController.popoverPresentationController.sourceView = self.view;
  342. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  343. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  344. [alertController.view layoutIfNeeded];
  345. [self presentViewController:alertController animated:YES completion:nil];
  346. }
  347. #pragma mark -
  348. #pragma --------------------------------------------------------------------------------------------
  349. #pragma mark ===== menu action : Favorite, More, Delete [swipe] =====
  350. #pragma --------------------------------------------------------------------------------------------
  351. - (BOOL)canOpenMenuAction:(tableMetadata *)metadata
  352. {
  353. return YES;
  354. }
  355. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction
  356. {
  357. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  358. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  359. return [self canOpenMenuAction:metadata];
  360. }
  361. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell tappedButtonAtIndex:(NSInteger)index direction:(MGSwipeDirection)direction fromExpansion:(BOOL)fromExpansion
  362. {
  363. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  364. if (direction == MGSwipeDirectionRightToLeft) {
  365. [self actionDelete:indexPath];
  366. }
  367. if (direction == MGSwipeDirectionLeftToRight) {
  368. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  369. [self settingFavorite:metadata favorite:NO];
  370. }
  371. return YES;
  372. }
  373. - (void)actionDelete:(NSIndexPath *)indexPath
  374. {
  375. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  376. tableLocalFile *localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  377. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  378. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  379. [self deleteFile:[[NSArray alloc] initWithObjects:metadata, nil] e2ee:false];
  380. }]];
  381. if (localFile) {
  382. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_local_file_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  383. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  384. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID] error:nil];
  385. [self reloadDatasource];
  386. }]];
  387. }
  388. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  389. }]];
  390. alertController.popoverPresentationController.sourceView = self.view;
  391. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  392. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  393. [alertController.view layoutIfNeeded];
  394. [self presentViewController:alertController animated:YES completion:nil];
  395. }
  396. - (void)actionMore:(UITapGestureRecognizer *)gestureRecognizer
  397. {
  398. CGPoint touch = [gestureRecognizer locationInView:self.tableView];
  399. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touch];
  400. UIImage *iconHeader;
  401. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  402. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.tabBarController.view title:nil];
  403. actionSheet.animationDuration = 0.2;
  404. actionSheet.buttonHeight = 50.0;
  405. actionSheet.cancelButtonHeight = 50.0f;
  406. actionSheet.separatorHeight = 5.0f;
  407. actionSheet.automaticallyTintButtonImages = @(NO);
  408. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].encrypted };
  409. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  410. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:17], NSForegroundColorAttributeName:[UIColor blackColor] };
  411. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor darkGrayColor] };
  412. actionSheet.separatorColor = [NCBrandColor sharedInstance].seperator;
  413. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  414. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  415. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]]) {
  416. iconHeader = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  417. } else {
  418. if (metadata.directory)
  419. iconHeader = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement];
  420. else
  421. iconHeader = [UIImage imageNamed:metadata.iconName];
  422. }
  423. [actionSheet addButtonWithTitle: metadata.fileNameView image: iconHeader backgroundColor: [NCBrandColor sharedInstance].tabBar height: 50.0 type: AHKActionSheetButtonTypeDisabled handler: nil
  424. ];
  425. // Favorite : ONLY root
  426. if (_serverUrl == nil) {
  427. [actionSheet addButtonWithTitle: NSLocalizedString(@"_remove_favorites_", nil)
  428. image: [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] multiplier:2 color:[NCBrandColor sharedInstance].yellowFavorite]
  429. backgroundColor: [NCBrandColor sharedInstance].backgroundView
  430. height: 50.0
  431. type: AHKActionSheetButtonTypeDefault
  432. handler: ^(AHKActionSheet *as) {
  433. [self settingFavorite:metadata favorite:NO];
  434. }];
  435. }
  436. // Share
  437. [actionSheet addButtonWithTitle:NSLocalizedString(@"_share_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"share"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement] backgroundColor:[NCBrandColor sharedInstance].backgroundView height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  438. [appDelegate.activeMain openWindowShare:metadata];
  439. }];
  440. // NO Directory
  441. if (metadata.directory == NO) {
  442. [actionSheet addButtonWithTitle:NSLocalizedString(@"_open_in_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"openFile"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement] backgroundColor:[NCBrandColor sharedInstance].backgroundView height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  443. [self.tableView setEditing:NO animated:YES];
  444. [self openWith:metadata];
  445. }];
  446. }
  447. // Delete
  448. [actionSheet addButtonWithTitle:NSLocalizedString(@"_delete_", nil)
  449. image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"delete"] multiplier:2 color:[UIColor redColor]]
  450. backgroundColor:[NCBrandColor sharedInstance].backgroundView
  451. height:50.0
  452. type:AHKActionSheetButtonTypeDestructive
  453. handler:^(AHKActionSheet *as) {
  454. [self actionDelete:indexPath];
  455. }];
  456. [actionSheet show];
  457. }
  458. #pragma --------------------------------------------------------------------------------------------
  459. #pragma mark ==== Table ====
  460. #pragma --------------------------------------------------------------------------------------------
  461. - (tableMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  462. {
  463. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  464. return metadata;
  465. }
  466. - (void)readFolder:(NSString *)serverUrl
  467. {
  468. [self reloadDatasource];
  469. }
  470. - (void)reloadDatasource
  471. {
  472. NSMutableArray *metadatas = [NSMutableArray new];
  473. NSArray *recordsTableMetadata ;
  474. NSString *sorted = [CCUtility getOrderSettings];
  475. if ([sorted isEqualToString:@"fileName"])
  476. sorted = @"fileName";
  477. if (!_serverUrl) {
  478. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", appDelegate.activeAccount] sorted:sorted ascending:[CCUtility getAscendingSettings]];
  479. } else {
  480. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  481. if (directoryID)
  482. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"directoryID == %@", directoryID] sorted:sorted ascending:[CCUtility getAscendingSettings]];
  483. }
  484. sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil fileIDHide:nil activeAccount:appDelegate.activeAccount];
  485. NSArray *fileIDs = [sectionDataSource.sectionArrayRow objectForKey:@"_none_"];
  486. for (NSString *fileID in fileIDs)
  487. [metadatas addObject:[sectionDataSource.allRecordsDataSource objectForKey:fileID]];
  488. // get auto upload folder
  489. autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  490. autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:appDelegate.activeUrl];
  491. [self.tableView reloadData];
  492. }
  493. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  494. {
  495. return 60;
  496. }
  497. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  498. {
  499. return 1;
  500. }
  501. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  502. {
  503. return [[sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:section]] count];
  504. }
  505. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  506. {
  507. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  508. if (metadata == nil || [[NCManageDatabase sharedInstance] isTableInvalidated:metadata]) {
  509. return [CCCellMain new];
  510. }
  511. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  512. if (serverUrl == nil) {
  513. return [CCCellMain new];
  514. }
  515. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, serverUrl]];
  516. if (directory == nil) {
  517. return [CCCellMain new];
  518. }
  519. tableMetadata *metadataFolder = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", directory.fileID]];
  520. // Download thumbnail
  521. if (metadata.thumbnailExists && ![[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]]) {
  522. [self downloadThumbnail:metadata serverUrl:serverUrl indexPath:indexPath];
  523. }
  524. UITableViewCell *cell = [[NCMainCommon sharedInstance] cellForRowAtIndexPath:indexPath tableView:tableView metadata:metadata metadataFolder:metadataFolder serverUrl:self.serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory];
  525. // NORMAL - > MAIN
  526. if ([cell isKindOfClass:[CCCellMain class]]) {
  527. // More
  528. if ([self canOpenMenuAction:metadata]) {
  529. UITapGestureRecognizer *tapMore = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionMore:)];
  530. [tapMore setNumberOfTapsRequired:1];
  531. ((CCCellMain *)cell).more.userInteractionEnabled = YES;
  532. [((CCCellMain *)cell).more addGestureRecognizer:tapMore];
  533. }
  534. // MGSwipeButton
  535. ((CCCellMain *)cell).delegate = self;
  536. // LEFT : configure ONLY Root Favorites : Remove file/folder Favorites
  537. if (_serverUrl == nil) {
  538. ((CCCellMain *)cell).leftButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] multiplier:2 color:[UIColor whiteColor]] backgroundColor:[NCBrandColor sharedInstance].yellowFavorite padding:25]];
  539. ((CCCellMain *)cell).leftExpansion.buttonIndex = 0;
  540. ((CCCellMain *)cell).leftExpansion.fillOnTrigger = NO;
  541. //centerIconOverText
  542. MGSwipeButton *favoriteButton = (MGSwipeButton *)[((CCCellMain *)cell).leftButtons objectAtIndex:0];
  543. [favoriteButton centerIconOverText];
  544. }
  545. // RIGHT
  546. ((CCCellMain *)cell).rightButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"delete"] multiplier:2 color:[UIColor whiteColor]] backgroundColor:[UIColor redColor] padding:25]];
  547. ((CCCellMain *)cell).rightExpansion.buttonIndex = 0;
  548. ((CCCellMain *)cell).rightExpansion.fillOnTrigger = NO;
  549. //centerIconOverText
  550. MGSwipeButton *deleteButton = (MGSwipeButton *)[((CCCellMain *)cell).rightButtons objectAtIndex:0];
  551. [deleteButton centerIconOverText];
  552. }
  553. // TRANSFER
  554. if ([cell isKindOfClass:[CCCellMainTransfer class]]) {
  555. // gesture Transfer
  556. [((CCCellMainTransfer *)cell).transferButton.stopButton addTarget:self action:@selector(cancelTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  557. UILongPressGestureRecognizer *stopLongGesture = [UILongPressGestureRecognizer new];
  558. [stopLongGesture addTarget:self action:@selector(cancelAllTask:)];
  559. [((CCCellMainTransfer *)cell).transferButton.stopButton addGestureRecognizer:stopLongGesture];
  560. }
  561. return cell;
  562. }
  563. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  564. {
  565. // deselect row
  566. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  567. self.metadata = [self setSelfMetadataFromIndexPath:indexPath];
  568. // if is in download [do not touch]
  569. if (self.metadata.status == k_metadataStatusWaitDownload || self.metadata.status == k_metadataStatusInDownload || self.metadata.status == k_metadataStatusDownloading)
  570. return;
  571. // File
  572. if (self.metadata.directory == NO) {
  573. // File do not exists
  574. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:self.metadata.directoryID];
  575. if (serverUrl) {
  576. if ([CCUtility fileProviderStorageExists:self.metadata.fileID fileName:self.metadata.fileNameView]) {
  577. [self downloadFileSuccessFailure:self.metadata.fileName fileID:self.metadata.fileID serverUrl:serverUrl selector:selectorLoadFileView errorMessage:@"" errorCode:0];
  578. } else {
  579. self.metadata.session = k_download_session;
  580. self.metadata.sessionError = @"";
  581. self.metadata.sessionSelector = selectorLoadFileView;
  582. self.metadata.status = k_metadataStatusWaitDownload;
  583. // Add Metadata for Download
  584. tableMetadata *metadata = [[NCManageDatabase sharedInstance] addMetadata:self.metadata];
  585. [[CCNetworking sharedNetworking] downloadFile:metadata taskStatus:k_taskStatusResume delegate:self];
  586. }
  587. }
  588. }
  589. // Directory
  590. if (self.metadata.directory)
  591. [self performSegueDirectoryWithControlPasscode];
  592. }
  593. -(void)performSegueDirectoryWithControlPasscode
  594. {
  595. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  596. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:self.metadata.directoryID];
  597. if (serverUrl) {
  598. vc.serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:self.metadata.fileName];
  599. vc.titleViewControl = self.metadata.fileNameView;
  600. [self.navigationController pushViewController:vc animated:YES];
  601. }
  602. }
  603. #pragma --------------------------------------------------------------------------------------------
  604. #pragma mark ===== Navigation ====
  605. #pragma --------------------------------------------------------------------------------------------
  606. - (BOOL)shouldPerformSegue
  607. {
  608. // if i am in background -> exit
  609. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  610. // if i am not window -> exit
  611. if (self.view.window == NO)
  612. return NO;
  613. // Collapsed but i am in detail -> exit
  614. if (self.splitViewController.isCollapsed)
  615. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  616. return YES;
  617. }
  618. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  619. {
  620. id viewController = segue.destinationViewController;
  621. if ([viewController isKindOfClass:[UINavigationController class]]) {
  622. UINavigationController *nav = viewController;
  623. _detailViewController = (CCDetail *)nav.topViewController;
  624. } else {
  625. _detailViewController = segue.destinationViewController;
  626. }
  627. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  628. for (NSString *fileID in sectionDataSource.allFileID) {
  629. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:fileID];
  630. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video] || [metadata.typeFile isEqualToString: k_metadataTypeFile_audio])
  631. [allRecordsDataSourceImagesVideos addObject:metadata];
  632. }
  633. _detailViewController.metadataDetail = self.metadata;
  634. _detailViewController.dateFilterQuery = nil;
  635. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  636. [_detailViewController setTitle:self.metadata.fileNameView];
  637. }
  638. @end