CCFavorites.m 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. //
  2. // CCFavorites.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 16/01/17.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 () <CCActionsDeleteDelegate, CCActionsSettingFavoriteDelegate>
  28. {
  29. AppDelegate *appDelegate;
  30. NSArray *_dataSource;
  31. BOOL _reloadDataSource;
  32. }
  33. @end
  34. @implementation CCFavorites
  35. #pragma --------------------------------------------------------------------------------------------
  36. #pragma mark ===== Init =====
  37. #pragma --------------------------------------------------------------------------------------------
  38. - (id)initWithCoder:(NSCoder *)aDecoder
  39. {
  40. if (self = [super initWithCoder:aDecoder]) {
  41. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  42. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  43. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  44. appDelegate.activeFavorites = self;
  45. }
  46. return self;
  47. }
  48. - (void)viewDidLoad
  49. {
  50. [super viewDidLoad];
  51. // Custom Cell
  52. [self.tableView registerNib:[UINib nibWithNibName:@"CCFavoritesCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
  53. // dataSource
  54. _dataSource = [NSMutableArray new];
  55. // Metadata
  56. _metadata = [tableMetadata new];
  57. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  58. self.tableView.separatorColor = [NCBrandColor sharedInstance].seperator;
  59. self.tableView.emptyDataSetDelegate = self;
  60. self.tableView.emptyDataSetSource = self;
  61. self.tableView.delegate = self;
  62. // calculate _serverUrl
  63. if (!_serverUrl)
  64. _serverUrl = nil;
  65. // Title
  66. if (_titleViewControl)
  67. self.title = _titleViewControl;
  68. else
  69. self.title = NSLocalizedString(@"_favorites_", nil);
  70. }
  71. // Apparirà
  72. - (void)viewWillAppear:(BOOL)animated
  73. {
  74. [super viewWillAppear:animated];
  75. // Color
  76. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  77. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  78. // Plus Button
  79. [appDelegate plusButtonVisibile:true];
  80. [self reloadDatasource];
  81. }
  82. // E' arrivato
  83. - (void)viewDidAppear:(BOOL)animated
  84. {
  85. [super viewDidAppear:animated];
  86. // Active Main
  87. appDelegate.activeFavorites = self;
  88. }
  89. - (void)changeTheming
  90. {
  91. if (self.isViewLoaded && self.view.window)
  92. [appDelegate changeTheming:self];
  93. // Reload Table View
  94. [self.tableView reloadData];
  95. }
  96. - (void)triggerProgressTask:(NSNotification *)notification
  97. {
  98. //NSDictionary *dict = notification.userInfo;
  99. //float progress = [[dict valueForKey:@"progress"] floatValue];
  100. }
  101. #pragma --------------------------------------------------------------------------------------------
  102. #pragma mark ==== DZNEmptyDataSetSource ====
  103. #pragma --------------------------------------------------------------------------------------------
  104. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  105. {
  106. return [NCBrandColor sharedInstance].backgroundView;
  107. }
  108. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  109. {
  110. return [UIImage imageNamed:@"favoriteNoFiles"];
  111. }
  112. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  113. {
  114. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_favorite_no_files_", nil)];
  115. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  116. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  117. }
  118. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  119. {
  120. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_favorite_view_", nil)];
  121. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  122. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  123. paragraph.alignment = NSTextAlignmentCenter;
  124. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  125. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  126. }
  127. #pragma --------------------------------------------------------------------------------------------
  128. #pragma mark ===== Delete <delegate> =====
  129. #pragma--------------------------------------------------------------------------------------------
  130. - (void)deleteFileOrFolderSuccessFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  131. {
  132. if (errorCode == 0)
  133. [self reloadDatasource];
  134. else
  135. NSLog(@"[LOG] DeleteFileOrFolder failure error %d, %@", (int)errorCode, message);
  136. }
  137. #pragma --------------------------------------------------------------------------------------------
  138. #pragma mark ===== Favorite <delegate> =====
  139. #pragma--------------------------------------------------------------------------------------------
  140. - (void)settingFavoriteFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  141. {
  142. NSLog(@"[LOG] Setting Favorite failure error %d, %@", (int)errorCode, message);
  143. }
  144. - (void)settingFavoriteSuccess:(CCMetadataNet *)metadataNet
  145. {
  146. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadataNet.fileID favorite:[metadataNet.options boolValue]];
  147. [self reloadDatasource];
  148. }
  149. - (void)readListingFavorites
  150. {
  151. // test
  152. if (appDelegate.activeAccount.length == 0)
  153. return;
  154. [[CCActions sharedInstance] listingFavorites:@"" delegate:self];
  155. }
  156. - (void)addFavoriteFolder:(NSString *)serverUrl
  157. {
  158. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  159. if (!directoryID) return;
  160. NSString *selector;
  161. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  162. metadataNet.action = actionReadFolder;
  163. metadataNet.depth = @"1";
  164. metadataNet.directoryID = directoryID;
  165. if ([CCUtility getFavoriteOffline])
  166. selector = selectorReadFolderWithDownload;
  167. else
  168. selector = selectorReadFolder;
  169. metadataNet.selector = selector;
  170. metadataNet.serverUrl = serverUrl;
  171. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:[CCSynchronize sharedSynchronize] metadataNet:metadataNet];
  172. }
  173. - (void)listingFavoritesSuccess:(CCMetadataNet *)metadataNet metadatas:(NSArray *)metadatas
  174. {
  175. // Check Active Account
  176. if (![metadataNet.account isEqualToString:appDelegate.activeAccount])
  177. return;
  178. NSString *father = @"";
  179. NSMutableArray *filesEtag = [NSMutableArray new];
  180. for (tableMetadata *metadata in metadatas) {
  181. // insert for test NOT favorite
  182. [filesEtag addObject:metadata.fileID];
  183. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  184. NSString *serverUrlSon = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName];
  185. if (![serverUrlSon containsString:father]) {
  186. if (metadata.directory) {
  187. if ([CCUtility getFavoriteOffline])
  188. [[CCSynchronize sharedSynchronize] readFileForFolder:metadata.fileName serverUrl:serverUrl selector:selectorReadFileFolderWithDownload];
  189. else
  190. [[CCSynchronize sharedSynchronize] readFileForFolder:metadata.fileName serverUrl:serverUrl selector:selectorReadFileFolder];
  191. } else {
  192. if ([CCUtility getFavoriteOffline])
  193. [[CCSynchronize sharedSynchronize] readFile:metadata selector:selectorReadFileWithDownload];
  194. else
  195. [[CCSynchronize sharedSynchronize] readFile:metadata selector:selectorReadFile];
  196. }
  197. father = serverUrlSon;
  198. }
  199. }
  200. // Verify remove favorite
  201. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND favorite = true", appDelegate.activeAccount] sorted:nil ascending:NO];
  202. for (tableMetadata *metadata in allRecordFavorite)
  203. if (![filesEtag containsObject:metadata.fileID])
  204. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadata.fileID favorite:NO];
  205. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"clearDateReadDataSource" object:nil];
  206. }
  207. - (void)listingFavoritesFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  208. {
  209. // Check Active Account
  210. if (![metadataNet.account isEqualToString:appDelegate.activeAccount])
  211. return;
  212. NSLog(@"[LOG] Listing Favorites failure error %d, %@", (int)errorCode, message);
  213. }
  214. #pragma --------------------------------------------------------------------------------------------
  215. #pragma mark ==== Download Thumbnail <Delegate> ====
  216. #pragma --------------------------------------------------------------------------------------------
  217. - (void)downloadThumbnailSuccess:(CCMetadataNet *)metadataNet
  218. {
  219. // Check Active Account
  220. if (![metadataNet.account isEqualToString:appDelegate.activeAccount])
  221. return;
  222. [self reloadDatasource];
  223. }
  224. #pragma --------------------------------------------------------------------------------------------
  225. #pragma mark ==== Download <Delegate> ====
  226. #pragma --------------------------------------------------------------------------------------------
  227. - (void)downloadFileSuccessFailure:(NSString *)fileName fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost errorMessage:(NSString *)errorMessage errorCode:(NSInteger)errorCode
  228. {
  229. if (errorCode == 0) {
  230. _metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", fileID]];
  231. if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_compress]) {
  232. //[self performSelector:@selector(unZipFile:) withObject:_metadata.fileID];
  233. [self openWith:_metadata];
  234. } else if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_unknown]) {
  235. [self openWith:_metadata];
  236. } else {
  237. if ([self shouldPerformSegue])
  238. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  239. }
  240. } else {
  241. if (errorCode != k_CCErrorFileAlreadyInDownload)
  242. [appDelegate messageNotification:@"_download_file_" description:errorMessage visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  243. }
  244. }
  245. - (void)openWith:(tableMetadata *)metadata
  246. {
  247. NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, metadata.fileID];
  248. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePath]) {
  249. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNameView] error:nil];
  250. [[NSFileManager defaultManager] linkItemAtPath:fileNamePath toPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNameView] error:nil];
  251. NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNameView]];
  252. _docController = [UIDocumentInteractionController interactionControllerWithURL:url];
  253. _docController.delegate = self;
  254. [_docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  255. }
  256. }
  257. - (void)requestDeleteMetadata:(tableMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  258. {
  259. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  260. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  261. [[CCActions sharedInstance] deleteFileOrFolder:metadata delegate:self hud:nil hudTitled:nil];
  262. [self reloadDatasource];
  263. }]];
  264. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  265. }]];
  266. alertController.popoverPresentationController.sourceView = self.view;
  267. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  268. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  269. [alertController.view layoutIfNeeded];
  270. [self presentViewController:alertController animated:YES completion:nil];
  271. }
  272. - (void)requestMoreMetadata:(tableMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  273. {
  274. UIImage *iconHeader;
  275. metadata = [_dataSource objectAtIndex:indexPath.row];
  276. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.tabBarController.view title:nil];
  277. actionSheet.animationDuration = 0.2;
  278. actionSheet.buttonHeight = 50.0;
  279. actionSheet.cancelButtonHeight = 50.0f;
  280. actionSheet.separatorHeight = 5.0f;
  281. actionSheet.automaticallyTintButtonImages = @(NO);
  282. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].encrypted };
  283. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  284. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:17], NSForegroundColorAttributeName:[UIColor blackColor] };
  285. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor darkGrayColor] };
  286. actionSheet.separatorColor = [NCBrandColor sharedInstance].seperator;
  287. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  288. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  289. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, metadata.fileID]]) {
  290. iconHeader = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, metadata.fileID]];
  291. } else {
  292. if (metadata.directory)
  293. iconHeader = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] color:[NCBrandColor sharedInstance].brandElement];
  294. else
  295. iconHeader = [UIImage imageNamed:metadata.iconName];
  296. }
  297. [actionSheet addButtonWithTitle: metadata.fileNameView image: iconHeader backgroundColor: [NCBrandColor sharedInstance].tabBar height: 50.0 type: AHKActionSheetButtonTypeDisabled handler: nil
  298. ];
  299. // Share
  300. [actionSheet addButtonWithTitle:NSLocalizedString(@"_share_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"actionSheetShare"] color:[NCBrandColor sharedInstance].brandElement] backgroundColor:[NCBrandColor sharedInstance].backgroundView height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  301. [appDelegate.activeMain openWindowShare:metadata];
  302. }];
  303. // NO Directory
  304. if (metadata.directory == NO) {
  305. [actionSheet addButtonWithTitle:NSLocalizedString(@"_open_in_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"actionSheetOpenIn"] color:[NCBrandColor sharedInstance].brandElement] backgroundColor:[NCBrandColor sharedInstance].backgroundView height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  306. [self.tableView setEditing:NO animated:YES];
  307. [self openWith:metadata];
  308. }];
  309. }
  310. [actionSheet show];
  311. }
  312. - (void)tapActionConnectionMounted:(UITapGestureRecognizer *)tapGesture
  313. {
  314. CGPoint location = [tapGesture locationInView:self.tableView];
  315. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  316. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  317. if (metadata)
  318. [appDelegate.activeMain openWindowShare:metadata];
  319. }
  320. #pragma mark -
  321. #pragma --------------------------------------------------------------------------------------------
  322. #pragma mark ===== Swipe Tablet -> menu =====
  323. #pragma --------------------------------------------------------------------------------------------
  324. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction
  325. {
  326. return YES;
  327. }
  328. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell tappedButtonAtIndex:(NSInteger)index direction:(MGSwipeDirection)direction fromExpansion:(BOOL)fromExpansion
  329. {
  330. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  331. if (direction == MGSwipeDirectionRightToLeft) {
  332. // Delete
  333. if (index == 0)
  334. [self requestDeleteMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  335. // More
  336. if (index == 1)
  337. [self requestMoreMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  338. }
  339. if (direction == MGSwipeDirectionLeftToRight) {
  340. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  341. [[CCActions sharedInstance] settingFavorite:metadata favorite:NO delegate:self];
  342. }
  343. return YES;
  344. }
  345. #pragma --------------------------------------------------------------------------------------------
  346. #pragma mark ==== Table ====
  347. #pragma --------------------------------------------------------------------------------------------
  348. - (tableMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  349. {
  350. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  351. return metadata;
  352. }
  353. - (void)readFolder:(NSString *)serverUrl
  354. {
  355. [self reloadDatasource];
  356. }
  357. - (void)reloadDatasource
  358. {
  359. NSMutableArray *metadatas = [NSMutableArray new];
  360. NSArray *recordsTableMetadata ;
  361. NSString *sorted = [CCUtility getOrderSettings];
  362. if ([sorted isEqualToString:@"fileName"])
  363. sorted = @"fileName";
  364. if (!_serverUrl) {
  365. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND favorite = true", appDelegate.activeAccount] sorted:sorted ascending:[CCUtility getAscendingSettings]];
  366. } else {
  367. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  368. if (directoryID)
  369. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@", appDelegate.activeAccount, directoryID] sorted:sorted ascending:[CCUtility getAscendingSettings]];
  370. }
  371. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil e2eEncryptions:nil groupByField:nil activeAccount:appDelegate.activeAccount];
  372. NSArray *fileIDs = [sectionDataSource.sectionArrayRow objectForKey:@"_none_"];
  373. for (NSString *fileID in fileIDs)
  374. [metadatas addObject:[sectionDataSource.allRecordsDataSource objectForKey:fileID]];
  375. _dataSource = [NSArray arrayWithArray:metadatas];
  376. [self.tableView reloadData];
  377. }
  378. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  379. {
  380. return 60;
  381. }
  382. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  383. {
  384. return 1;
  385. }
  386. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  387. {
  388. return [_dataSource count];
  389. }
  390. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  391. {
  392. CCFavoritesCell *cell = (CCFavoritesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  393. tableMetadata *metadata;
  394. // variable base
  395. cell.delegate = self;
  396. cell.indexPath = indexPath;
  397. // separator
  398. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  399. // Initialize
  400. cell.status.image = nil;
  401. cell.favorite.image = nil;
  402. cell.local.image = nil;
  403. cell.shared.image = nil;
  404. // change color selection
  405. UIView *selectionColor = [[UIView alloc] init];
  406. selectionColor.backgroundColor = [[NCBrandColor sharedInstance] getColorSelectBackgrond];
  407. cell.selectedBackgroundView = selectionColor;
  408. metadata = [_dataSource objectAtIndex:indexPath.row];
  409. // favorite
  410. if (_serverUrl == nil)
  411. cell.favorite.image = [UIImage imageNamed:@"favorite"];
  412. cell.labelTitle.textColor = [UIColor blackColor];
  413. // filename
  414. cell.labelTitle.text = metadata.fileNameView;
  415. cell.labelInfoFile.text = @"";
  416. // Shared
  417. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  418. if (!serverUrl)
  419. return cell;
  420. NSString *shareLink = [appDelegate.sharesLink objectForKey:[serverUrl stringByAppendingString:metadata.fileName]];
  421. NSString *shareUserAndGroup = [appDelegate.sharesUserAndGroup objectForKey:[serverUrl stringByAppendingString:metadata.fileName]];
  422. // Immage
  423. if (metadata.directory) {
  424. if ([shareLink length] > 0) {
  425. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder_public"] color:[NCBrandColor sharedInstance].brandElement];
  426. } else if ([shareUserAndGroup length] > 0) {
  427. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder_shared_with_me"] color:[NCBrandColor sharedInstance].brandElement];
  428. } else {
  429. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] color:[NCBrandColor sharedInstance].brandElement];
  430. }
  431. } else {
  432. if ([shareLink length] > 0 || [shareUserAndGroup length] > 0) {
  433. if ([shareLink length] > 0)
  434. cell.shared.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"shareLink"] color:[NCBrandColor sharedInstance].brandElement];
  435. else
  436. cell.shared.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"actionSheetShare"] color:[NCBrandColor sharedInstance].brandElement];
  437. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionConnectionMounted:)];
  438. [tap setNumberOfTapsRequired:1];
  439. cell.shared.userInteractionEnabled = YES;
  440. [cell.shared addGestureRecognizer:tap];
  441. }
  442. cell.file.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, metadata.fileID]];
  443. if (cell.file.image == nil) {
  444. cell.file.image = [UIImage imageNamed:metadata.iconName];
  445. if (metadata.thumbnailExists)
  446. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  447. }
  448. // ----------------------------------------------------------------------------------------------------------
  449. // E2EE Image Status Encrypted
  450. // ----------------------------------------------------------------------------------------------------------
  451. tableE2eEncryption *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND fileNameIdentifier = %@", appDelegate.activeAccount, metadata.fileName]];
  452. if (tableE2eEncryption)
  453. cell.status.image = [UIImage imageNamed:@"encrypted"];
  454. }
  455. // text and length
  456. if (metadata.directory) {
  457. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  458. //cell.accessoryType = UITableViewCellAccessoryNone;
  459. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  460. } else {
  461. NSString *date = [CCUtility dateDiff:metadata.date];
  462. NSString *length = [CCUtility transformedSize:metadata.size];
  463. BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, metadata.fileID]];
  464. if (fileExists)
  465. cell.local.image = [UIImage imageNamed:@"local"];
  466. else
  467. cell.local.image = nil;
  468. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ %@", date, length];
  469. cell.accessoryType = UITableViewCellAccessoryNone;
  470. }
  471. // ======== MGSwipe ========
  472. //configure left buttons : ONLY Root Favorites : Remove file/folder Favorites
  473. if (_serverUrl == nil) {
  474. cell.leftButtons = @[[MGSwipeButton buttonWithTitle:[NSString stringWithFormat:@" %@ ", NSLocalizedString(@"_unfavorite_", nil)] icon:[UIImage imageNamed:@"swipeUnfavorite"] backgroundColor:[UIColor colorWithRed:242.0/255.0 green:220.0/255.0 blue:132.0/255.0 alpha:1.000]]];
  475. cell.leftExpansion.buttonIndex = 0;
  476. cell.leftExpansion.fillOnTrigger = NO;
  477. //centerIconOverText
  478. MGSwipeButton *favoriteButton = (MGSwipeButton *)[cell.leftButtons objectAtIndex:0];
  479. [favoriteButton centerIconOverText];
  480. }
  481. //configure right buttons
  482. cell.rightButtons = @[[MGSwipeButton buttonWithTitle:[NSString stringWithFormat:@" %@ ", NSLocalizedString(@"_delete_", nil)] icon:[UIImage imageNamed:@"swipeDelete"] backgroundColor:[UIColor redColor]], [MGSwipeButton buttonWithTitle:[NSString stringWithFormat:@" %@ ", NSLocalizedString(@"_more_", nil)] icon:[UIImage imageNamed:@"swipeMore"] backgroundColor:[UIColor lightGrayColor]]];
  483. cell.rightSwipeSettings.transition = MGSwipeTransitionBorder;
  484. //centerIconOverText
  485. MGSwipeButton *deleteButton = (MGSwipeButton *)[cell.rightButtons objectAtIndex:0];
  486. MGSwipeButton *moreButton = (MGSwipeButton *)[cell.rightButtons objectAtIndex:1];
  487. [deleteButton centerIconOverText];
  488. [moreButton centerIconOverText];
  489. return cell;
  490. }
  491. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  492. {
  493. // deselect row
  494. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  495. _metadata = [self setSelfMetadataFromIndexPath:indexPath];
  496. // if is in download [do not touch]
  497. if ([_metadata.session length] > 0 && [_metadata.session containsString:@"download"])
  498. return;
  499. // File
  500. if (_metadata.directory == NO) {
  501. // File do not exists
  502. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  503. if (serverUrl) {
  504. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, _metadata.fileID]]) {
  505. [self downloadFileSuccessFailure:_metadata.fileName fileID:_metadata.fileID serverUrl:serverUrl selector:selectorLoadFileView selectorPost:@"" errorMessage:@"" errorCode:0];
  506. } else {
  507. [[CCNetworking sharedNetworking] downloadFile:_metadata.fileName fileID:_metadata.fileID serverUrl:serverUrl selector:selectorLoadFileView selectorPost:nil session:k_download_session taskStatus:k_taskStatusResume delegate:self];
  508. }
  509. }
  510. }
  511. // Directory
  512. if (_metadata.directory)
  513. [self performSegueDirectoryWithControlPasscode];
  514. }
  515. -(void)performSegueDirectoryWithControlPasscode
  516. {
  517. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  518. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  519. if (serverUrl) {
  520. vc.serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:_metadata.fileName];
  521. vc.titleViewControl = _metadata.fileNameView;
  522. [self.navigationController pushViewController:vc animated:YES];
  523. }
  524. }
  525. #pragma --------------------------------------------------------------------------------------------
  526. #pragma mark ===== Navigation ====
  527. #pragma --------------------------------------------------------------------------------------------
  528. - (BOOL)shouldPerformSegue
  529. {
  530. // if i am in background -> exit
  531. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  532. // if i am not window -> exit
  533. if (self.view.window == NO)
  534. return NO;
  535. // Collapsed but i am in detail -> exit
  536. if (self.splitViewController.isCollapsed)
  537. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  538. return YES;
  539. }
  540. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  541. {
  542. id viewController = segue.destinationViewController;
  543. if ([viewController isKindOfClass:[UINavigationController class]]) {
  544. UINavigationController *nav = viewController;
  545. _detailViewController = (CCDetail *)nav.topViewController;
  546. } else {
  547. _detailViewController = segue.destinationViewController;
  548. }
  549. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  550. for (tableMetadata *metadata in _dataSource) {
  551. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  552. [allRecordsDataSourceImagesVideos addObject:metadata];
  553. }
  554. _detailViewController.metadataDetail = _metadata;
  555. _detailViewController.dateFilterQuery = nil;
  556. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  557. [_detailViewController setTitle:_metadata.fileNameView];
  558. }
  559. @end