CCFavorites.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. // Automatic Upload Folder
  33. NSString *_autoUploadFileName;
  34. NSString *_autoUploadDirectory;
  35. }
  36. @end
  37. @implementation CCFavorites
  38. #pragma --------------------------------------------------------------------------------------------
  39. #pragma mark ===== Init =====
  40. #pragma --------------------------------------------------------------------------------------------
  41. - (id)initWithCoder:(NSCoder *)aDecoder
  42. {
  43. if (self = [super initWithCoder:aDecoder]) {
  44. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  45. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  46. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  47. appDelegate.activeFavorites = self;
  48. }
  49. return self;
  50. }
  51. - (void)viewDidLoad
  52. {
  53. [super viewDidLoad];
  54. // Custom Cell
  55. [self.tableView registerNib:[UINib nibWithNibName:@"CCFavoritesCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
  56. // dataSource
  57. _dataSource = [NSMutableArray new];
  58. // Metadata
  59. _metadata = [tableMetadata new];
  60. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  61. self.tableView.separatorColor = [NCBrandColor sharedInstance].seperator;
  62. self.tableView.emptyDataSetDelegate = self;
  63. self.tableView.emptyDataSetSource = self;
  64. self.tableView.delegate = self;
  65. // calculate _serverUrl
  66. if (!_serverUrl)
  67. _serverUrl = nil;
  68. // Title
  69. if (_titleViewControl)
  70. self.title = _titleViewControl;
  71. else
  72. self.title = NSLocalizedString(@"_favorites_", nil);
  73. }
  74. // Apparirà
  75. - (void)viewWillAppear:(BOOL)animated
  76. {
  77. [super viewWillAppear:animated];
  78. // Color
  79. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  80. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  81. // Plus Button
  82. [appDelegate plusButtonVisibile:true];
  83. [self reloadDatasource];
  84. }
  85. // E' arrivato
  86. - (void)viewDidAppear:(BOOL)animated
  87. {
  88. [super viewDidAppear:animated];
  89. // Active Main
  90. appDelegate.activeFavorites = self;
  91. }
  92. - (void)changeTheming
  93. {
  94. if (self.isViewLoaded && self.view.window)
  95. [appDelegate changeTheming:self];
  96. // Reload Table View
  97. [self.tableView reloadData];
  98. }
  99. - (void)triggerProgressTask:(NSNotification *)notification
  100. {
  101. //NSDictionary *dict = notification.userInfo;
  102. //float progress = [[dict valueForKey:@"progress"] floatValue];
  103. }
  104. #pragma --------------------------------------------------------------------------------------------
  105. #pragma mark ==== DZNEmptyDataSetSource ====
  106. #pragma --------------------------------------------------------------------------------------------
  107. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  108. {
  109. return [NCBrandColor sharedInstance].backgroundView;
  110. }
  111. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  112. {
  113. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favoriteNoFiles"] color:[NCBrandColor sharedInstance].graySoft];
  114. }
  115. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  116. {
  117. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_favorite_no_files_", nil)];
  118. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  119. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  120. }
  121. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  122. {
  123. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_favorite_view_", nil)];
  124. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  125. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  126. paragraph.alignment = NSTextAlignmentCenter;
  127. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  128. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  129. }
  130. #pragma --------------------------------------------------------------------------------------------
  131. #pragma mark ===== Delete <delegate> =====
  132. #pragma--------------------------------------------------------------------------------------------
  133. - (void)deleteFileOrFolderSuccessFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  134. {
  135. if (errorCode == 0)
  136. [self reloadDatasource];
  137. else
  138. NSLog(@"[LOG] DeleteFileOrFolder failure error %d, %@", (int)errorCode, message);
  139. }
  140. #pragma --------------------------------------------------------------------------------------------
  141. #pragma mark ===== Favorite <delegate> =====
  142. #pragma--------------------------------------------------------------------------------------------
  143. - (void)settingFavoriteSuccessFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  144. {
  145. if (errorCode == 0) {
  146. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadataNet.fileID favorite:[metadataNet.options boolValue]];
  147. [self reloadDatasource];
  148. } else {
  149. NSLog(@"[LOG] Setting Favorite failure error %d, %@", (int)errorCode, message);
  150. }
  151. }
  152. - (void)addFavoriteFolder:(NSString *)serverUrl
  153. {
  154. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  155. if (!directoryID) return;
  156. NSString *selector;
  157. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  158. metadataNet.action = actionReadFolder;
  159. metadataNet.depth = @"1";
  160. metadataNet.directoryID = directoryID;
  161. if ([CCUtility getFavoriteOffline])
  162. selector = selectorReadFolderWithDownload;
  163. else
  164. selector = selectorReadFolder;
  165. metadataNet.selector = selector;
  166. metadataNet.serverUrl = serverUrl;
  167. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:[CCSynchronize sharedSynchronize] metadataNet:metadataNet];
  168. }
  169. - (void)listingFavoritesSuccessFailure:(CCMetadataNet *)metadataNet metadatas:(NSArray *)metadatas message:(NSString *)message errorCode:(NSInteger)errorCode
  170. {
  171. // Check Active Account
  172. if (![metadataNet.account isEqualToString:appDelegate.activeAccount])
  173. return;
  174. if (errorCode == 0) {
  175. NSString *father = @"";
  176. NSMutableArray *filesEtag = [NSMutableArray new];
  177. for (tableMetadata *metadata in metadatas) {
  178. // insert for test NOT favorite
  179. [filesEtag addObject:metadata.fileID];
  180. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  181. NSString *serverUrlSon = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileName];
  182. if (![serverUrlSon containsString:father]) {
  183. if (metadata.directory) {
  184. if ([CCUtility getFavoriteOffline])
  185. [[CCSynchronize sharedSynchronize] readFileForFolder:metadata.fileName serverUrl:serverUrl selector:selectorReadFileFolderWithDownload];
  186. else
  187. [[CCSynchronize sharedSynchronize] readFileForFolder:metadata.fileName serverUrl:serverUrl selector:selectorReadFileFolder];
  188. } else {
  189. if ([CCUtility getFavoriteOffline])
  190. [[CCSynchronize sharedSynchronize] readFile:metadata selector:selectorReadFileWithDownload];
  191. else
  192. [[CCSynchronize sharedSynchronize] readFile:metadata selector:selectorReadFile];
  193. }
  194. father = serverUrlSon;
  195. }
  196. }
  197. // Verify remove favorite
  198. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND favorite = true", appDelegate.activeAccount] sorted:nil ascending:NO];
  199. for (tableMetadata *metadata in allRecordFavorite)
  200. if (![filesEtag containsObject:metadata.fileID])
  201. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadata.fileID favorite:NO];
  202. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"clearDateReadDataSource" object:nil];
  203. } else {
  204. NSLog(@"[LOG] Listing Favorites failure error %d, %@", (int)errorCode, message);
  205. }
  206. }
  207. - (void)listingFavorites
  208. {
  209. // test
  210. if (appDelegate.activeAccount.length == 0)
  211. return;
  212. [[CCActions sharedInstance] listingFavorites:@"" delegate:self];
  213. }
  214. #pragma --------------------------------------------------------------------------------------------
  215. #pragma mark ==== Download Thumbnail <Delegate> ====
  216. #pragma --------------------------------------------------------------------------------------------
  217. - (void)downloadThumbnailSuccessFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  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:@"share"] 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 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. // get auto upload folder
  377. _autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  378. _autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:appDelegate.activeUrl];
  379. [self.tableView reloadData];
  380. }
  381. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  382. {
  383. return 60;
  384. }
  385. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  386. {
  387. return 1;
  388. }
  389. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  390. {
  391. return [_dataSource count];
  392. }
  393. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  394. {
  395. CCFavoritesCell *cell = (CCFavoritesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  396. tableMetadata *metadata;
  397. // variable base
  398. cell.delegate = self;
  399. cell.indexPath = indexPath;
  400. // separator
  401. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  402. // Initialize
  403. cell.status.image = nil;
  404. cell.favorite.image = nil;
  405. cell.local.image = nil;
  406. cell.shared.image = nil;
  407. // change color selection
  408. UIView *selectionColor = [[UIView alloc] init];
  409. selectionColor.backgroundColor = [[NCBrandColor sharedInstance] getColorSelectBackgrond];
  410. cell.selectedBackgroundView = selectionColor;
  411. metadata = [_dataSource objectAtIndex:indexPath.row];
  412. // favorite
  413. if (_serverUrl == nil)
  414. cell.favorite.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] color:[NCBrandColor sharedInstance].yellowFavorite];
  415. cell.labelTitle.textColor = [UIColor blackColor];
  416. // filename
  417. cell.labelTitle.text = metadata.fileNameView;
  418. cell.labelInfoFile.text = @"";
  419. // Shared
  420. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  421. if (!serverUrl)
  422. return cell;
  423. NSString *shareLink = [appDelegate.sharesLink objectForKey:[serverUrl stringByAppendingString:metadata.fileName]];
  424. NSString *shareUserAndGroup = [appDelegate.sharesUserAndGroup objectForKey:[serverUrl stringByAppendingString:metadata.fileName]];
  425. // Immage
  426. if (metadata.directory) {
  427. if (metadata.e2eEncrypted) {
  428. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderEncrypted"] color:[NCBrandColor sharedInstance].brandElement];
  429. } else if ([metadata.fileName isEqualToString:_autoUploadFileName] && [self.serverUrl isEqualToString:_autoUploadDirectory]) {
  430. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderPhotos"] color:[NCBrandColor sharedInstance].brandElement];
  431. } else if ([shareLink length] > 0) {
  432. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder_public"] color:[NCBrandColor sharedInstance].brandElement];
  433. } else if ([shareUserAndGroup length] > 0) {
  434. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder_shared_with_me"] color:[NCBrandColor sharedInstance].brandElement];
  435. } else {
  436. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] color:[NCBrandColor sharedInstance].brandElement];
  437. }
  438. } else {
  439. if ([shareLink length] > 0 || [shareUserAndGroup length] > 0) {
  440. if ([shareLink length] > 0)
  441. cell.shared.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"sharebylink"] color:[NCBrandColor sharedInstance].gray];
  442. else
  443. cell.shared.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"share"] color:[NCBrandColor sharedInstance].gray];
  444. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionConnectionMounted:)];
  445. [tap setNumberOfTapsRequired:1];
  446. cell.shared.userInteractionEnabled = YES;
  447. [cell.shared addGestureRecognizer:tap];
  448. }
  449. cell.file.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, metadata.fileID]];
  450. if (cell.file.image == nil) {
  451. cell.file.image = [UIImage imageNamed:metadata.iconName];
  452. if (metadata.thumbnailExists)
  453. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  454. }
  455. // ----------------------------------------------------------------------------------------------------------
  456. // E2EE Image Status Encrypted
  457. // ----------------------------------------------------------------------------------------------------------
  458. tableE2eEncryption *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND fileNameIdentifier = %@", appDelegate.activeAccount, metadata.fileName]];
  459. if (tableE2eEncryption)
  460. cell.status.image = [UIImage imageNamed:@"encrypted"];
  461. }
  462. // text and length
  463. if (metadata.directory) {
  464. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  465. //cell.accessoryType = UITableViewCellAccessoryNone;
  466. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  467. } else {
  468. NSString *date = [CCUtility dateDiff:metadata.date];
  469. NSString *length = [CCUtility transformedSize:metadata.size];
  470. BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, metadata.fileID]];
  471. if (fileExists)
  472. cell.local.image = [UIImage imageNamed:@"local"];
  473. else
  474. cell.local.image = nil;
  475. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ %@", date, length];
  476. cell.accessoryType = UITableViewCellAccessoryNone;
  477. }
  478. // ======== MGSwipe ========
  479. //configure left buttons : ONLY Root Favorites : Remove file/folder Favorites
  480. if (_serverUrl == nil) {
  481. cell.leftButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"favorite"] color:[UIColor whiteColor]] backgroundColor:[NCBrandColor sharedInstance].yellowFavorite padding:25]];
  482. cell.leftExpansion.buttonIndex = 0;
  483. cell.leftExpansion.fillOnTrigger = NO;
  484. //centerIconOverText
  485. MGSwipeButton *favoriteButton = (MGSwipeButton *)[cell.leftButtons objectAtIndex:0];
  486. [favoriteButton centerIconOverText];
  487. }
  488. //configure right buttons
  489. cell.rightButtons = @[[MGSwipeButton buttonWithTitle:[NSString stringWithFormat:@" %@ ", NSLocalizedString(@"_delete_", nil)] icon:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"delete"] color:[UIColor whiteColor]] backgroundColor:[UIColor redColor]], [MGSwipeButton buttonWithTitle:[NSString stringWithFormat:@" %@ ", NSLocalizedString(@"_more_", nil)] icon:[UIImage imageNamed:@"swipeMore"] backgroundColor:[UIColor lightGrayColor]]];
  490. cell.rightSwipeSettings.transition = MGSwipeTransitionBorder;
  491. //centerIconOverText
  492. MGSwipeButton *deleteButton = (MGSwipeButton *)[cell.rightButtons objectAtIndex:0];
  493. MGSwipeButton *moreButton = (MGSwipeButton *)[cell.rightButtons objectAtIndex:1];
  494. [deleteButton centerIconOverText];
  495. [moreButton centerIconOverText];
  496. return cell;
  497. }
  498. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  499. {
  500. // deselect row
  501. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  502. _metadata = [self setSelfMetadataFromIndexPath:indexPath];
  503. // if is in download [do not touch]
  504. if ([_metadata.session length] > 0 && [_metadata.session containsString:@"download"])
  505. return;
  506. // File
  507. if (_metadata.directory == NO) {
  508. // File do not exists
  509. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  510. if (serverUrl) {
  511. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, _metadata.fileID]]) {
  512. [self downloadFileSuccessFailure:_metadata.fileName fileID:_metadata.fileID serverUrl:serverUrl selector:selectorLoadFileView selectorPost:@"" errorMessage:@"" errorCode:0];
  513. } else {
  514. [[CCNetworking sharedNetworking] downloadFile:_metadata.fileName fileID:_metadata.fileID serverUrl:serverUrl selector:selectorLoadFileView selectorPost:nil session:k_download_session taskStatus:k_taskStatusResume delegate:self];
  515. }
  516. }
  517. }
  518. // Directory
  519. if (_metadata.directory)
  520. [self performSegueDirectoryWithControlPasscode];
  521. }
  522. -(void)performSegueDirectoryWithControlPasscode
  523. {
  524. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  525. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  526. if (serverUrl) {
  527. vc.serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:_metadata.fileName];
  528. vc.titleViewControl = _metadata.fileNameView;
  529. [self.navigationController pushViewController:vc animated:YES];
  530. }
  531. }
  532. #pragma --------------------------------------------------------------------------------------------
  533. #pragma mark ===== Navigation ====
  534. #pragma --------------------------------------------------------------------------------------------
  535. - (BOOL)shouldPerformSegue
  536. {
  537. // if i am in background -> exit
  538. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  539. // if i am not window -> exit
  540. if (self.view.window == NO)
  541. return NO;
  542. // Collapsed but i am in detail -> exit
  543. if (self.splitViewController.isCollapsed)
  544. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  545. return YES;
  546. }
  547. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  548. {
  549. id viewController = segue.destinationViewController;
  550. if ([viewController isKindOfClass:[UINavigationController class]]) {
  551. UINavigationController *nav = viewController;
  552. _detailViewController = (CCDetail *)nav.topViewController;
  553. } else {
  554. _detailViewController = segue.destinationViewController;
  555. }
  556. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  557. for (tableMetadata *metadata in _dataSource) {
  558. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  559. [allRecordsDataSourceImagesVideos addObject:metadata];
  560. }
  561. _detailViewController.metadataDetail = _metadata;
  562. _detailViewController.dateFilterQuery = nil;
  563. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  564. [_detailViewController setTitle:_metadata.fileNameView];
  565. }
  566. @end