CCFavorites.m 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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.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] setMetadataFavoriteWithOcId:metadata.ocId favorite:favorite];
  140. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl ocId:metadata.ocId action:k_action_MOD];
  141. } else if (errorCode != 0) {
  142. [appDelegate messageNotification:@"_error_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  143. } else {
  144. NSLog(@"[LOG] It has been changed user during networking process, error.");
  145. }
  146. }];
  147. }
  148. #pragma --------------------------------------------------------------------------------------------
  149. #pragma mark ===== listingFavorites =====
  150. #pragma--------------------------------------------------------------------------------------------
  151. - (void)listingFavorites
  152. {
  153. // test
  154. if (appDelegate.activeAccount.length == 0)
  155. return;
  156. [[OCNetworking sharedManager] listingFavoritesWithAccount:appDelegate.activeAccount completion:^(NSString *account, NSArray *metadatas, NSString *message, NSInteger errorCode) {
  157. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  158. NSString *father = @"";
  159. NSMutableArray *filesEtag = [NSMutableArray new];
  160. for (tableMetadata *metadata in metadatas) {
  161. // insert for test NOT favorite
  162. [filesEtag addObject:metadata.ocId];
  163. NSString *serverUrl = metadata.serverUrl;
  164. NSString *serverUrlSon = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName];
  165. if (![serverUrlSon containsString:father]) {
  166. if (metadata.directory) {
  167. if ([CCUtility getFavoriteOffline])
  168. [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName] selector:selectorReadFolderWithDownload account:account];
  169. else
  170. [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName] selector:selectorReadFolder account:account];
  171. } else {
  172. if ([CCUtility getFavoriteOffline])
  173. [[CCSynchronize sharedSynchronize] readFile:metadata.ocId fileName:metadata.fileName serverUrl:serverUrl selector:selectorReadFileWithDownload account:account];
  174. else
  175. [[CCSynchronize sharedSynchronize] readFile:metadata.ocId fileName:metadata.fileName serverUrl:serverUrl selector:selectorReadFile account:account];
  176. }
  177. father = serverUrlSon;
  178. }
  179. }
  180. // Verify remove favorite
  181. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", account] sorted:nil ascending:NO];
  182. for (tableMetadata *metadata in allRecordFavorite)
  183. if (![filesEtag containsObject:metadata.ocId])
  184. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithOcId:metadata.ocId favorite:NO];
  185. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"clearDateReadDataSource" object:nil];
  186. } else if (errorCode != 0) {
  187. [appDelegate messageNotification:@"_error_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  188. } else {
  189. NSLog(@"[LOG] It has been changed user during networking process, error.");
  190. }
  191. }];
  192. }
  193. - (void)tapActionComment:(UITapGestureRecognizer *)tapGesture
  194. {
  195. CGPoint location = [tapGesture locationInView:self.tableView];
  196. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  197. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  198. if (metadata) {
  199. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:1];
  200. }
  201. }
  202. - (void)tapActionShared:(UITapGestureRecognizer *)tapGesture
  203. {
  204. CGPoint location = [tapGesture locationInView:self.tableView];
  205. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  206. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  207. if (metadata) {
  208. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:2];
  209. }
  210. }
  211. #pragma --------------------------------------------------------------------------------------------
  212. #pragma mark ===== Progress & Task Button =====
  213. #pragma --------------------------------------------------------------------------------------------
  214. - (void)triggerProgressTask:(NSNotification *)notification
  215. {
  216. if (sectionDataSource.ocIdIndexPath != nil) {
  217. [[NCMainCommon sharedInstance] triggerProgressTask:notification sectionDataSourceocIdIndexPath:sectionDataSource.ocIdIndexPath tableView:self.tableView viewController:self serverUrlViewController:self.serverUrl];
  218. }
  219. }
  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. return viewController;
  270. }
  271. return nil;
  272. }
  273. - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
  274. {
  275. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:previewingContext.sourceRect.origin];
  276. [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
  277. }
  278. #pragma mark -
  279. #pragma --------------------------------------------------------------------------------------------
  280. #pragma mark ===== menu action : Favorite, More, Delete [swipe] =====
  281. #pragma --------------------------------------------------------------------------------------------
  282. - (BOOL)canOpenMenuAction:(tableMetadata *)metadata
  283. {
  284. return YES;
  285. }
  286. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction
  287. {
  288. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  289. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  290. return [self canOpenMenuAction:metadata];
  291. }
  292. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell tappedButtonAtIndex:(NSInteger)index direction:(MGSwipeDirection)direction fromExpansion:(BOOL)fromExpansion
  293. {
  294. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  295. if (direction == MGSwipeDirectionRightToLeft) {
  296. [self actionDelete:indexPath];
  297. }
  298. if (direction == MGSwipeDirectionLeftToRight) {
  299. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  300. [self settingFavorite:metadata favorite:NO];
  301. }
  302. return YES;
  303. }
  304. - (void)actionDelete:(NSIndexPath *)indexPath
  305. {
  306. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  307. tableLocalFile *localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  308. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  309. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  310. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND e2eEncrypted == 1 AND serverUrl == %@", appDelegate.activeAccount, metadata.serverUrl]];
  311. [[NCMainCommon sharedInstance ] deleteFileWithMetadatas:[[NSArray alloc] initWithObjects:metadata, nil] e2ee:tableDirectory.e2eEncrypted serverUrl:metadata.serverUrl folderocId:tableDirectory.ocId completion:^(NSInteger errorCode, NSString *message) {
  312. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl ocId:metadata.ocId action:k_action_DEL];
  313. }];
  314. }]];
  315. if (localFile) {
  316. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_local_file_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  317. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  318. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId] error:nil];
  319. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:metadata.serverUrl ocId:metadata.ocId action:k_action_MOD];
  320. }]];
  321. }
  322. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  323. }]];
  324. alertController.popoverPresentationController.sourceView = self.tableView;
  325. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  326. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  327. [alertController.view layoutIfNeeded];
  328. [self presentViewController:alertController animated:YES completion:nil];
  329. }
  330. - (void)actionMore:(UITapGestureRecognizer *)gestureRecognizer
  331. {
  332. CGPoint touch = [gestureRecognizer locationInView:self.tableView];
  333. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touch];
  334. UIImage *iconHeader;
  335. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  336. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.tabBarController.view title:nil];
  337. actionSheet.animationDuration = 0.2;
  338. actionSheet.buttonHeight = 50.0;
  339. actionSheet.cancelButtonHeight = 50.0f;
  340. actionSheet.separatorHeight = 5.0f;
  341. actionSheet.automaticallyTintButtonImages = @(NO);
  342. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].encrypted };
  343. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  344. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:17], NSForegroundColorAttributeName:[UIColor blackColor] };
  345. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor darkGrayColor] };
  346. actionSheet.separatorColor = [NCBrandColor sharedInstance].seperator;
  347. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  348. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  349. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]]) {
  350. iconHeader = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconOcId:metadata.ocId fileNameView:metadata.fileNameView]];
  351. } else {
  352. if (metadata.directory)
  353. iconHeader = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:[NCBrandColor sharedInstance].icon];
  354. else
  355. iconHeader = [UIImage imageNamed:metadata.iconName];
  356. }
  357. [actionSheet addButtonWithTitle: metadata.fileNameView image: iconHeader backgroundColor: [NCBrandColor sharedInstance].tabBar height: 50.0 type: AHKActionSheetButtonTypeDisabled handler: nil
  358. ];
  359. // Favorite : ONLY root
  360. if (_serverUrl == nil) {
  361. [actionSheet addButtonWithTitle: NSLocalizedString(@"_remove_favorites_", nil)
  362. image: [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] multiplier:2 color:[NCBrandColor sharedInstance].yellowFavorite]
  363. backgroundColor: [NCBrandColor sharedInstance].backgroundView
  364. height: 50.0
  365. type: AHKActionSheetButtonTypeDefault
  366. handler: ^(AHKActionSheet *as) {
  367. [self settingFavorite:metadata favorite:NO];
  368. }];
  369. }
  370. // Share
  371. [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) {
  372. [[NCMainCommon sharedInstance] openShareWithViewController:self metadata:metadata indexPage:0];
  373. }];
  374. // NO Directory
  375. if (metadata.directory == NO && [NCBrandOptions sharedInstance].disable_openin_file == NO) {
  376. [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) {
  377. [self.tableView setEditing:NO animated:YES];
  378. [[NCMainCommon sharedInstance] downloadOpenInMetadata:metadata];
  379. }];
  380. }
  381. // Delete
  382. [actionSheet addButtonWithTitle:NSLocalizedString(@"_delete_", nil)
  383. image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:[UIColor redColor]]
  384. backgroundColor:[NCBrandColor sharedInstance].backgroundView
  385. height:50.0
  386. type:AHKActionSheetButtonTypeDestructive
  387. handler:^(AHKActionSheet *as) {
  388. [self actionDelete:indexPath];
  389. }];
  390. [actionSheet show];
  391. }
  392. #pragma --------------------------------------------------------------------------------------------
  393. #pragma mark ==== Table ====
  394. #pragma --------------------------------------------------------------------------------------------
  395. - (tableMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  396. {
  397. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  398. return metadata;
  399. }
  400. - (void)reloadDatasource:(NSString *)ocId action:(NSInteger)action
  401. {
  402. // test
  403. if (appDelegate.activeAccount.length == 0 || self.view.window == nil) {
  404. return;
  405. }
  406. [self queryDatasource];
  407. }
  408. - (void)queryDatasource
  409. {
  410. // test
  411. if (appDelegate.activeAccount.length == 0) {
  412. return;
  413. }
  414. NSArray *recordsTableMetadata;
  415. NSString *sorted = [CCUtility getOrderSettings];
  416. if ([sorted isEqualToString:@"fileName"]) sorted = @"fileName";
  417. // get auto upload folder
  418. autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  419. autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:appDelegate.activeUrl];
  420. if (!_serverUrl) {
  421. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND favorite == true", appDelegate.activeAccount] sorted:nil ascending:NO];
  422. } else {
  423. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, _serverUrl] sorted:nil ascending:NO];
  424. }
  425. sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil filterocId:appDelegate.filterocId filterTypeFileImage:NO filterTypeFileVideo:NO sorted:sorted ascending:[CCUtility getAscendingSettings] activeAccount:appDelegate.activeAccount];
  426. [self.tableView reloadData];
  427. }
  428. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  429. {
  430. return 60;
  431. }
  432. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  433. {
  434. return [[sectionDataSource.sectionArrayRow allKeys] count];
  435. }
  436. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  437. {
  438. return [[sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:section]] count];
  439. }
  440. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  441. {
  442. tableShare *shareCell;
  443. tableMetadata *metadata = [[NCMainCommon sharedInstance] getMetadataFromSectionDataSourceIndexPath:indexPath sectionDataSource:sectionDataSource];
  444. if (metadata == nil || [[NCManageDatabase sharedInstance] isTableInvalidated:metadata]) {
  445. return [CCCellMain new];
  446. }
  447. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, metadata.serverUrl]];
  448. if (directory == nil) {
  449. return [CCCellMain new];
  450. }
  451. tableMetadata *metadataFolder = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", directory.ocId]];
  452. for (tableShare *share in appDelegate.shares) {
  453. if ([share.serverUrl isEqualToString:metadata.serverUrl] && [share.fileName isEqualToString:metadata.fileName]) {
  454. shareCell = share;
  455. break;
  456. }
  457. }
  458. UITableViewCell *cell = [[NCMainCommon sharedInstance] cellForRowAtIndexPath:indexPath tableView:tableView metadata:metadata metadataFolder:metadataFolder serverUrl:self.serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory tableShare:shareCell];
  459. // NORMAL - > MAIN
  460. if ([cell isKindOfClass:[CCCellMain class]]) {
  461. // Comment tap
  462. if (metadata.commentsUnread) {
  463. UITapGestureRecognizer *tapComment = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionComment:)];
  464. [tapComment setNumberOfTapsRequired:1];
  465. ((CCCellMain *)cell).comment.userInteractionEnabled = YES;
  466. [((CCCellMain *)cell).comment addGestureRecognizer:tapComment];
  467. }
  468. // Share add Tap
  469. UITapGestureRecognizer *tapShare = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionShared:)];
  470. [tapShare setNumberOfTapsRequired:1];
  471. ((CCCellMain *)cell).viewShared.userInteractionEnabled = YES;
  472. [((CCCellMain *)cell).viewShared addGestureRecognizer:tapShare];
  473. // More
  474. if ([self canOpenMenuAction:metadata]) {
  475. UITapGestureRecognizer *tapMore = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionMore:)];
  476. [tapMore setNumberOfTapsRequired:1];
  477. ((CCCellMain *)cell).more.userInteractionEnabled = YES;
  478. [((CCCellMain *)cell).more addGestureRecognizer:tapMore];
  479. }
  480. // MGSwipeButton
  481. ((CCCellMain *)cell).delegate = self;
  482. // LEFT : configure ONLY Root Favorites : Remove file/folder Favorites
  483. if (_serverUrl == nil) {
  484. ((CCCellMain *)cell).leftButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] width:50 height:50 color:[UIColor whiteColor]] backgroundColor:[NCBrandColor sharedInstance].yellowFavorite padding:25]];
  485. ((CCCellMain *)cell).leftExpansion.buttonIndex = 0;
  486. ((CCCellMain *)cell).leftExpansion.fillOnTrigger = NO;
  487. //centerIconOverText
  488. MGSwipeButton *favoriteButton = (MGSwipeButton *)[((CCCellMain *)cell).leftButtons objectAtIndex:0];
  489. [favoriteButton centerIconOverText];
  490. }
  491. // RIGHT
  492. ((CCCellMain *)cell).rightButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:[UIColor whiteColor]] backgroundColor:[UIColor redColor] padding:25]];
  493. ((CCCellMain *)cell).rightExpansion.buttonIndex = 0;
  494. ((CCCellMain *)cell).rightExpansion.fillOnTrigger = NO;
  495. //centerIconOverText
  496. MGSwipeButton *deleteButton = (MGSwipeButton *)[((CCCellMain *)cell).rightButtons objectAtIndex:0];
  497. [deleteButton centerIconOverText];
  498. }
  499. // TRANSFER
  500. if ([cell isKindOfClass:[CCCellMainTransfer class]]) {
  501. // gesture Transfer
  502. [((CCCellMainTransfer *)cell).transferButton.stopButton addTarget:self action:@selector(cancelTaskButton:withEvent:) forControlEvents:UIControlEventTouchUpInside];
  503. UILongPressGestureRecognizer *stopLongGesture = [UILongPressGestureRecognizer new];
  504. [stopLongGesture addTarget:self action:@selector(cancelAllTask:)];
  505. [((CCCellMainTransfer *)cell).transferButton.stopButton addGestureRecognizer:stopLongGesture];
  506. }
  507. return cell;
  508. }
  509. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  510. {
  511. // deselect row
  512. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  513. self.metadata = [self setSelfMetadataFromIndexPath:indexPath];
  514. // if is in download [do not touch]
  515. if (self.metadata.status == k_metadataStatusWaitDownload || self.metadata.status == k_metadataStatusInDownload || self.metadata.status == k_metadataStatusDownloading)
  516. return;
  517. // File
  518. if (self.metadata.directory == NO) {
  519. // File do not exists
  520. if ([CCUtility fileProviderStorageExists:self.metadata.ocId fileNameView:self.metadata.fileNameView]) {
  521. [[NCNetworkingMain sharedInstance] downloadFileSuccessFailure:self.metadata.fileName ocId:self.metadata.ocId serverUrl:self.metadata.serverUrl selector:selectorLoadFileView errorMessage:@"" errorCode:0];
  522. } else {
  523. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, self.metadata.serverUrl]];
  524. if (tableDirectory.e2eEncrypted && ![CCUtility isEndToEndEnabled:appDelegate.activeAccount]) {
  525. [appDelegate messageNotification:@"_info_" description:@"_e2e_goto_settings_for_enable_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
  526. } else {
  527. 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) {
  528. [self shouldPerformSegue:self.metadata];
  529. } else {
  530. self.metadata.session = k_download_session;
  531. self.metadata.sessionError = @"";
  532. self.metadata.sessionSelector = selectorLoadFileView;
  533. self.metadata.status = k_metadataStatusWaitDownload;
  534. // Add Metadata for Download
  535. tableMetadata *metadata = [[NCManageDatabase sharedInstance] addMetadata:self.metadata];
  536. [[CCNetworking sharedNetworking] downloadFile:metadata taskStatus:k_taskStatusResume];
  537. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:self.metadata.serverUrl ocId:self.metadata.ocId action:k_action_MOD];
  538. }
  539. }
  540. }
  541. }
  542. // Directory
  543. if (self.metadata.directory)
  544. [self performSegueDirectoryWithControlPasscode];
  545. }
  546. -(void)performSegueDirectoryWithControlPasscode
  547. {
  548. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  549. vc.serverUrl = [CCUtility stringAppendServerUrl:self.metadata.serverUrl addFileName:self.metadata.fileName];
  550. vc.titleViewControl = self.metadata.fileNameView;
  551. [self.navigationController pushViewController:vc animated:YES];
  552. }
  553. #pragma --------------------------------------------------------------------------------------------
  554. #pragma mark ===== Navigation ====
  555. #pragma --------------------------------------------------------------------------------------------
  556. - (void)shouldPerformSegue:(tableMetadata *)metadata
  557. {
  558. // if i am in background -> exit
  559. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return;
  560. // if i am not window -> exit
  561. if (self.view.window == NO)
  562. return;
  563. // Collapsed but i am in detail -> exit
  564. if (self.splitViewController.isCollapsed)
  565. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return;
  566. // Metadata for push detail
  567. self.metadataForPushDetail = metadata;
  568. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  569. }
  570. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  571. {
  572. id viewController = segue.destinationViewController;
  573. if ([viewController isKindOfClass:[UINavigationController class]]) {
  574. UINavigationController *nav = viewController;
  575. _detailViewController = (CCDetail *)nav.topViewController;
  576. } else {
  577. _detailViewController = segue.destinationViewController;
  578. }
  579. NSMutableArray *photoDataSource = [NSMutableArray new];
  580. for (NSString *ocId in sectionDataSource.allOcId) {
  581. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:ocId];
  582. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  583. [photoDataSource addObject:metadata];
  584. }
  585. _detailViewController.metadataDetail = self.metadataForPushDetail;
  586. _detailViewController.dateFilterQuery = nil;
  587. _detailViewController.photoDataSource = photoDataSource;
  588. [_detailViewController setTitle:self.metadata.fileNameView];
  589. }
  590. @end