CCFavorites.m 31 KB

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