CCFavorites.m 31 KB

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