CCFavorites.m 30 KB

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