CCFavorites.m 33 KB

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