CCFavorites.m 33 KB

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