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