CCFavorites.m 37 KB

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