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