CCFavorites.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. // Notification
  56. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:k_notificationCenter_progressTask object:nil];
  57. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:k_notificationCenter_changeTheming object:nil];
  58. // Metadata
  59. self.metadata = [tableMetadata new];
  60. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  61. self.tableView.emptyDataSetDelegate = self;
  62. self.tableView.emptyDataSetSource = self;
  63. self.tableView.delegate = self;
  64. self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 35, 0);
  65. // Register for 3D Touch Previewing if available
  66. if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable))
  67. {
  68. [self registerForPreviewingWithDelegate:self sourceView:self.view];
  69. }
  70. // calculate _serverUrl
  71. if (!_serverUrl)
  72. _serverUrl = nil;
  73. // Title
  74. if (_titleViewControl)
  75. self.title = _titleViewControl;
  76. else
  77. self.title = NSLocalizedString(@"_favorites_", nil);
  78. [self changeTheming];
  79. }
  80. - (void)viewDidAppear:(BOOL)animated
  81. {
  82. [super viewDidAppear:animated];
  83. // Active Main
  84. appDelegate.activeFavorites = self;
  85. [self reloadDatasource];
  86. }
  87. #pragma --------------------------------------------------------------------------------------------
  88. #pragma mark ==== NotificationCenter ====
  89. #pragma --------------------------------------------------------------------------------------------
  90. - (void)triggerProgressTask:(NSNotification *)notification
  91. {
  92. if (sectionDataSource.ocIdIndexPath != nil) {
  93. [[NCMainCommon sharedInstance] triggerProgressTask:notification sectionDataSourceocIdIndexPath:sectionDataSource.ocIdIndexPath tableView:self.tableView viewController:self serverUrlViewController:self.serverUrl];
  94. }
  95. }
  96. - (void)changeTheming
  97. {
  98. [appDelegate changeTheming:self tableView:self.tableView collectionView:nil form:false];
  99. }
  100. #pragma --------------------------------------------------------------------------------------------
  101. #pragma mark ==== DZNEmptyDataSetSource ====
  102. #pragma --------------------------------------------------------------------------------------------
  103. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  104. {
  105. return NCBrandColor.sharedInstance.backgroundView;
  106. }
  107. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  108. {
  109. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] width:300 height:300 color:NCBrandColor.sharedInstance.yellowFavorite];
  110. }
  111. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  112. {
  113. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_favorite_no_files_", nil)];
  114. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  115. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  116. }
  117. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  118. {
  119. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_favorite_view_", nil)];
  120. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  121. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  122. paragraph.alignment = NSTextAlignmentCenter;
  123. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  124. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  125. }
  126. #pragma --------------------------------------------------------------------------------------------
  127. #pragma mark ===== listingFavorites =====
  128. #pragma--------------------------------------------------------------------------------------------
  129. - (void)listingFavorites
  130. {
  131. // test
  132. if (appDelegate.activeAccount.length == 0)
  133. return;
  134. [[NCCommunication shared] listingFavoritesWithShowHiddenFiles:[CCUtility getShowHiddenFiles] customUserAgent:nil addCustomHeaders:nil completionHandler:^(NSString *account, NSArray *files, NSInteger errorCode, NSString *errorMessage) {
  135. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount] && files != nil) {
  136. [[NCManageDatabase sharedInstance] convertNCCommunicationFilesToMetadatas:files useMetadataFolder:false account:account completion:^(tableMetadata *metadataFolder, NSArray<tableMetadata *> *metadatasFolder, NSArray<tableMetadata *> *metadatas) {
  137. NSString *father = @"";
  138. NSMutableArray *filesOcId = [NSMutableArray new];
  139. for (tableMetadata *metadata in metadatas) {
  140. // insert for test NOT favorite
  141. [filesOcId addObject:metadata.ocId];
  142. NSString *serverUrl = metadata.serverUrl;
  143. NSString *serverUrlSon = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName];
  144. if (![serverUrlSon containsString:father]) {
  145. if (metadata.directory) {
  146. if ([CCUtility getFavoriteOffline])
  147. [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName] selector:selectorReadFolderWithDownload account:account];
  148. else
  149. [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName] selector:selectorReadFolder account:account];
  150. } else {
  151. if ([CCUtility getFavoriteOffline])
  152. [[CCSynchronize sharedSynchronize] readFile:metadata.ocId fileName:metadata.fileName serverUrl:serverUrl selector:selectorReadFileWithDownload account:account];
  153. else
  154. [[CCSynchronize sharedSynchronize] readFile:metadata.ocId fileName:metadata.fileName serverUrl:serverUrl selector:selectorReadFile account:account];
  155. }
  156. father = serverUrlSon;
  157. }
  158. }
  159. // Verify remove favorite
  160. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", account] sorted:nil ascending:NO];
  161. for (tableMetadata *metadata in allRecordFavorite)
  162. if (![filesOcId containsObject:metadata.ocId])
  163. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithOcId:metadata.ocId favorite:NO];
  164. }];
  165. } else if (errorCode != 0) {
  166. [[NCContentPresenter shared] messageNotification:@"_error_" description:errorMessage delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode];
  167. } else {
  168. NSLog(@"[LOG] It has been changed user during networking process, error.");
  169. }
  170. }];
  171. }
  172. - (void)tapActionComment:(UITapGestureRecognizer *)tapGesture
  173. {
  174. CGPoint location = [tapGesture locationInView:self.tableView];
  175. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  176. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  177. if (metadata) {
  178. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:1];
  179. }
  180. }
  181. - (void)tapActionShared:(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:2];
  188. }
  189. }
  190. #pragma --------------------------------------------------------------------------------------------
  191. #pragma mark ===== Progress & Task Button =====
  192. #pragma --------------------------------------------------------------------------------------------
  193. - (void)cancelTaskButton:(id)sender withEvent:(UIEvent *)event
  194. {
  195. UITouch *touch = [[event allTouches] anyObject];
  196. CGPoint location = [touch locationInView:self.tableView];
  197. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  198. if ([[NCMainCommon sharedInstance] isValidIndexPath:indexPath view:self.tableView]) {
  199. tableMetadata *metadataSection = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  200. if (metadataSection) {
  201. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadataSection.ocId]];
  202. if (metadata)
  203. [[NCMainCommon sharedInstance] cancelTransferMetadata:metadata reloadDatasource:true uploadStatusForcedStart:false];
  204. }
  205. }
  206. }
  207. - (void)cancelAllTask:(id)sender
  208. {
  209. CGPoint location = [sender locationInView:self.tableView];
  210. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  211. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  212. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_all_task_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  213. [NCUtility.sharedInstance startActivityIndicatorWithView:self.view bottom:0];
  214. [[NCMainCommon sharedInstance] cancelAllTransfer];
  215. [NCUtility.sharedInstance stopActivityIndicator];
  216. }]];
  217. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]];
  218. alertController.popoverPresentationController.sourceView = self.tableView;
  219. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  220. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  221. [alertController.view layoutIfNeeded];
  222. [self presentViewController:alertController animated:YES completion:nil];
  223. }
  224. #pragma mark -
  225. #pragma --------------------------------------------------------------------------------------------
  226. #pragma mark ===== Peek & Pop =====
  227. #pragma --------------------------------------------------------------------------------------------
  228. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
  229. {
  230. CGPoint convertedLocation = [self.view convertPoint:location toView:self.tableView];
  231. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];
  232. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  233. CCCellMain *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  234. if (cell) {
  235. previewingContext.sourceRect = cell.frame;
  236. CCPeekPop *viewController = [[UIStoryboard storyboardWithName:@"CCPeekPop" bundle:nil] instantiateViewControllerWithIdentifier:@"PeekPopImagePreview"];
  237. viewController.metadata = metadata;
  238. viewController.imageFile = cell.file.image;
  239. viewController.showOpenIn = true;
  240. viewController.showShare = false;
  241. viewController.showOpenQuickLook = [[NCUtility sharedInstance] isQuickLookDisplayableWithMetadata:metadata];
  242. return viewController;
  243. }
  244. return nil;
  245. }
  246. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
  247. {
  248. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:previewingContext.sourceRect.origin];
  249. [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
  250. }
  251. #pragma mark -
  252. #pragma --------------------------------------------------------------------------------------------
  253. #pragma mark ===== menu action : Favorite, More, Delete [swipe] =====
  254. #pragma --------------------------------------------------------------------------------------------
  255. - (BOOL)canOpenMenuAction:(tableMetadata *)metadata
  256. {
  257. return YES;
  258. }
  259. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction
  260. {
  261. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  262. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  263. return [self canOpenMenuAction:metadata];
  264. }
  265. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell tappedButtonAtIndex:(NSInteger)index direction:(MGSwipeDirection)direction fromExpansion:(BOOL)fromExpansion
  266. {
  267. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  268. if (direction == MGSwipeDirectionRightToLeft) {
  269. [self actionDelete:indexPath];
  270. }
  271. if (direction == MGSwipeDirectionLeftToRight) {
  272. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  273. [[NCNetworking shared] favoriteMetadata:metadata url:appDelegate.activeUrl completion:^(NSInteger errorCode, NSString *errorDescription) { }];
  274. }
  275. return YES;
  276. }
  277. - (void)actionDelete:(NSIndexPath *)indexPath
  278. {
  279. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  280. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  281. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  282. [[NCNetworking shared] deleteMetadata:metadata account:appDelegate.activeAccount url:appDelegate.activeUrl completion:^(NSInteger errorCode, NSString *errorDescription) { }];
  283. }]];
  284. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  285. }]];
  286. alertController.popoverPresentationController.sourceView = self.tableView;
  287. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  288. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  289. [alertController.view layoutIfNeeded];
  290. [self presentViewController:alertController animated:YES completion:nil];
  291. }
  292. - (void)actionMore:(UITapGestureRecognizer *)gestureRecognizer
  293. {
  294. CGPoint touch = [gestureRecognizer locationInView:self.tableView];
  295. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touch];
  296. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  297. [self toggleMoreMenuWithViewController:self.tabBarController indexPath:indexPath metadata:metadata];
  298. }
  299. #pragma --------------------------------------------------------------------------------------------
  300. #pragma mark ==== Table ====
  301. #pragma --------------------------------------------------------------------------------------------
  302. - (tableMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  303. {
  304. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  305. return metadata;
  306. }
  307. - (void)reloadDatasource
  308. {
  309. // test
  310. if (appDelegate.activeAccount.length == 0 || self.view.window == nil) {
  311. return;
  312. }
  313. NSArray *recordsTableMetadata;
  314. NSString *sorted = [CCUtility getOrderSettings];
  315. if ([sorted isEqualToString:@"fileName"]) sorted = @"fileName";
  316. // get auto upload folder
  317. autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  318. autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:appDelegate.activeUrl];
  319. if (!_serverUrl) {
  320. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", appDelegate.activeAccount] sorted:nil ascending:NO];
  321. } else {
  322. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, _serverUrl] sorted:nil ascending:NO];
  323. }
  324. sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil filterTypeFileImage:NO filterTypeFileVideo:NO sorted:sorted ascending:[CCUtility getAscendingSettings] activeAccount:appDelegate.activeAccount];
  325. [self.tableView reloadData];
  326. }
  327. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  328. {
  329. return 60;
  330. }
  331. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  332. {
  333. return [[sectionDataSource.sectionArrayRow allKeys] count];
  334. }
  335. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  336. {
  337. return [[sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:section]] count];
  338. }
  339. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  340. {
  341. tableShare *shareCell;
  342. tableMetadata *metadataFolder;
  343. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  344. if (metadata == nil || [[NCManageDatabase sharedInstance] isTableInvalidated:metadata]) {
  345. return [CCCellMain new];
  346. }
  347. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, metadata.serverUrl]];
  348. if (directory != nil) {
  349. metadataFolder = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", directory.ocId]];
  350. }
  351. for (tableShare *share in appDelegate.shares) {
  352. if ([share.serverUrl isEqualToString:metadata.serverUrl] && [share.fileName isEqualToString:metadata.fileName]) {
  353. shareCell = share;
  354. break;
  355. }
  356. }
  357. UITableViewCell *cell = [[NCMainCommon sharedInstance] cellForRowAtIndexPath:indexPath tableView:tableView metadata:metadata metadataFolder:metadataFolder serverUrl:self.serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory tableShare:shareCell];
  358. // NORMAL - > MAIN
  359. if ([cell isKindOfClass:[CCCellMain class]]) {
  360. // Comment tap
  361. if (metadata.commentsUnread) {
  362. UITapGestureRecognizer *tapComment = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionComment:)];
  363. [tapComment setNumberOfTapsRequired:1];
  364. ((CCCellMain *)cell).comment.userInteractionEnabled = YES;
  365. [((CCCellMain *)cell).comment addGestureRecognizer:tapComment];
  366. }
  367. // Share add Tap
  368. UITapGestureRecognizer *tapShare = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionShared:)];
  369. [tapShare setNumberOfTapsRequired:1];
  370. ((CCCellMain *)cell).viewShared.userInteractionEnabled = YES;
  371. [((CCCellMain *)cell).viewShared addGestureRecognizer:tapShare];
  372. // More
  373. if ([self canOpenMenuAction:metadata]) {
  374. UITapGestureRecognizer *tapMore = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionMore:)];
  375. [tapMore setNumberOfTapsRequired:1];
  376. ((CCCellMain *)cell).more.userInteractionEnabled = YES;
  377. [((CCCellMain *)cell).more addGestureRecognizer:tapMore];
  378. }
  379. // MGSwipeButton
  380. ((CCCellMain *)cell).delegate = self;
  381. // LEFT : configure ONLY Root Favorites : Remove file/folder Favorites
  382. if (_serverUrl == nil) {
  383. ((CCCellMain *)cell).leftButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] width:50 height:50 color:[UIColor whiteColor]] backgroundColor:NCBrandColor.sharedInstance.yellowFavorite padding:25]];
  384. ((CCCellMain *)cell).leftExpansion.buttonIndex = 0;
  385. ((CCCellMain *)cell).leftExpansion.fillOnTrigger = NO;
  386. //centerIconOverText
  387. MGSwipeButton *favoriteButton = (MGSwipeButton *)[((CCCellMain *)cell).leftButtons objectAtIndex:0];
  388. [favoriteButton centerIconOverText];
  389. }
  390. // RIGHT
  391. ((CCCellMain *)cell).rightButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:[UIColor whiteColor]] backgroundColor:[UIColor redColor] padding:25]];
  392. ((CCCellMain *)cell).rightExpansion.buttonIndex = 0;
  393. ((CCCellMain *)cell).rightExpansion.fillOnTrigger = NO;
  394. //centerIconOverText
  395. MGSwipeButton *deleteButton = (MGSwipeButton *)[((CCCellMain *)cell).rightButtons objectAtIndex:0];
  396. [deleteButton centerIconOverText];
  397. }
  398. // TRANSFER
  399. if ([cell isKindOfClass:[CCCellMainTransfer class]]) {
  400. // gesture Transfer
  401. [((CCCellMainTransfer *)cell).transferButton.stopButton addTarget:self action:@selector(cancelTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  402. UILongPressGestureRecognizer *stopLongGesture = [UILongPressGestureRecognizer new];
  403. [stopLongGesture addTarget:self action:@selector(cancelAllTask:)];
  404. [((CCCellMainTransfer *)cell).transferButton.stopButton addGestureRecognizer:stopLongGesture];
  405. }
  406. return cell;
  407. }
  408. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  409. {
  410. // deselect row
  411. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  412. self.metadata = [self setSelfMetadataFromIndexPath:indexPath];
  413. if (self.metadata.status != k_metadataStatusNormal && self.metadata.status != k_metadataStatusDownloadError) {
  414. return;
  415. }
  416. // File
  417. if (self.metadata.directory == NO) {
  418. // File do not exists
  419. if ([CCUtility fileProviderStorageExists:self.metadata.ocId fileNameView:self.metadata.fileNameView]) {
  420. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_downloadedFile object:nil userInfo:@{@"metadata": self.metadata, @"selector": selectorLoadFileView, @"errorCode": @(0), @"errorDescription": @""}];
  421. } else {
  422. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, self.metadata.serverUrl]];
  423. if (tableDirectory.e2eEncrypted && ![CCUtility isEndToEndEnabled:appDelegate.activeAccount]) {
  424. [[NCContentPresenter shared] messageNotification:@"_info_" description:@"_e2e_goto_settings_for_enable_" delay:k_dismissAfterSecond type:messageTypeInfo errorCode:0];
  425. } else {
  426. if (([self.metadata.typeFile isEqualToString: k_metadataTypeFile_video] || [self.metadata.typeFile isEqualToString: k_metadataTypeFile_audio]) && self.metadata.e2eEncrypted == NO) {
  427. [self shouldPerformSegue:self.metadata selector:@""];
  428. } else if ([self.metadata.typeFile isEqualToString: k_metadataTypeFile_document] && [[NCUtility sharedInstance] isDirectEditing:self.metadata] != nil) {
  429. if (NCCommunication.shared.isNetworkReachable) {
  430. [self shouldPerformSegue:self.metadata selector:@""];
  431. } else {
  432. [[NCContentPresenter shared] messageNotification:@"_info_" description:@"_go_online_" delay:k_dismissAfterSecond type:messageTypeInfo errorCode:0];
  433. }
  434. } else if ([self.metadata.typeFile isEqualToString: k_metadataTypeFile_document] && [[NCUtility sharedInstance] isRichDocument:self.metadata]) {
  435. if (NCCommunication.shared.isNetworkReachable) {
  436. [self shouldPerformSegue:self.metadata selector:@""];
  437. } else {
  438. [[NCContentPresenter shared] messageNotification:@"_info_" description:@"_go_online_" delay:k_dismissAfterSecond type:messageTypeInfo errorCode:0];
  439. }
  440. } else {
  441. if ([self.metadata.typeFile isEqualToString: k_metadataTypeFile_image]) {
  442. [self shouldPerformSegue:self.metadata selector:selectorLoadFileView];
  443. }
  444. [[NCNetworking shared] downloadWithMetadata:self.metadata selector:selectorLoadFileViewFavorite setFavorite:false completion:^(NSInteger errorCode) { }];
  445. }
  446. }
  447. }
  448. }
  449. // Directory
  450. if (self.metadata.directory) {
  451. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  452. vc.serverUrl = [CCUtility stringAppendServerUrl:self.metadata.serverUrl addFileName:self.metadata.fileName];
  453. vc.titleViewControl = self.metadata.fileNameView;
  454. [self.navigationController pushViewController:vc animated:YES];
  455. }
  456. }
  457. #pragma --------------------------------------------------------------------------------------------
  458. #pragma mark ===== Navigation ====
  459. #pragma --------------------------------------------------------------------------------------------
  460. - (void)shouldPerformSegue:(tableMetadata *)metadata selector:(NSString *)selector
  461. {
  462. // if i am in background -> exit
  463. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return;
  464. // if i am not window -> exit
  465. if (self.view.window == NO)
  466. return;
  467. // Collapsed but i am in detail -> exit
  468. if (self.splitViewController.isCollapsed) {
  469. if (appDelegate.activeDetail.isViewLoaded && appDelegate.activeDetail.view.window) return;
  470. }
  471. // Metadata for push detail
  472. self.metadataForPushDetail = metadata;
  473. self.selectorForPushDetail = selector;
  474. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  475. }
  476. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  477. {
  478. UINavigationController *navigationController = segue.destinationViewController;
  479. NCDetailViewController *detailViewController = (NCDetailViewController *)navigationController.topViewController;
  480. NSMutableArray *photoDataSource = [NSMutableArray new];
  481. for (NSString *ocId in sectionDataSource.allOcId) {
  482. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:ocId];
  483. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  484. [photoDataSource addObject:metadata];
  485. }
  486. detailViewController.metadata = self.metadataForPushDetail;
  487. detailViewController.selector = self.selectorForPushDetail;
  488. detailViewController.favoriteFilterImage = true;
  489. [detailViewController setTitle:self.metadata.fileNameView];
  490. }
  491. @end