CCFavorites.m 31 KB

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