CCFavorites.m 29 KB

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