CCFavorites.m 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. //
  2. // CCFavorites.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 16/01/17.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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. UIDocumentInteractionController *docController;
  34. // Datasource
  35. CCSectionDataSourceMetadata *sectionDataSource;
  36. }
  37. @end
  38. @implementation CCFavorites
  39. #pragma --------------------------------------------------------------------------------------------
  40. #pragma mark ===== Init =====
  41. #pragma --------------------------------------------------------------------------------------------
  42. - (id)initWithCoder:(NSCoder *)aDecoder
  43. {
  44. if (self = [super initWithCoder:aDecoder]) {
  45. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  46. appDelegate.activeFavorites = self;
  47. }
  48. return self;
  49. }
  50. - (void)viewDidLoad
  51. {
  52. [super viewDidLoad];
  53. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellMain" bundle:nil] forCellReuseIdentifier:@"CellMain"];
  54. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellMainTransfer" bundle:nil] forCellReuseIdentifier:@"CellMainTransfer"];
  55. // Metadata
  56. self.metadata = [tableMetadata new];
  57. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  58. self.tableView.emptyDataSetDelegate = self;
  59. self.tableView.emptyDataSetSource = self;
  60. self.tableView.delegate = self;
  61. // Register for 3D Touch Previewing if available
  62. if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable))
  63. {
  64. [self registerForPreviewingWithDelegate:self sourceView:self.view];
  65. }
  66. // calculate _serverUrl
  67. if (!_serverUrl)
  68. _serverUrl = nil;
  69. // Title
  70. if (_titleViewControl)
  71. self.title = _titleViewControl;
  72. else
  73. self.title = NSLocalizedString(@"_favorites_", nil);
  74. // Notification
  75. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  76. // changeTheming
  77. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  78. [self changeTheming];
  79. // Query data source
  80. [self queryDatasource];
  81. }
  82. - (void)viewDidAppear:(BOOL)animated
  83. {
  84. [super viewDidAppear:animated];
  85. // Active Main
  86. appDelegate.activeFavorites = self;
  87. [self reloadDatasource:nil action:k_action_NULL];
  88. }
  89. - (void)changeTheming
  90. {
  91. [appDelegate changeTheming:self tableView:self.tableView collectionView:nil form:false];
  92. }
  93. #pragma --------------------------------------------------------------------------------------------
  94. #pragma mark ==== DZNEmptyDataSetSource ====
  95. #pragma --------------------------------------------------------------------------------------------
  96. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  97. {
  98. return NCBrandColor.sharedInstance.backgroundView;
  99. }
  100. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  101. {
  102. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] width:300 height:300 color:NCBrandColor.sharedInstance.yellowFavorite];
  103. }
  104. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  105. {
  106. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_favorite_no_files_", nil)];
  107. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  108. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  109. }
  110. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  111. {
  112. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_favorite_view_", nil)];
  113. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  114. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  115. paragraph.alignment = NSTextAlignmentCenter;
  116. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  117. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  118. }
  119. #pragma --------------------------------------------------------------------------------------------
  120. #pragma mark ===== Favorite =====
  121. #pragma--------------------------------------------------------------------------------------------
  122. - (void)settingFavorite:(tableMetadata *)metadata favorite:(BOOL)favorite
  123. {
  124. NSString *fileNameServerUrl = [CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:metadata.serverUrl activeUrl:appDelegate.activeUrl];
  125. [[NCCommunication sharedInstance] setFavoriteWithUrlString:appDelegate.activeUrl fileName:fileNameServerUrl favorite:favorite account:appDelegate.activeAccount completionHandler:^(NSString *account, NSInteger errorCode, NSString *errorDecription) {
  126. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  127. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithOcId:metadata.ocId favorite:favorite];
  128. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl ocId:metadata.ocId action:k_action_MOD];
  129. } else if (errorCode != 0) {
  130. [[NCContentPresenter shared] messageNotification:@"_error_" description:errorDecription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode];
  131. } else {
  132. NSLog(@"[LOG] It has been changed user during networking process, error.");
  133. }
  134. }];
  135. }
  136. #pragma --------------------------------------------------------------------------------------------
  137. #pragma mark ===== listingFavorites =====
  138. #pragma--------------------------------------------------------------------------------------------
  139. - (void)listingFavorites
  140. {
  141. // test
  142. if (appDelegate.activeAccount.length == 0)
  143. return;
  144. [[OCNetworking sharedManager] listingFavoritesWithAccount:appDelegate.activeAccount completion:^(NSString *account, NSArray *metadatas, NSString *message, NSInteger errorCode) {
  145. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  146. NSString *father = @"";
  147. NSMutableArray *filesOcId = [NSMutableArray new];
  148. for (tableMetadata *metadata in metadatas) {
  149. // insert for test NOT favorite
  150. [filesOcId addObject:metadata.ocId];
  151. NSString *serverUrl = metadata.serverUrl;
  152. NSString *serverUrlSon = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName];
  153. if (![serverUrlSon containsString:father]) {
  154. if (metadata.directory) {
  155. if ([CCUtility getFavoriteOffline])
  156. [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName] selector:selectorReadFolderWithDownload account:account];
  157. else
  158. [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName] selector:selectorReadFolder account:account];
  159. } else {
  160. if ([CCUtility getFavoriteOffline])
  161. [[CCSynchronize sharedSynchronize] readFile:metadata.ocId fileName:metadata.fileName serverUrl:serverUrl selector:selectorReadFileWithDownload account:account];
  162. else
  163. [[CCSynchronize sharedSynchronize] readFile:metadata.ocId fileName:metadata.fileName serverUrl:serverUrl selector:selectorReadFile account:account];
  164. }
  165. father = serverUrlSon;
  166. }
  167. }
  168. // Verify remove favorite
  169. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", account] sorted:nil ascending:NO];
  170. for (tableMetadata *metadata in allRecordFavorite)
  171. if (![filesOcId containsObject:metadata.ocId])
  172. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithOcId:metadata.ocId favorite:NO];
  173. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"clearDateReadDataSource" object:nil];
  174. } else if (errorCode != 0) {
  175. [[NCContentPresenter shared] messageNotification:@"_error_" description:message delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode];
  176. } else {
  177. NSLog(@"[LOG] It has been changed user during networking process, error.");
  178. }
  179. }];
  180. }
  181. - (void)tapActionComment:(UITapGestureRecognizer *)tapGesture
  182. {
  183. CGPoint location = [tapGesture locationInView:self.tableView];
  184. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  185. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  186. if (metadata) {
  187. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:1];
  188. }
  189. }
  190. - (void)tapActionShared:(UITapGestureRecognizer *)tapGesture
  191. {
  192. CGPoint location = [tapGesture locationInView:self.tableView];
  193. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  194. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  195. if (metadata) {
  196. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:2];
  197. }
  198. }
  199. #pragma --------------------------------------------------------------------------------------------
  200. #pragma mark ===== Progress & Task Button =====
  201. #pragma --------------------------------------------------------------------------------------------
  202. - (void)triggerProgressTask:(NSNotification *)notification
  203. {
  204. if (sectionDataSource.ocIdIndexPath != nil) {
  205. [[NCMainCommon sharedInstance] triggerProgressTask:notification sectionDataSourceocIdIndexPath:sectionDataSource.ocIdIndexPath tableView:self.tableView viewController:self serverUrlViewController:self.serverUrl];
  206. }
  207. }
  208. - (void)cancelTaskButton:(id)sender withEvent:(UIEvent *)event
  209. {
  210. UITouch *touch = [[event allTouches] anyObject];
  211. CGPoint location = [touch locationInView:self.tableView];
  212. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  213. if ([[NCMainCommon sharedInstance] isValidIndexPath:indexPath view:self.tableView]) {
  214. tableMetadata *metadataSection = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  215. if (metadataSection) {
  216. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadataSection.ocId]];
  217. if (metadata)
  218. [[NCMainCommon sharedInstance] cancelTransferMetadata:metadata reloadDatasource:true uploadStatusForcedStart:false];
  219. }
  220. }
  221. }
  222. - (void)cancelAllTask:(id)sender
  223. {
  224. CGPoint location = [sender locationInView:self.tableView];
  225. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  226. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  227. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_all_task_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  228. [NCUtility.sharedInstance startActivityIndicatorWithView:self.view bottom:0];
  229. [[NCMainCommon sharedInstance] cancelAllTransfer];
  230. [NCUtility.sharedInstance stopActivityIndicator];
  231. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:nil ocId:nil action:k_action_NULL];
  232. }]];
  233. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]];
  234. alertController.popoverPresentationController.sourceView = self.tableView;
  235. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  236. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  237. [alertController.view layoutIfNeeded];
  238. [self presentViewController:alertController animated:YES completion:nil];
  239. }
  240. #pragma mark -
  241. #pragma --------------------------------------------------------------------------------------------
  242. #pragma mark ===== Peek & Pop =====
  243. #pragma --------------------------------------------------------------------------------------------
  244. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
  245. {
  246. CGPoint convertedLocation = [self.view convertPoint:location toView:self.tableView];
  247. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];
  248. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  249. CCCellMain *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  250. if (cell) {
  251. previewingContext.sourceRect = cell.frame;
  252. CCPeekPop *viewController = [[UIStoryboard storyboardWithName:@"CCPeekPop" bundle:nil] instantiateViewControllerWithIdentifier:@"PeekPopImagePreview"];
  253. viewController.metadata = metadata;
  254. viewController.imageFile = cell.file.image;
  255. viewController.showOpenIn = true;
  256. viewController.showShare = false;
  257. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_document]) {
  258. viewController.showOpenInternalViewer = true;
  259. }
  260. return viewController;
  261. }
  262. return nil;
  263. }
  264. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
  265. {
  266. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:previewingContext.sourceRect.origin];
  267. [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
  268. }
  269. #pragma mark -
  270. #pragma --------------------------------------------------------------------------------------------
  271. #pragma mark ===== menu action : Favorite, More, Delete [swipe] =====
  272. #pragma --------------------------------------------------------------------------------------------
  273. - (BOOL)canOpenMenuAction:(tableMetadata *)metadata
  274. {
  275. return YES;
  276. }
  277. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction
  278. {
  279. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  280. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  281. return [self canOpenMenuAction:metadata];
  282. }
  283. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell tappedButtonAtIndex:(NSInteger)index direction:(MGSwipeDirection)direction fromExpansion:(BOOL)fromExpansion
  284. {
  285. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  286. if (direction == MGSwipeDirectionRightToLeft) {
  287. [self actionDelete:indexPath];
  288. }
  289. if (direction == MGSwipeDirectionLeftToRight) {
  290. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  291. [self settingFavorite:metadata favorite:NO];
  292. }
  293. return YES;
  294. }
  295. - (void)actionDelete:(NSIndexPath *)indexPath
  296. {
  297. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  298. tableLocalFile *localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  299. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  300. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  301. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND e2eEncrypted == 1 AND serverUrl == %@", appDelegate.activeAccount, metadata.serverUrl]];
  302. [[NCMainCommon sharedInstance ] deleteFileWithMetadatas:[[NSArray alloc] initWithObjects:metadata, nil] e2ee:tableDirectory.e2eEncrypted serverUrl:metadata.serverUrl folderocId:tableDirectory.ocId completion:^(NSInteger errorCode, NSString *message) {
  303. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl ocId:metadata.ocId action:k_action_DEL];
  304. }];
  305. }]];
  306. if (localFile) {
  307. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_local_file_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  308. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  309. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId] error:nil];
  310. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl ocId:metadata.ocId action:k_action_MOD];
  311. }]];
  312. }
  313. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  314. }]];
  315. alertController.popoverPresentationController.sourceView = self.tableView;
  316. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  317. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  318. [alertController.view layoutIfNeeded];
  319. [self presentViewController:alertController animated:YES completion:nil];
  320. }
  321. - (void)actionMore:(UITapGestureRecognizer *)gestureRecognizer
  322. {
  323. CGPoint touch = [gestureRecognizer locationInView:self.tableView];
  324. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touch];
  325. UIImage *iconHeader;
  326. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  327. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.tabBarController.view title:nil];
  328. actionSheet.animationDuration = 0.2;
  329. actionSheet.buttonHeight = 50.0;
  330. actionSheet.cancelButtonHeight = 50.0f;
  331. actionSheet.separatorHeight = 5.0f;
  332. actionSheet.automaticallyTintButtonImages = @(NO);
  333. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:NCBrandColor.sharedInstance.encrypted };
  334. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:NCBrandColor.sharedInstance.textView };
  335. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:17], NSForegroundColorAttributeName:NCBrandColor.sharedInstance.textView };
  336. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:NCBrandColor.sharedInstance.textView };
  337. actionSheet.separatorColor = NCBrandColor.sharedInstance.separator;
  338. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  339. actionSheet.cancelButtonBackgroudColor = NCBrandColor.sharedInstance.backgroundForm;
  340. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  341. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]]) {
  342. iconHeader = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]];
  343. } else {
  344. if (metadata.directory)
  345. iconHeader = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:NCBrandColor.sharedInstance.icon];
  346. else
  347. iconHeader = [UIImage imageNamed:metadata.iconName];
  348. }
  349. [actionSheet addButtonWithTitle: metadata.fileNameView image: iconHeader backgroundColor: NCBrandColor.sharedInstance.backgroundForm height: 50.0 type: AHKActionSheetButtonTypeDisabled handler: nil
  350. ];
  351. // Favorite : ONLY root
  352. if (_serverUrl == nil) {
  353. [actionSheet addButtonWithTitle: NSLocalizedString(@"_remove_favorites_", nil)
  354. image: [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] multiplier:2 color:NCBrandColor.sharedInstance.yellowFavorite]
  355. backgroundColor: NCBrandColor.sharedInstance.backgroundForm
  356. height: 50.0
  357. type: AHKActionSheetButtonTypeDefault
  358. handler: ^(AHKActionSheet *as) {
  359. [self settingFavorite:metadata favorite:NO];
  360. }];
  361. }
  362. // Share
  363. [actionSheet addButtonWithTitle:NSLocalizedString(@"_details_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"details"] width:50 height:50 color:NCBrandColor.sharedInstance.icon] backgroundColor:NCBrandColor.sharedInstance.backgroundForm height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  364. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:0];
  365. }];
  366. // NO Directory
  367. if (metadata.directory == NO && [NCBrandOptions sharedInstance].disable_openin_file == NO) {
  368. [actionSheet addButtonWithTitle:NSLocalizedString(@"_open_in_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"openFile"] multiplier:2 color:NCBrandColor.sharedInstance.icon] backgroundColor:NCBrandColor.sharedInstance.backgroundForm height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  369. [self.tableView setEditing:NO animated:YES];
  370. [[NCMainCommon sharedInstance] downloadOpenWithMetadata:metadata selector:selectorOpenIn];
  371. }];
  372. }
  373. // Delete
  374. [actionSheet addButtonWithTitle:NSLocalizedString(@"_delete_", nil)
  375. image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:[UIColor redColor]]
  376. backgroundColor:NCBrandColor.sharedInstance.backgroundForm
  377. height:50.0
  378. type:AHKActionSheetButtonTypeDestructive
  379. handler:^(AHKActionSheet *as) {
  380. [self actionDelete:indexPath];
  381. }];
  382. [actionSheet show];
  383. }
  384. #pragma --------------------------------------------------------------------------------------------
  385. #pragma mark ==== Table ====
  386. #pragma --------------------------------------------------------------------------------------------
  387. - (tableMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  388. {
  389. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  390. return metadata;
  391. }
  392. - (void)reloadDatasource:(NSString *)ocId action:(NSInteger)action
  393. {
  394. // test
  395. if (appDelegate.activeAccount.length == 0 || self.view.window == nil) {
  396. return;
  397. }
  398. [self queryDatasource];
  399. }
  400. - (void)queryDatasource
  401. {
  402. // test
  403. if (appDelegate.activeAccount.length == 0) {
  404. return;
  405. }
  406. NSArray *recordsTableMetadata;
  407. NSString *sorted = [CCUtility getOrderSettings];
  408. if ([sorted isEqualToString:@"fileName"]) sorted = @"fileName";
  409. // get auto upload folder
  410. autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  411. autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:appDelegate.activeUrl];
  412. if (!_serverUrl) {
  413. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", appDelegate.activeAccount] sorted:nil ascending:NO];
  414. } else {
  415. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, _serverUrl] sorted:nil ascending:NO];
  416. }
  417. sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil filterocId:appDelegate.filterocId filterTypeFileImage:NO filterTypeFileVideo:NO sorted:sorted ascending:[CCUtility getAscendingSettings] activeAccount:appDelegate.activeAccount];
  418. [self.tableView reloadData];
  419. }
  420. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  421. {
  422. return 60;
  423. }
  424. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  425. {
  426. return [[sectionDataSource.sectionArrayRow allKeys] count];
  427. }
  428. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  429. {
  430. return [[sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:section]] count];
  431. }
  432. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  433. {
  434. tableShare *shareCell;
  435. tableMetadata *metadataFolder;
  436. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  437. if (metadata == nil || [[NCManageDatabase sharedInstance] isTableInvalidated:metadata]) {
  438. return [CCCellMain new];
  439. }
  440. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, metadata.serverUrl]];
  441. if (directory != nil) {
  442. metadataFolder = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", directory.ocId]];
  443. }
  444. for (tableShare *share in appDelegate.shares) {
  445. if ([share.serverUrl isEqualToString:metadata.serverUrl] && [share.fileName isEqualToString:metadata.fileName]) {
  446. shareCell = share;
  447. break;
  448. }
  449. }
  450. UITableViewCell *cell = [[NCMainCommon sharedInstance] cellForRowAtIndexPath:indexPath tableView:tableView metadata:metadata metadataFolder:metadataFolder serverUrl:self.serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory tableShare:shareCell];
  451. // NORMAL - > MAIN
  452. if ([cell isKindOfClass:[CCCellMain class]]) {
  453. // Comment tap
  454. if (metadata.commentsUnread) {
  455. UITapGestureRecognizer *tapComment = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionComment:)];
  456. [tapComment setNumberOfTapsRequired:1];
  457. ((CCCellMain *)cell).comment.userInteractionEnabled = YES;
  458. [((CCCellMain *)cell).comment addGestureRecognizer:tapComment];
  459. }
  460. // Share add Tap
  461. UITapGestureRecognizer *tapShare = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionShared:)];
  462. [tapShare setNumberOfTapsRequired:1];
  463. ((CCCellMain *)cell).viewShared.userInteractionEnabled = YES;
  464. [((CCCellMain *)cell).viewShared addGestureRecognizer:tapShare];
  465. // More
  466. if ([self canOpenMenuAction:metadata]) {
  467. UITapGestureRecognizer *tapMore = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionMore:)];
  468. [tapMore setNumberOfTapsRequired:1];
  469. ((CCCellMain *)cell).more.userInteractionEnabled = YES;
  470. [((CCCellMain *)cell).more addGestureRecognizer:tapMore];
  471. }
  472. // MGSwipeButton
  473. ((CCCellMain *)cell).delegate = self;
  474. // LEFT : configure ONLY Root Favorites : Remove file/folder Favorites
  475. if (_serverUrl == nil) {
  476. ((CCCellMain *)cell).leftButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] width:50 height:50 color:[UIColor whiteColor]] backgroundColor:NCBrandColor.sharedInstance.yellowFavorite padding:25]];
  477. ((CCCellMain *)cell).leftExpansion.buttonIndex = 0;
  478. ((CCCellMain *)cell).leftExpansion.fillOnTrigger = NO;
  479. //centerIconOverText
  480. MGSwipeButton *favoriteButton = (MGSwipeButton *)[((CCCellMain *)cell).leftButtons objectAtIndex:0];
  481. [favoriteButton centerIconOverText];
  482. }
  483. // RIGHT
  484. ((CCCellMain *)cell).rightButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:[UIColor whiteColor]] backgroundColor:[UIColor redColor] padding:25]];
  485. ((CCCellMain *)cell).rightExpansion.buttonIndex = 0;
  486. ((CCCellMain *)cell).rightExpansion.fillOnTrigger = NO;
  487. //centerIconOverText
  488. MGSwipeButton *deleteButton = (MGSwipeButton *)[((CCCellMain *)cell).rightButtons objectAtIndex:0];
  489. [deleteButton centerIconOverText];
  490. }
  491. // TRANSFER
  492. if ([cell isKindOfClass:[CCCellMainTransfer class]]) {
  493. // gesture Transfer
  494. [((CCCellMainTransfer *)cell).transferButton.stopButton addTarget:self action:@selector(cancelTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  495. UILongPressGestureRecognizer *stopLongGesture = [UILongPressGestureRecognizer new];
  496. [stopLongGesture addTarget:self action:@selector(cancelAllTask:)];
  497. [((CCCellMainTransfer *)cell).transferButton.stopButton addGestureRecognizer:stopLongGesture];
  498. }
  499. return cell;
  500. }
  501. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  502. {
  503. // deselect row
  504. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  505. self.metadata = [self setSelfMetadataFromIndexPath:indexPath];
  506. // if is in download [do not touch]
  507. if (self.metadata.status == k_metadataStatusWaitDownload || self.metadata.status == k_metadataStatusInDownload || self.metadata.status == k_metadataStatusDownloading)
  508. return;
  509. // File
  510. if (self.metadata.directory == NO) {
  511. // File do not exists
  512. if ([CCUtility fileProviderStorageExists:self.metadata.ocId fileNameView:self.metadata.fileNameView]) {
  513. [[NCNetworkingMain sharedInstance] downloadFileSuccessFailure:self.metadata.fileName ocId:self.metadata.ocId serverUrl:self.metadata.serverUrl selector:selectorLoadFileView errorMessage:@"" errorCode:0];
  514. } else {
  515. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, self.metadata.serverUrl]];
  516. if (tableDirectory.e2eEncrypted && ![CCUtility isEndToEndEnabled:appDelegate.activeAccount]) {
  517. [[NCContentPresenter shared] messageNotification:@"_info_" description:@"_e2e_goto_settings_for_enable_" delay:k_dismissAfterSecond type:messageTypeInfo errorCode:0];
  518. } else {
  519. if (([self.metadata.typeFile isEqualToString: k_metadataTypeFile_video] || [self.metadata.typeFile isEqualToString: k_metadataTypeFile_audio] || [_metadata.typeFile isEqualToString: k_metadataTypeFile_image]) && self.metadata.e2eEncrypted == NO) {
  520. [self shouldPerformSegue:self.metadata selector:@""];
  521. } else {
  522. self.metadata.session = k_download_session;
  523. self.metadata.sessionError = @"";
  524. self.metadata.sessionSelector = selectorLoadFileView;
  525. self.metadata.status = k_metadataStatusWaitDownload;
  526. // Add Metadata for Download
  527. tableMetadata *metadata = [[NCManageDatabase sharedInstance] addMetadata:self.metadata];
  528. [[CCNetworking sharedNetworking] downloadFile:metadata taskStatus:k_taskStatusResume];
  529. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:self.metadata.serverUrl ocId:self.metadata.ocId action:k_action_MOD];
  530. }
  531. }
  532. }
  533. }
  534. // Directory
  535. if (self.metadata.directory)
  536. [self performSegueDirectoryWithControlPasscode];
  537. }
  538. -(void)performSegueDirectoryWithControlPasscode
  539. {
  540. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  541. vc.serverUrl = [CCUtility stringAppendServerUrl:self.metadata.serverUrl addFileName:self.metadata.fileName];
  542. vc.titleViewControl = self.metadata.fileNameView;
  543. [self.navigationController pushViewController:vc animated:YES];
  544. }
  545. #pragma --------------------------------------------------------------------------------------------
  546. #pragma mark ===== Navigation ====
  547. #pragma --------------------------------------------------------------------------------------------
  548. - (void)shouldPerformSegue:(tableMetadata *)metadata selector:(NSString *)selector
  549. {
  550. // if i am in background -> exit
  551. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return;
  552. // if i am not window -> exit
  553. if (self.view.window == NO)
  554. return;
  555. // Collapsed but i am in detail -> exit
  556. if (self.splitViewController.isCollapsed)
  557. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return;
  558. // Metadata for push detail
  559. self.metadataForPushDetail = metadata;
  560. self.selectorForPushDetail = selector;
  561. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  562. }
  563. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  564. {
  565. id viewController = segue.destinationViewController;
  566. if ([viewController isKindOfClass:[UINavigationController class]]) {
  567. UINavigationController *nav = viewController;
  568. _detailViewController = (CCDetail *)nav.topViewController;
  569. } else {
  570. _detailViewController = segue.destinationViewController;
  571. }
  572. NSMutableArray *photoDataSource = [NSMutableArray new];
  573. for (NSString *ocId in sectionDataSource.allOcId) {
  574. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:ocId];
  575. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  576. [photoDataSource addObject:metadata];
  577. }
  578. _detailViewController.metadataDetail = self.metadataForPushDetail;
  579. _detailViewController.selectorDetail = self.selectorForPushDetail;
  580. _detailViewController.dateFilterQuery = nil;
  581. _detailViewController.photoDataSource = photoDataSource;
  582. [_detailViewController setTitle:self.metadata.fileNameView];
  583. }
  584. @end