CCFavorites.m 31 KB

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