CCFavorites.m 36 KB

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