CCFavorites.m 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  47. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  48. appDelegate.activeFavorites = self;
  49. }
  50. return self;
  51. }
  52. - (void)viewDidLoad
  53. {
  54. [super viewDidLoad];
  55. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellMain" bundle:nil] forCellReuseIdentifier:@"CellMain"];
  56. [self.tableView registerNib:[UINib nibWithNibName:@"CCCellMainTransfer" bundle:nil] forCellReuseIdentifier:@"CellMainTransfer"];
  57. // Metadata
  58. self.metadata = [tableMetadata new];
  59. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  60. self.tableView.emptyDataSetDelegate = self;
  61. self.tableView.emptyDataSetSource = self;
  62. self.tableView.delegate = self;
  63. // Register for 3D Touch Previewing if available
  64. if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable))
  65. {
  66. [self registerForPreviewingWithDelegate:self sourceView:self.view];
  67. }
  68. // calculate _serverUrl
  69. if (!_serverUrl)
  70. _serverUrl = nil;
  71. // Title
  72. if (_titleViewControl)
  73. self.title = _titleViewControl;
  74. else
  75. self.title = NSLocalizedString(@"_favorites_", nil);
  76. // Query data source
  77. [self queryDatasource];
  78. }
  79. - (void)viewWillAppear:(BOOL)animated
  80. {
  81. [super viewWillAppear:animated];
  82. // Color
  83. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  84. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  85. self.tableView.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  86. self.tableView.separatorColor = [NCBrandColor sharedInstance].separator;
  87. // Plus Button
  88. [appDelegate plusButtonVisibile:true];
  89. }
  90. - (void)viewDidAppear:(BOOL)animated
  91. {
  92. [super viewDidAppear:animated];
  93. // Active Main
  94. appDelegate.activeFavorites = self;
  95. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  96. [self reloadDatasource:nil action:k_action_NULL];
  97. });
  98. }
  99. - (void)changeTheming
  100. {
  101. if (self.isViewLoaded && self.view.window)
  102. [appDelegate changeTheming:self];
  103. // Reload Table View
  104. [self.tableView reloadData];
  105. }
  106. #pragma --------------------------------------------------------------------------------------------
  107. #pragma mark ==== DZNEmptyDataSetSource ====
  108. #pragma --------------------------------------------------------------------------------------------
  109. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  110. {
  111. return [NCBrandColor sharedInstance].backgroundView;
  112. }
  113. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  114. {
  115. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] width:300 height:300 color:[NCBrandColor sharedInstance].yellowFavorite];
  116. }
  117. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  118. {
  119. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_favorite_no_files_", nil)];
  120. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  121. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  122. }
  123. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  124. {
  125. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_favorite_view_", nil)];
  126. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  127. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  128. paragraph.alignment = NSTextAlignmentCenter;
  129. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  130. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  131. }
  132. #pragma --------------------------------------------------------------------------------------------
  133. #pragma mark ===== Favorite =====
  134. #pragma--------------------------------------------------------------------------------------------
  135. - (void)settingFavorite:(tableMetadata *)metadata favorite:(BOOL)favorite
  136. {
  137. NSString *fileNameServerUrl = [CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:metadata.serverUrl activeUrl:appDelegate.activeUrl];
  138. [[OCNetworking sharedManager] settingFavoriteWithAccount:appDelegate.activeAccount fileName:fileNameServerUrl favorite:favorite completion:^(NSString *account, NSString *message, NSInteger errorCode) {
  139. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  140. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithOcId:metadata.ocId favorite:favorite];
  141. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl ocId:metadata.ocId action:k_action_MOD];
  142. } else if (errorCode != 0) {
  143. [appDelegate messageNotification:@"_error_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  144. } else {
  145. NSLog(@"[LOG] It has been changed user during networking process, error.");
  146. }
  147. }];
  148. }
  149. #pragma --------------------------------------------------------------------------------------------
  150. #pragma mark ===== listingFavorites =====
  151. #pragma--------------------------------------------------------------------------------------------
  152. - (void)listingFavorites
  153. {
  154. // test
  155. if (appDelegate.activeAccount.length == 0)
  156. return;
  157. [[OCNetworking sharedManager] listingFavoritesWithAccount:appDelegate.activeAccount completion:^(NSString *account, NSArray *metadatas, NSString *message, NSInteger errorCode) {
  158. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  159. NSString *father = @"";
  160. NSMutableArray *filesEtag = [NSMutableArray new];
  161. for (tableMetadata *metadata in metadatas) {
  162. // insert for test NOT favorite
  163. [filesEtag addObject:metadata.ocId];
  164. NSString *serverUrl = metadata.serverUrl;
  165. NSString *serverUrlSon = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName];
  166. if (![serverUrlSon containsString:father]) {
  167. if (metadata.directory) {
  168. if ([CCUtility getFavoriteOffline])
  169. [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName] selector:selectorReadFolderWithDownload account:account];
  170. else
  171. [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName] selector:selectorReadFolder account:account];
  172. } else {
  173. if ([CCUtility getFavoriteOffline])
  174. [[CCSynchronize sharedSynchronize] readFile:metadata.ocId fileName:metadata.fileName serverUrl:serverUrl selector:selectorReadFileWithDownload account:account];
  175. else
  176. [[CCSynchronize sharedSynchronize] readFile:metadata.ocId fileName:metadata.fileName serverUrl:serverUrl selector:selectorReadFile account:account];
  177. }
  178. father = serverUrlSon;
  179. }
  180. }
  181. // Verify remove favorite
  182. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", account] sorted:nil ascending:NO];
  183. for (tableMetadata *metadata in allRecordFavorite)
  184. if (![filesEtag containsObject:metadata.ocId])
  185. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithOcId:metadata.ocId favorite:NO];
  186. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"clearDateReadDataSource" object:nil];
  187. } else if (errorCode != 0) {
  188. [appDelegate messageNotification:@"_error_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  189. } else {
  190. NSLog(@"[LOG] It has been changed user during networking process, error.");
  191. }
  192. }];
  193. }
  194. - (void)tapActionComment:(UITapGestureRecognizer *)tapGesture
  195. {
  196. CGPoint location = [tapGesture locationInView:self.tableView];
  197. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  198. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  199. if (metadata) {
  200. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:1];
  201. }
  202. }
  203. - (void)tapActionShared:(UITapGestureRecognizer *)tapGesture
  204. {
  205. CGPoint location = [tapGesture locationInView:self.tableView];
  206. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  207. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  208. if (metadata) {
  209. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:2];
  210. }
  211. }
  212. #pragma --------------------------------------------------------------------------------------------
  213. #pragma mark ===== Progress & Task Button =====
  214. #pragma --------------------------------------------------------------------------------------------
  215. - (void)triggerProgressTask:(NSNotification *)notification
  216. {
  217. if (sectionDataSource.ocIdIndexPath != nil) {
  218. [[NCMainCommon sharedInstance] triggerProgressTask:notification sectionDataSourceocIdIndexPath:sectionDataSource.ocIdIndexPath tableView:self.tableView viewController:self serverUrlViewController:self.serverUrl];
  219. }
  220. }
  221. - (void)cancelTaskButton:(id)sender withEvent:(UIEvent *)event
  222. {
  223. UITouch *touch = [[event allTouches] anyObject];
  224. CGPoint location = [touch locationInView:self.tableView];
  225. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  226. if ([[NCMainCommon sharedInstance] isValidIndexPath:indexPath view:self.tableView]) {
  227. tableMetadata *metadataSection = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  228. if (metadataSection) {
  229. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadataSection.ocId]];
  230. if (metadata)
  231. [[NCMainCommon sharedInstance] cancelTransferMetadata:metadata reloadDatasource:true uploadStatusForcedStart:false];
  232. }
  233. }
  234. }
  235. - (void)cancelAllTask:(id)sender
  236. {
  237. CGPoint location = [sender locationInView:self.tableView];
  238. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  239. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  240. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_all_task_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  241. [NCUtility.sharedInstance startActivityIndicatorWithView:self.view bottom:0];
  242. [[NCMainCommon sharedInstance] cancelAllTransfer];
  243. [NCUtility.sharedInstance stopActivityIndicator];
  244. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:nil ocId:nil action:k_action_NULL];
  245. }]];
  246. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]];
  247. alertController.popoverPresentationController.sourceView = self.tableView;
  248. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  249. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  250. [alertController.view layoutIfNeeded];
  251. [self presentViewController:alertController animated:YES completion:nil];
  252. }
  253. #pragma mark -
  254. #pragma --------------------------------------------------------------------------------------------
  255. #pragma mark ===== Peek & Pop =====
  256. #pragma --------------------------------------------------------------------------------------------
  257. - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
  258. {
  259. CGPoint convertedLocation = [self.view convertPoint:location toView:self.tableView];
  260. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];
  261. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  262. CCCellMain *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  263. if (cell) {
  264. previewingContext.sourceRect = cell.frame;
  265. CCPeekPop *viewController = [[UIStoryboard storyboardWithName:@"CCPeekPop" bundle:nil] instantiateViewControllerWithIdentifier:@"PeekPopImagePreview"];
  266. viewController.metadata = metadata;
  267. viewController.imageFile = cell.file.image;
  268. viewController.showOpenIn = true;
  269. viewController.showShare = false;
  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. [self settingFavorite:metadata favorite:NO];
  302. }
  303. return YES;
  304. }
  305. - (void)actionDelete:(NSIndexPath *)indexPath
  306. {
  307. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  308. tableLocalFile *localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  309. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  310. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  311. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND e2eEncrypted == 1 AND serverUrl == %@", appDelegate.activeAccount, metadata.serverUrl]];
  312. [[NCMainCommon sharedInstance ] deleteFileWithMetadatas:[[NSArray alloc] initWithObjects:metadata, nil] e2ee:tableDirectory.e2eEncrypted serverUrl:metadata.serverUrl folderocId:tableDirectory.ocId completion:^(NSInteger errorCode, NSString *message) {
  313. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl ocId:metadata.ocId action:k_action_DEL];
  314. }];
  315. }]];
  316. if (localFile) {
  317. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_local_file_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  318. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  319. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId] error:nil];
  320. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl ocId:metadata.ocId action:k_action_MOD];
  321. }]];
  322. }
  323. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  324. }]];
  325. alertController.popoverPresentationController.sourceView = self.tableView;
  326. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  327. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  328. [alertController.view layoutIfNeeded];
  329. [self presentViewController:alertController animated:YES completion:nil];
  330. }
  331. - (void)actionMore:(UITapGestureRecognizer *)gestureRecognizer
  332. {
  333. CGPoint touch = [gestureRecognizer locationInView:self.tableView];
  334. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touch];
  335. UIImage *iconHeader;
  336. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  337. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.tabBarController.view title:nil];
  338. actionSheet.animationDuration = 0.2;
  339. actionSheet.buttonHeight = 50.0;
  340. actionSheet.cancelButtonHeight = 50.0f;
  341. actionSheet.separatorHeight = 5.0f;
  342. actionSheet.automaticallyTintButtonImages = @(NO);
  343. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].encrypted };
  344. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  345. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:17], NSForegroundColorAttributeName:[UIColor blackColor] };
  346. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor darkGrayColor] };
  347. actionSheet.separatorColor = [NCBrandColor sharedInstance].separator;
  348. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  349. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  350. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]]) {
  351. iconHeader = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]];
  352. } else {
  353. if (metadata.directory)
  354. iconHeader = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:[NCBrandColor sharedInstance].icon];
  355. else
  356. iconHeader = [UIImage imageNamed:metadata.iconName];
  357. }
  358. [actionSheet addButtonWithTitle: metadata.fileNameView image: iconHeader backgroundColor: [NCBrandColor sharedInstance].tabBar height: 50.0 type: AHKActionSheetButtonTypeDisabled handler: nil
  359. ];
  360. // Favorite : ONLY root
  361. if (_serverUrl == nil) {
  362. [actionSheet addButtonWithTitle: NSLocalizedString(@"_remove_favorites_", nil)
  363. image: [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] multiplier:2 color:[NCBrandColor sharedInstance].yellowFavorite]
  364. backgroundColor: [NCBrandColor sharedInstance].backgroundView
  365. height: 50.0
  366. type: AHKActionSheetButtonTypeDefault
  367. handler: ^(AHKActionSheet *as) {
  368. [self settingFavorite:metadata favorite:NO];
  369. }];
  370. }
  371. // Share
  372. [actionSheet addButtonWithTitle:NSLocalizedString(@"_details_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"details"] width:50 height:50 color:[NCBrandColor sharedInstance].icon] backgroundColor:[NCBrandColor sharedInstance].backgroundView height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  373. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:0];
  374. }];
  375. // NO Directory
  376. if (metadata.directory == NO && [NCBrandOptions sharedInstance].disable_openin_file == NO) {
  377. [actionSheet addButtonWithTitle:NSLocalizedString(@"_open_in_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"openFile"] multiplier:2 color:[NCBrandColor sharedInstance].icon] backgroundColor:[NCBrandColor sharedInstance].backgroundView height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  378. [self.tableView setEditing:NO animated:YES];
  379. [[NCMainCommon sharedInstance] downloadOpenInMetadata:metadata];
  380. }];
  381. }
  382. // Delete
  383. [actionSheet addButtonWithTitle:NSLocalizedString(@"_delete_", nil)
  384. image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:[UIColor redColor]]
  385. backgroundColor:[NCBrandColor sharedInstance].backgroundView
  386. height:50.0
  387. type:AHKActionSheetButtonTypeDestructive
  388. handler:^(AHKActionSheet *as) {
  389. [self actionDelete:indexPath];
  390. }];
  391. [actionSheet show];
  392. }
  393. #pragma --------------------------------------------------------------------------------------------
  394. #pragma mark ==== Table ====
  395. #pragma --------------------------------------------------------------------------------------------
  396. - (tableMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  397. {
  398. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  399. return metadata;
  400. }
  401. - (void)reloadDatasource:(NSString *)ocId action:(NSInteger)action
  402. {
  403. // test
  404. if (appDelegate.activeAccount.length == 0 || self.view.window == nil) {
  405. return;
  406. }
  407. [self queryDatasource];
  408. }
  409. - (void)queryDatasource
  410. {
  411. // test
  412. if (appDelegate.activeAccount.length == 0) {
  413. return;
  414. }
  415. NSArray *recordsTableMetadata;
  416. NSString *sorted = [CCUtility getOrderSettings];
  417. if ([sorted isEqualToString:@"fileName"]) sorted = @"fileName";
  418. // get auto upload folder
  419. autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  420. autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:appDelegate.activeUrl];
  421. if (!_serverUrl) {
  422. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", appDelegate.activeAccount] sorted:nil ascending:NO];
  423. } else {
  424. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, _serverUrl] sorted:nil ascending:NO];
  425. }
  426. sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil filterocId:appDelegate.filterocId filterTypeFileImage:NO filterTypeFileVideo:NO sorted:sorted ascending:[CCUtility getAscendingSettings] activeAccount:appDelegate.activeAccount];
  427. [self.tableView reloadData];
  428. }
  429. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  430. {
  431. return 60;
  432. }
  433. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  434. {
  435. return [[sectionDataSource.sectionArrayRow allKeys] count];
  436. }
  437. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  438. {
  439. return [[sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:section]] count];
  440. }
  441. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  442. {
  443. tableShare *shareCell;
  444. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  445. if (metadata == nil || [[NCManageDatabase sharedInstance] isTableInvalidated:metadata]) {
  446. return [CCCellMain new];
  447. }
  448. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, metadata.serverUrl]];
  449. if (directory == nil) {
  450. return [CCCellMain new];
  451. }
  452. tableMetadata *metadataFolder = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", directory.ocId]];
  453. for (tableShare *share in appDelegate.shares) {
  454. if ([share.serverUrl isEqualToString:metadata.serverUrl] && [share.fileName isEqualToString:metadata.fileName]) {
  455. shareCell = share;
  456. break;
  457. }
  458. }
  459. UITableViewCell *cell = [[NCMainCommon sharedInstance] cellForRowAtIndexPath:indexPath tableView:tableView metadata:metadata metadataFolder:metadataFolder serverUrl:self.serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory tableShare:shareCell];
  460. // NORMAL - > MAIN
  461. if ([cell isKindOfClass:[CCCellMain class]]) {
  462. // Comment tap
  463. if (metadata.commentsUnread) {
  464. UITapGestureRecognizer *tapComment = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionComment:)];
  465. [tapComment setNumberOfTapsRequired:1];
  466. ((CCCellMain *)cell).comment.userInteractionEnabled = YES;
  467. [((CCCellMain *)cell).comment addGestureRecognizer:tapComment];
  468. }
  469. // Share add Tap
  470. UITapGestureRecognizer *tapShare = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionShared:)];
  471. [tapShare setNumberOfTapsRequired:1];
  472. ((CCCellMain *)cell).viewShared.userInteractionEnabled = YES;
  473. [((CCCellMain *)cell).viewShared addGestureRecognizer:tapShare];
  474. // More
  475. if ([self canOpenMenuAction:metadata]) {
  476. UITapGestureRecognizer *tapMore = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionMore:)];
  477. [tapMore setNumberOfTapsRequired:1];
  478. ((CCCellMain *)cell).more.userInteractionEnabled = YES;
  479. [((CCCellMain *)cell).more addGestureRecognizer:tapMore];
  480. }
  481. // MGSwipeButton
  482. ((CCCellMain *)cell).delegate = self;
  483. // LEFT : configure ONLY Root Favorites : Remove file/folder Favorites
  484. if (_serverUrl == nil) {
  485. ((CCCellMain *)cell).leftButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] width:50 height:50 color:[UIColor whiteColor]] backgroundColor:[NCBrandColor sharedInstance].yellowFavorite padding:25]];
  486. ((CCCellMain *)cell).leftExpansion.buttonIndex = 0;
  487. ((CCCellMain *)cell).leftExpansion.fillOnTrigger = NO;
  488. //centerIconOverText
  489. MGSwipeButton *favoriteButton = (MGSwipeButton *)[((CCCellMain *)cell).leftButtons objectAtIndex:0];
  490. [favoriteButton centerIconOverText];
  491. }
  492. // RIGHT
  493. ((CCCellMain *)cell).rightButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:[UIColor whiteColor]] backgroundColor:[UIColor redColor] padding:25]];
  494. ((CCCellMain *)cell).rightExpansion.buttonIndex = 0;
  495. ((CCCellMain *)cell).rightExpansion.fillOnTrigger = NO;
  496. //centerIconOverText
  497. MGSwipeButton *deleteButton = (MGSwipeButton *)[((CCCellMain *)cell).rightButtons objectAtIndex:0];
  498. [deleteButton centerIconOverText];
  499. }
  500. // TRANSFER
  501. if ([cell isKindOfClass:[CCCellMainTransfer class]]) {
  502. // gesture Transfer
  503. [((CCCellMainTransfer *)cell).transferButton.stopButton addTarget:self action:@selector(cancelTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  504. UILongPressGestureRecognizer *stopLongGesture = [UILongPressGestureRecognizer new];
  505. [stopLongGesture addTarget:self action:@selector(cancelAllTask:)];
  506. [((CCCellMainTransfer *)cell).transferButton.stopButton addGestureRecognizer:stopLongGesture];
  507. }
  508. return cell;
  509. }
  510. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  511. {
  512. // deselect row
  513. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  514. self.metadata = [self setSelfMetadataFromIndexPath:indexPath];
  515. // if is in download [do not touch]
  516. if (self.metadata.status == k_metadataStatusWaitDownload || self.metadata.status == k_metadataStatusInDownload || self.metadata.status == k_metadataStatusDownloading)
  517. return;
  518. // File
  519. if (self.metadata.directory == NO) {
  520. // File do not exists
  521. if ([CCUtility fileProviderStorageExists:self.metadata.ocId fileNameView:self.metadata.fileNameView]) {
  522. [[NCNetworkingMain sharedInstance] downloadFileSuccessFailure:self.metadata.fileName ocId:self.metadata.ocId serverUrl:self.metadata.serverUrl selector:selectorLoadFileView errorMessage:@"" errorCode:0];
  523. } else {
  524. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, self.metadata.serverUrl]];
  525. if (tableDirectory.e2eEncrypted && ![CCUtility isEndToEndEnabled:appDelegate.activeAccount]) {
  526. [appDelegate messageNotification:@"_info_" description:@"_e2e_goto_settings_for_enable_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
  527. } else {
  528. if (([self.metadata.typeFile isEqualToString: k_metadataTypeFile_video] || [self.metadata.typeFile isEqualToString: k_metadataTypeFile_audio] || [_metadata.typeFile isEqualToString: k_metadataTypeFile_image]) && self.metadata.e2eEncrypted == NO) {
  529. [self shouldPerformSegue:self.metadata];
  530. } else {
  531. self.metadata.session = k_download_session;
  532. self.metadata.sessionError = @"";
  533. self.metadata.sessionSelector = selectorLoadFileView;
  534. self.metadata.status = k_metadataStatusWaitDownload;
  535. // Add Metadata for Download
  536. tableMetadata *metadata = [[NCManageDatabase sharedInstance] addMetadata:self.metadata];
  537. [[CCNetworking sharedNetworking] downloadFile:metadata taskStatus:k_taskStatusResume];
  538. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:self.metadata.serverUrl ocId:self.metadata.ocId action:k_action_MOD];
  539. }
  540. }
  541. }
  542. }
  543. // Directory
  544. if (self.metadata.directory)
  545. [self performSegueDirectoryWithControlPasscode];
  546. }
  547. -(void)performSegueDirectoryWithControlPasscode
  548. {
  549. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  550. vc.serverUrl = [CCUtility stringAppendServerUrl:self.metadata.serverUrl addFileName:self.metadata.fileName];
  551. vc.titleViewControl = self.metadata.fileNameView;
  552. [self.navigationController pushViewController:vc animated:YES];
  553. }
  554. #pragma --------------------------------------------------------------------------------------------
  555. #pragma mark ===== Navigation ====
  556. #pragma --------------------------------------------------------------------------------------------
  557. - (void)shouldPerformSegue:(tableMetadata *)metadata
  558. {
  559. // if i am in background -> exit
  560. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return;
  561. // if i am not window -> exit
  562. if (self.view.window == NO)
  563. return;
  564. // Collapsed but i am in detail -> exit
  565. if (self.splitViewController.isCollapsed)
  566. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return;
  567. // Metadata for push detail
  568. self.metadataForPushDetail = metadata;
  569. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  570. }
  571. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  572. {
  573. id viewController = segue.destinationViewController;
  574. if ([viewController isKindOfClass:[UINavigationController class]]) {
  575. UINavigationController *nav = viewController;
  576. _detailViewController = (CCDetail *)nav.topViewController;
  577. } else {
  578. _detailViewController = segue.destinationViewController;
  579. }
  580. NSMutableArray *photoDataSource = [NSMutableArray new];
  581. for (NSString *ocId in sectionDataSource.allOcId) {
  582. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:ocId];
  583. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  584. [photoDataSource addObject:metadata];
  585. }
  586. _detailViewController.metadataDetail = self.metadataForPushDetail;
  587. _detailViewController.dateFilterQuery = nil;
  588. _detailViewController.photoDataSource = photoDataSource;
  589. [_detailViewController setTitle:self.metadata.fileNameView];
  590. }
  591. @end