CCFavorites.m 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 [UIColor whiteColor];
  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)deleteFileOrFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  131. {
  132. NSLog(@"[LOG] DeleteFileOrFolder failure error %lu, %@", (long)errorCode, message);
  133. }
  134. - (void)deleteFileOrFolderSuccess:(CCMetadataNet *)metadataNet
  135. {
  136. [self reloadDatasource];
  137. }
  138. #pragma --------------------------------------------------------------------------------------------
  139. #pragma mark ===== Favorite <delegate> =====
  140. #pragma--------------------------------------------------------------------------------------------
  141. - (void)settingFavoriteFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  142. {
  143. NSLog(@"[LOG] Setting Favorite failure error %lu, %@", (long)errorCode, message);
  144. }
  145. - (void)settingFavoriteSuccess:(CCMetadataNet *)metadataNet
  146. {
  147. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadataNet.fileID favorite:[metadataNet.options boolValue]];
  148. [self reloadDatasource];
  149. }
  150. - (void)readListingFavorites
  151. {
  152. // test
  153. if (appDelegate.activeAccount.length == 0)
  154. return;
  155. [[CCActions sharedInstance] listingFavorites:@"" delegate:self];
  156. }
  157. - (void)addFavoriteFolder:(NSString *)serverUrl
  158. {
  159. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  160. if (!directoryID) return;
  161. NSString *selector;
  162. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  163. metadataNet.action = actionReadFolder;
  164. metadataNet.depth = @"1";
  165. metadataNet.directoryID = directoryID;
  166. if ([CCUtility getFavoriteOffline])
  167. selector = selectorReadFolderWithDownload;
  168. else
  169. selector = selectorReadFolder;
  170. metadataNet.selector = selector;
  171. metadataNet.serverUrl = serverUrl;
  172. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:[CCSynchronize sharedSynchronize] metadataNet:metadataNet];
  173. }
  174. - (void)listingFavoritesSuccess:(CCMetadataNet *)metadataNet metadatas:(NSArray *)metadatas
  175. {
  176. // verify active user
  177. tableAccount *record = [[NCManageDatabase sharedInstance] getAccountActive];
  178. if (![record.account isEqualToString:metadataNet.account])
  179. return;
  180. NSString *father = @"";
  181. NSMutableArray *filesEtag = [NSMutableArray new];
  182. for (tableMetadata *metadata in metadatas) {
  183. // insert for test NOT favorite
  184. [filesEtag addObject:metadata.fileID];
  185. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  186. NSString *serverUrlSon = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName];
  187. if (![serverUrlSon containsString:father]) {
  188. if (metadata.directory) {
  189. if ([CCUtility getFavoriteOffline])
  190. [[CCSynchronize sharedSynchronize] readFileForFolder:metadata.fileName serverUrl:serverUrl selector:selectorReadFileFolderWithDownload];
  191. else
  192. [[CCSynchronize sharedSynchronize] readFileForFolder:metadata.fileName serverUrl:serverUrl selector:selectorReadFileFolder];
  193. } else {
  194. if ([CCUtility getFavoriteOffline])
  195. [[CCSynchronize sharedSynchronize] readFile:metadata selector:selectorReadFileWithDownload];
  196. else
  197. [[CCSynchronize sharedSynchronize] readFile:metadata selector:selectorReadFile];
  198. }
  199. father = serverUrlSon;
  200. }
  201. }
  202. // Verify remove favorite
  203. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND favorite = true", appDelegate.activeAccount] sorted:nil ascending:NO];
  204. for (tableMetadata *metadata in allRecordFavorite)
  205. if (![filesEtag containsObject:metadata.fileID])
  206. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadata.fileID favorite:NO];
  207. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"clearDateReadDataSource" object:nil];
  208. }
  209. - (void)listingFavoritesFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  210. {
  211. NSLog(@"[LOG] Listing Favorites failure error %lu, %@", (long)errorCode, message);
  212. }
  213. #pragma --------------------------------------------------------------------------------------------
  214. #pragma mark ==== Download Thumbnail <Delegate> ====
  215. #pragma --------------------------------------------------------------------------------------------
  216. - (void)downloadThumbnailSuccess:(CCMetadataNet *)metadataNet
  217. {
  218. [self reloadDatasource];
  219. }
  220. #pragma --------------------------------------------------------------------------------------------
  221. #pragma mark ==== Download <Delegate> ====
  222. #pragma --------------------------------------------------------------------------------------------
  223. - (void)downloadFileSuccessFailure:(NSString *)fileName fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost errorMessage:(NSString *)errorMessage errorCode:(NSInteger)errorCode
  224. {
  225. if (errorCode == 0) {
  226. _metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", fileID]];
  227. if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_compress]) {
  228. //[self performSelector:@selector(unZipFile:) withObject:_metadata.fileID];
  229. [self openWith:_metadata];
  230. } else if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_unknown]) {
  231. [self openWith:_metadata];
  232. } else {
  233. if ([self shouldPerformSegue])
  234. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  235. }
  236. } else {
  237. if (errorCode != k_CCErrorFileAlreadyInDownload)
  238. [appDelegate messageNotification:@"_download_file_" description:errorMessage visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  239. }
  240. }
  241. - (void)openWith:(tableMetadata *)metadata
  242. {
  243. NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, metadata.fileID];
  244. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePath]) {
  245. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNameView] error:nil];
  246. [[NSFileManager defaultManager] linkItemAtPath:fileNamePath toPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNameView] error:nil];
  247. NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNameView]];
  248. _docController = [UIDocumentInteractionController interactionControllerWithURL:url];
  249. _docController.delegate = self;
  250. [_docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  251. }
  252. }
  253. - (void)requestDeleteMetadata:(tableMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  254. {
  255. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  256. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  257. [[CCActions sharedInstance] deleteFileOrFolder:metadata delegate:self];
  258. [self reloadDatasource];
  259. }]];
  260. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  261. }]];
  262. alertController.popoverPresentationController.sourceView = self.view;
  263. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  264. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  265. [alertController.view layoutIfNeeded];
  266. [self presentViewController:alertController animated:YES completion:nil];
  267. }
  268. - (void)requestMoreMetadata:(tableMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  269. {
  270. UIImage *iconHeader;
  271. metadata = [_dataSource objectAtIndex:indexPath.row];
  272. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.tabBarController.view title:nil];
  273. actionSheet.animationDuration = 0.2;
  274. actionSheet.buttonHeight = 50.0;
  275. actionSheet.cancelButtonHeight = 50.0f;
  276. actionSheet.separatorHeight = 5.0f;
  277. actionSheet.automaticallyTintButtonImages = @(NO);
  278. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].encrypted };
  279. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  280. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:17], NSForegroundColorAttributeName:[UIColor blackColor] };
  281. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor darkGrayColor] };
  282. actionSheet.separatorColor = [NCBrandColor sharedInstance].seperator;
  283. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  284. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  285. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, metadata.fileID]]) {
  286. iconHeader = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, metadata.fileID]];
  287. } else {
  288. if (metadata.directory)
  289. iconHeader = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] color:[NCBrandColor sharedInstance].brand];
  290. else
  291. iconHeader = [UIImage imageNamed:metadata.iconName];
  292. }
  293. [actionSheet addButtonWithTitle: metadata.fileNameView image: iconHeader backgroundColor: [NCBrandColor sharedInstance].tabBar height: 50.0 type: AHKActionSheetButtonTypeDisabled handler: nil
  294. ];
  295. // Share
  296. [actionSheet addButtonWithTitle:NSLocalizedString(@"_share_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"actionSheetShare"] color:[NCBrandColor sharedInstance].brand] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  297. [appDelegate.activeMain openWindowShare:metadata];
  298. }];
  299. // NO Directory
  300. if (metadata.directory == NO) {
  301. [actionSheet addButtonWithTitle:NSLocalizedString(@"_open_in_", nil) image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"actionSheetOpenIn"] color:[NCBrandColor sharedInstance].brand] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  302. [self.tableView setEditing:NO animated:YES];
  303. [self openWith:metadata];
  304. }];
  305. }
  306. [actionSheet show];
  307. }
  308. - (void)tapActionConnectionMounted:(UITapGestureRecognizer *)tapGesture
  309. {
  310. CGPoint location = [tapGesture locationInView:self.tableView];
  311. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  312. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  313. if (metadata)
  314. [appDelegate.activeMain openWindowShare:metadata];
  315. }
  316. #pragma mark -
  317. #pragma --------------------------------------------------------------------------------------------
  318. #pragma mark ===== Swipe Tablet -> menu =====
  319. #pragma --------------------------------------------------------------------------------------------
  320. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction
  321. {
  322. return YES;
  323. }
  324. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell tappedButtonAtIndex:(NSInteger)index direction:(MGSwipeDirection)direction fromExpansion:(BOOL)fromExpansion
  325. {
  326. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  327. if (direction == MGSwipeDirectionRightToLeft) {
  328. // Delete
  329. if (index == 0)
  330. [self requestDeleteMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  331. // More
  332. if (index == 1)
  333. [self requestMoreMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  334. }
  335. if (direction == MGSwipeDirectionLeftToRight) {
  336. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  337. [[CCActions sharedInstance] settingFavorite:metadata favorite:NO delegate:self];
  338. }
  339. return YES;
  340. }
  341. #pragma --------------------------------------------------------------------------------------------
  342. #pragma mark ==== Table ====
  343. #pragma --------------------------------------------------------------------------------------------
  344. - (tableMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  345. {
  346. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  347. return metadata;
  348. }
  349. - (void)readFolder:(NSString *)serverUrl
  350. {
  351. [self reloadDatasource];
  352. }
  353. - (void)reloadDatasource
  354. {
  355. NSMutableArray *metadatas = [NSMutableArray new];
  356. NSArray *recordsTableMetadata ;
  357. NSString *sorted = [CCUtility getOrderSettings];
  358. if ([sorted isEqualToString:@"fileName"])
  359. sorted = @"fileName";
  360. if (!_serverUrl) {
  361. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND favorite = true", appDelegate.activeAccount] sorted:sorted ascending:[CCUtility getAscendingSettings]];
  362. } else {
  363. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  364. if (directoryID)
  365. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@", appDelegate.activeAccount, directoryID] sorted:sorted ascending:[CCUtility getAscendingSettings]];
  366. }
  367. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil e2eEncryptions:nil groupByField:nil activeAccount:appDelegate.activeAccount];
  368. NSArray *fileIDs = [sectionDataSource.sectionArrayRow objectForKey:@"_none_"];
  369. for (NSString *fileID in fileIDs)
  370. [metadatas addObject:[sectionDataSource.allRecordsDataSource objectForKey:fileID]];
  371. _dataSource = [NSArray arrayWithArray:metadatas];
  372. [self.tableView reloadData];
  373. }
  374. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  375. {
  376. return 60;
  377. }
  378. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  379. {
  380. return 1;
  381. }
  382. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  383. {
  384. return [_dataSource count];
  385. }
  386. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  387. {
  388. CCFavoritesCell *cell = (CCFavoritesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  389. tableMetadata *metadata;
  390. // variable base
  391. cell.delegate = self;
  392. cell.indexPath = indexPath;
  393. // separator
  394. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  395. // Initialize
  396. cell.status.image = nil;
  397. cell.favorite.image = nil;
  398. cell.local.image = nil;
  399. cell.shared.image = nil;
  400. // change color selection
  401. UIView *selectionColor = [[UIView alloc] init];
  402. selectionColor.backgroundColor = [[NCBrandColor sharedInstance] getColorSelectBackgrond];
  403. cell.selectedBackgroundView = selectionColor;
  404. metadata = [_dataSource objectAtIndex:indexPath.row];
  405. // favorite
  406. if (_serverUrl == nil)
  407. cell.favorite.image = [UIImage imageNamed:@"favorite"];
  408. cell.labelTitle.textColor = [UIColor blackColor];
  409. // filename
  410. cell.labelTitle.text = metadata.fileNameView;
  411. cell.labelInfoFile.text = @"";
  412. // Shared
  413. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  414. if (!serverUrl)
  415. return cell;
  416. NSString *shareLink = [appDelegate.sharesLink objectForKey:[serverUrl stringByAppendingString:metadata.fileName]];
  417. NSString *shareUserAndGroup = [appDelegate.sharesUserAndGroup objectForKey:[serverUrl stringByAppendingString:metadata.fileName]];
  418. // Immage
  419. if (metadata.directory) {
  420. if ([shareLink length] > 0) {
  421. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder_public"] color:[NCBrandColor sharedInstance].brand];
  422. } else if ([shareUserAndGroup length] > 0) {
  423. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder_shared_with_me"] color:[NCBrandColor sharedInstance].brand];
  424. } else {
  425. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] color:[NCBrandColor sharedInstance].brand];
  426. }
  427. } else {
  428. if ([shareLink length] > 0 || [shareUserAndGroup length] > 0) {
  429. if ([shareLink length] > 0)
  430. cell.shared.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"shareLink"] color:[NCBrandColor sharedInstance].brand];
  431. else
  432. cell.shared.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"actionSheetShare"] color:[NCBrandColor sharedInstance].brand];
  433. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionConnectionMounted:)];
  434. [tap setNumberOfTapsRequired:1];
  435. cell.shared.userInteractionEnabled = YES;
  436. [cell.shared addGestureRecognizer:tap];
  437. }
  438. cell.file.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, metadata.fileID]];
  439. if (cell.file.image == nil) {
  440. cell.file.image = [UIImage imageNamed:metadata.iconName];
  441. if (metadata.thumbnailExists)
  442. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  443. }
  444. // ----------------------------------------------------------------------------------------------------------
  445. // E2E Image Status Encrypted
  446. // ----------------------------------------------------------------------------------------------------------
  447. tableE2eEncryption *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND fileNameIdentifier = %@", appDelegate.activeAccount, metadata.fileName]];
  448. if (tableE2eEncryption)
  449. cell.status.image = [UIImage imageNamed:@"encrypted"];
  450. }
  451. // text and length
  452. if (metadata.directory) {
  453. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  454. //cell.accessoryType = UITableViewCellAccessoryNone;
  455. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  456. } else {
  457. NSString *date = [CCUtility dateDiff:metadata.date];
  458. NSString *length = [CCUtility transformedSize:metadata.size];
  459. BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, metadata.fileID]];
  460. if (fileExists)
  461. cell.local.image = [UIImage imageNamed:@"local"];
  462. else
  463. cell.local.image = nil;
  464. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ %@", date, length];
  465. cell.accessoryType = UITableViewCellAccessoryNone;
  466. }
  467. // ======== MGSwipe ========
  468. //configure left buttons : ONLY Root Favorites : Remove file/folder Favorites
  469. if (_serverUrl == nil) {
  470. 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]]];
  471. cell.leftExpansion.buttonIndex = 0;
  472. cell.leftExpansion.fillOnTrigger = NO;
  473. //centerIconOverText
  474. MGSwipeButton *favoriteButton = (MGSwipeButton *)[cell.leftButtons objectAtIndex:0];
  475. [favoriteButton centerIconOverText];
  476. }
  477. //configure right buttons
  478. 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]]];
  479. cell.rightSwipeSettings.transition = MGSwipeTransitionBorder;
  480. //centerIconOverText
  481. MGSwipeButton *deleteButton = (MGSwipeButton *)[cell.rightButtons objectAtIndex:0];
  482. MGSwipeButton *moreButton = (MGSwipeButton *)[cell.rightButtons objectAtIndex:1];
  483. [deleteButton centerIconOverText];
  484. [moreButton centerIconOverText];
  485. return cell;
  486. }
  487. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  488. {
  489. // deselect row
  490. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  491. _metadata = [self setSelfMetadataFromIndexPath:indexPath];
  492. // if is in download [do not touch]
  493. if ([_metadata.session length] > 0 && [_metadata.session containsString:@"download"])
  494. return;
  495. // File
  496. if (_metadata.directory == NO) {
  497. // File do not exists
  498. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  499. if (serverUrl) {
  500. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, _metadata.fileID]]) {
  501. [self downloadFileSuccessFailure:_metadata.fileName fileID:_metadata.fileID serverUrl:serverUrl selector:selectorLoadFileView selectorPost:@"" errorMessage:@"" errorCode:0];
  502. } else {
  503. [[CCNetworking sharedNetworking] downloadFile:_metadata.fileName fileID:_metadata.fileID serverUrl:serverUrl selector:selectorLoadFileView selectorPost:nil session:k_download_session taskStatus:k_taskStatusResume delegate:self];
  504. }
  505. }
  506. }
  507. // Directory
  508. if (_metadata.directory)
  509. [self performSegueDirectoryWithControlPasscode];
  510. }
  511. -(void)performSegueDirectoryWithControlPasscode
  512. {
  513. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  514. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  515. if (serverUrl) {
  516. vc.serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:_metadata.fileName];
  517. vc.titleViewControl = _metadata.fileNameView;
  518. [self.navigationController pushViewController:vc animated:YES];
  519. }
  520. }
  521. #pragma --------------------------------------------------------------------------------------------
  522. #pragma mark ===== Navigation ====
  523. #pragma --------------------------------------------------------------------------------------------
  524. - (BOOL)shouldPerformSegue
  525. {
  526. // if i am in background -> exit
  527. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  528. // if i am not window -> exit
  529. if (self.view.window == NO)
  530. return NO;
  531. // Collapsed but i am in detail -> exit
  532. if (self.splitViewController.isCollapsed)
  533. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  534. return YES;
  535. }
  536. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  537. {
  538. id viewController = segue.destinationViewController;
  539. if ([viewController isKindOfClass:[UINavigationController class]]) {
  540. UINavigationController *nav = viewController;
  541. _detailViewController = (CCDetail *)nav.topViewController;
  542. } else {
  543. _detailViewController = segue.destinationViewController;
  544. }
  545. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  546. for (tableMetadata *metadata in _dataSource) {
  547. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  548. [allRecordsDataSourceImagesVideos addObject:metadata];
  549. }
  550. _detailViewController.metadataDetail = _metadata;
  551. _detailViewController.dateFilterQuery = nil;
  552. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  553. [_detailViewController setTitle:_metadata.fileNameView];
  554. }
  555. @end