CCFavorites.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. //
  2. // CCFavorites.m
  3. // Crypto Cloud Technology Nextcloud
  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 encrypted:NO 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. if (progress == 0)
  100. [self.navigationController cancelCCProgress];
  101. else
  102. [self.navigationController setCCProgressPercentage:progress*100 andTintColor:[NCBrandColor sharedInstance].navigationBarProgress];
  103. }
  104. #pragma --------------------------------------------------------------------------------------------
  105. #pragma mark ==== DZNEmptyDataSetSource ====
  106. #pragma --------------------------------------------------------------------------------------------
  107. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  108. {
  109. return [UIColor whiteColor];
  110. }
  111. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  112. {
  113. return [UIImage imageNamed:@"favoriteNoFiles"];
  114. }
  115. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  116. {
  117. NSString *text = [NSString stringWithFormat:@"%@", NSLocalizedString(@"_favorite_no_files_", nil)];
  118. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  119. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  120. }
  121. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  122. {
  123. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_favorite_view_", nil)];
  124. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  125. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  126. paragraph.alignment = NSTextAlignmentCenter;
  127. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  128. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  129. }
  130. #pragma --------------------------------------------------------------------------------------------
  131. #pragma mark ===== Delete <delegate> =====
  132. #pragma--------------------------------------------------------------------------------------------
  133. - (void)deleteFileOrFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  134. {
  135. NSLog(@"[LOG] Delete error %@", message);
  136. }
  137. - (void)deleteFileOrFolderSuccess:(CCMetadataNet *)metadataNet
  138. {
  139. [self reloadDatasource];
  140. }
  141. #pragma --------------------------------------------------------------------------------------------
  142. #pragma mark ===== Favorite <delegate> =====
  143. #pragma--------------------------------------------------------------------------------------------
  144. - (void)settingFavoriteFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  145. {
  146. NSLog(@"[LOG] Remove Favorite error %@", message);
  147. }
  148. - (void)settingFavoriteSuccess:(CCMetadataNet *)metadataNet
  149. {
  150. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadataNet.fileID favorite:[metadataNet.options boolValue]];
  151. [self reloadDatasource];
  152. }
  153. - (void)readListingFavorites
  154. {
  155. // test
  156. if (app.activeAccount.length == 0)
  157. return;
  158. [[CCActions sharedInstance] listingFavorites:@"" delegate:self];
  159. }
  160. - (void)addFavoriteFolder:(NSString *)serverUrl
  161. {
  162. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  163. if (!directoryID) return;
  164. NSString *selector;
  165. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  166. metadataNet.action = actionReadFolder;
  167. metadataNet.depth = @"1";
  168. metadataNet.directoryID = directoryID;
  169. if ([CCUtility getFavoriteOffline])
  170. selector = selectorReadFolderWithDownload;
  171. else
  172. selector = selectorReadFolder;
  173. metadataNet.selector = selector;
  174. metadataNet.serverUrl = serverUrl;
  175. [app addNetworkingOperationQueue:app.netQueue delegate:[CCSynchronize sharedSynchronize] metadataNet:metadataNet];
  176. }
  177. - (void)listingFavoritesSuccess:(CCMetadataNet *)metadataNet metadatas:(NSArray *)metadatas
  178. {
  179. // verify active user
  180. tableAccount *record = [[NCManageDatabase sharedInstance] getAccountActive];
  181. if (![record.account isEqualToString:metadataNet.account])
  182. return;
  183. NSString *father = @"";
  184. NSMutableArray *filesEtag = [NSMutableArray new];
  185. for (tableMetadata *metadata in metadatas) {
  186. // type of file
  187. NSInteger typeFilename = [CCUtility getTypeFileName:metadata.fileName];
  188. // do not insert cryptated favorite file
  189. if (typeFilename == k_metadataTypeFilenameCrypto || typeFilename == k_metadataTypeFilenamePlist)
  190. continue;
  191. // insert for test NOT favorite
  192. [filesEtag addObject:metadata.fileID];
  193. // Get ServerUrl
  194. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  195. serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileNameData];
  196. if (![serverUrl containsString:father]) {
  197. if (metadata.directory) {
  198. NSString *selector;
  199. if ([CCUtility getFavoriteOffline])
  200. selector = selectorReadFolderWithDownload;
  201. else
  202. selector = selectorReadFolder;
  203. [[CCSynchronize sharedSynchronize] synchronizedFolder:serverUrl selector:selector];
  204. } else {
  205. if ([CCUtility getFavoriteOffline])
  206. [[CCSynchronize sharedSynchronize] synchronizedFile:metadata selector:selectorReadFileWithDownload];
  207. else
  208. [[CCSynchronize sharedSynchronize] synchronizedFile:metadata selector:selectorReadFile];
  209. }
  210. father = serverUrl;
  211. }
  212. }
  213. // Verify remove favorite
  214. NSArray *allRecordFavorite = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND favorite = true", app.activeAccount] sorted:nil ascending:NO];
  215. for (tableMetadata *metadata in allRecordFavorite)
  216. if (![filesEtag containsObject:metadata.fileID])
  217. [[NCManageDatabase sharedInstance] setMetadataFavoriteWithFileID:metadata.fileID favorite:NO];
  218. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"clearDateReadDataSource" object:nil];
  219. }
  220. - (void)listingFavoritesFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  221. {
  222. NSLog(@"Read Favorites Failure");
  223. }
  224. #pragma --------------------------------------------------------------------------------------------
  225. #pragma mark ==== Download Thumbnail <Delegate> ====
  226. #pragma --------------------------------------------------------------------------------------------
  227. - (void)downloadThumbnailSuccess:(CCMetadataNet *)metadataNet
  228. {
  229. [self reloadDatasource];
  230. }
  231. #pragma --------------------------------------------------------------------------------------------
  232. #pragma mark ==== Download <Delegate> ====
  233. #pragma --------------------------------------------------------------------------------------------
  234. - (void)downloadFileFailure:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  235. {
  236. if (errorCode != k_CCErrorFileAlreadyInDownload)
  237. [app messageNotification:@"_download_file_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  238. }
  239. - (void)downloadFileSuccess:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  240. {
  241. _metadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", fileID]];
  242. if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_compress]) {
  243. //[self performSelector:@selector(unZipFile:) withObject:_metadata.fileID];
  244. [self openWith:_metadata];
  245. } else if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_unknown]) {
  246. [self openWith:_metadata];
  247. } else {
  248. if ([self shouldPerformSegue])
  249. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  250. }
  251. }
  252. #pragma --------------------------------------------------------------------------------------------
  253. #pragma mark ===== menu =====
  254. #pragma--------------------------------------------------------------------------------------------
  255. - (void)openModel:(tableMetadata *)metadata
  256. {
  257. UIViewController *viewController;
  258. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  259. if (serverUrl) {
  260. if ([metadata.model isEqualToString:@"cartadicredito"])
  261. viewController = [[CCCartaDiCredito alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  262. if ([metadata.model isEqualToString:@"bancomat"])
  263. viewController = [[CCBancomat alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  264. if ([metadata.model isEqualToString:@"contocorrente"])
  265. viewController = [[CCContoCorrente alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  266. if ([metadata.model isEqualToString:@"accountweb"])
  267. viewController = [[CCAccountWeb alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  268. if ([metadata.model isEqualToString:@"patenteguida"])
  269. viewController = [[CCPatenteGuida alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  270. if ([metadata.model isEqualToString:@"cartaidentita"])
  271. viewController = [[CCCartaIdentita alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  272. if ([metadata.model isEqualToString:@"passaporto"])
  273. viewController = [[CCPassaporto alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  274. if ([metadata.model isEqualToString:@"note"]) {
  275. viewController = [[CCNote alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  276. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  277. [self presentViewController:navigationController animated:YES completion:nil];
  278. } else {
  279. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  280. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  281. [self presentViewController:navigationController animated:YES completion:nil];
  282. }
  283. }
  284. }
  285. - (void)openWith:(tableMetadata *)metadata
  286. {
  287. NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID];
  288. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePath]) {
  289. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint] error:nil];
  290. [[NSFileManager defaultManager] linkItemAtPath:fileNamePath toPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint] error:nil];
  291. NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint]];
  292. _docController = [UIDocumentInteractionController interactionControllerWithURL:url];
  293. _docController.delegate = self;
  294. [_docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  295. }
  296. }
  297. - (void)requestDeleteMetadata:(tableMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  298. {
  299. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  300. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  301. [[CCActions sharedInstance] deleteFileOrFolder:metadata delegate:self];
  302. [self reloadDatasource];
  303. }]];
  304. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  305. }]];
  306. alertController.popoverPresentationController.sourceView = self.view;
  307. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  308. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  309. [alertController.view layoutIfNeeded];
  310. [self presentViewController:alertController animated:YES completion:nil];
  311. }
  312. - (void)requestMoreMetadata:(tableMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  313. {
  314. UIImage *iconHeader;
  315. metadata = [_dataSource objectAtIndex:indexPath.row];
  316. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.tabBarController.view title:nil];
  317. actionSheet.animationDuration = 0.2;
  318. actionSheet.buttonHeight = 50.0;
  319. actionSheet.cancelButtonHeight = 50.0f;
  320. actionSheet.separatorHeight = 5.0f;
  321. actionSheet.automaticallyTintButtonImages = @(NO);
  322. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].cryptocloud };
  323. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  324. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:17], NSForegroundColorAttributeName:[UIColor blackColor] };
  325. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor darkGrayColor] };
  326. actionSheet.separatorColor = [NCBrandColor sharedInstance].seperator;
  327. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  328. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  329. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]]) {
  330. iconHeader = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  331. } else {
  332. if (metadata.directory)
  333. iconHeader = [CCGraphics changeThemingColorImage:[UIImage imageNamed:metadata.iconName] color:[NCBrandColor sharedInstance].brand];
  334. else
  335. iconHeader = [UIImage imageNamed:metadata.iconName];
  336. }
  337. [actionSheet addButtonWithTitle: metadata.fileNamePrint image: iconHeader backgroundColor: [NCBrandColor sharedInstance].tabBar height: 50.0 type: AHKActionSheetButtonTypeDisabled handler: nil
  338. ];
  339. // Share
  340. if (_metadata.cryptated == NO) {
  341. [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) {
  342. [app.activeMain openWindowShare:metadata];
  343. }];
  344. }
  345. // NO Directory - NO Template
  346. if (metadata.directory == NO && [metadata.type isEqualToString:k_metadataType_template] == NO) {
  347. [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) {
  348. [self.tableView setEditing:NO animated:YES];
  349. [self openWith:metadata];
  350. }];
  351. }
  352. [actionSheet show];
  353. }
  354. - (void)tapActionConnectionMounted:(UITapGestureRecognizer *)tapGesture
  355. {
  356. CGPoint location = [tapGesture locationInView:self.tableView];
  357. NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  358. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  359. if (metadata)
  360. [app.activeMain openWindowShare:metadata];
  361. }
  362. #pragma mark -
  363. #pragma --------------------------------------------------------------------------------------------
  364. #pragma mark ===== Swipe Tablet -> menu =====
  365. #pragma --------------------------------------------------------------------------------------------
  366. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell canSwipe:(MGSwipeDirection)direction
  367. {
  368. return YES;
  369. }
  370. - (BOOL)swipeTableCell:(MGSwipeTableCell *)cell tappedButtonAtIndex:(NSInteger)index direction:(MGSwipeDirection)direction fromExpansion:(BOOL)fromExpansion
  371. {
  372. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  373. if (direction == MGSwipeDirectionRightToLeft) {
  374. // Delete
  375. if (index == 0)
  376. [self requestDeleteMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  377. // More
  378. if (index == 1)
  379. [self requestMoreMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  380. }
  381. if (direction == MGSwipeDirectionLeftToRight) {
  382. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  383. [[CCActions sharedInstance] settingFavorite:metadata favorite:NO delegate:self];
  384. }
  385. return YES;
  386. }
  387. #pragma --------------------------------------------------------------------------------------------
  388. #pragma mark ==== Table ====
  389. #pragma --------------------------------------------------------------------------------------------
  390. - (tableMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  391. {
  392. tableMetadata *metadata = [_dataSource objectAtIndex:indexPath.row];
  393. return metadata;
  394. }
  395. - (void)readFolder:(NSString *)serverUrl
  396. {
  397. [self reloadDatasource];
  398. }
  399. - (void)reloadDatasource
  400. {
  401. NSMutableArray *metadatas = [NSMutableArray new];
  402. NSArray *recordsTableMetadata ;
  403. NSString *sorted = [CCUtility getOrderSettings];
  404. if ([sorted isEqualToString:@"fileName"])
  405. sorted = @"fileNamePrint";
  406. if (!_serverUrl) {
  407. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND favorite = true", app.activeAccount] sorted:sorted ascending:[CCUtility getAscendingSettings]];
  408. } else {
  409. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_serverUrl];
  410. if (directoryID)
  411. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@", app.activeAccount, directoryID] sorted:sorted ascending:[CCUtility getAscendingSettings]];
  412. }
  413. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil activeAccount:app.activeAccount];
  414. NSArray *fileIDs = [sectionDataSource.sectionArrayRow objectForKey:@"_none_"];
  415. for (NSString *fileID in fileIDs)
  416. [metadatas addObject:[sectionDataSource.allRecordsDataSource objectForKey:fileID]];
  417. _dataSource = [NSArray arrayWithArray:metadatas];
  418. [self.tableView reloadData];
  419. }
  420. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  421. {
  422. return 60;
  423. }
  424. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  425. {
  426. return 1;
  427. }
  428. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  429. {
  430. return [_dataSource count];
  431. }
  432. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  433. {
  434. CCFavoritesCell *cell = (CCFavoritesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  435. tableMetadata *metadata;
  436. // variable base
  437. cell.delegate = self;
  438. cell.indexPath = indexPath;
  439. // separator
  440. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  441. // Initialize
  442. cell.status.image = nil;
  443. cell.favorite.image = nil;
  444. cell.local.image = nil;
  445. cell.shared.image = nil;
  446. // change color selection
  447. UIView *selectionColor = [[UIView alloc] init];
  448. selectionColor.backgroundColor = [[NCBrandColor sharedInstance] getColorSelectBackgrond];
  449. cell.selectedBackgroundView = selectionColor;
  450. metadata = [_dataSource objectAtIndex:indexPath.row];
  451. // favorite
  452. if (_serverUrl == nil)
  453. cell.favorite.image = [UIImage imageNamed:@"favorite"];
  454. // encrypted color
  455. if (metadata.cryptated) {
  456. cell.labelTitle.textColor = [NCBrandColor sharedInstance].cryptocloud;
  457. } else {
  458. cell.labelTitle.textColor = [UIColor blackColor];
  459. }
  460. // filename
  461. cell.labelTitle.text = metadata.fileNamePrint;
  462. cell.labelInfoFile.text = @"";
  463. // Shared
  464. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  465. if (!serverUrl)
  466. return cell;
  467. NSString *shareLink = [app.sharesLink objectForKey:[serverUrl stringByAppendingString:metadata.fileName]];
  468. NSString *shareUserAndGroup = [app.sharesUserAndGroup objectForKey:[serverUrl stringByAppendingString:metadata.fileName]];
  469. // Immage
  470. if (metadata.directory) {
  471. if ([shareLink length] > 0) {
  472. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder_public"] color:[NCBrandColor sharedInstance].brand];
  473. } else if ([shareUserAndGroup length] > 0) {
  474. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder_shared_with_me"] color:[NCBrandColor sharedInstance].brand];
  475. } else {
  476. cell.file.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:metadata.iconName] color:[NCBrandColor sharedInstance].brand];
  477. }
  478. } else {
  479. if ([shareLink length] > 0 || [shareUserAndGroup length] > 0) {
  480. if ([shareLink length] > 0)
  481. cell.shared.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"shareLink"] color:[NCBrandColor sharedInstance].brand];
  482. else
  483. cell.shared.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"actionSheetShare"] color:[NCBrandColor sharedInstance].brand];
  484. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionConnectionMounted:)];
  485. [tap setNumberOfTapsRequired:1];
  486. cell.shared.userInteractionEnabled = YES;
  487. [cell.shared addGestureRecognizer:tap];
  488. }
  489. cell.file.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  490. if (cell.file.image == nil) {
  491. cell.file.image = [UIImage imageNamed:metadata.iconName];
  492. if (metadata.thumbnailExists)
  493. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  494. }
  495. }
  496. // it's encrypted ???
  497. if (metadata.cryptated && [metadata.type isEqualToString: k_metadataType_template] == NO)
  498. cell.status.image = [UIImage imageNamed:@"lock"];
  499. // text and length
  500. if (metadata.directory) {
  501. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  502. //cell.accessoryType = UITableViewCellAccessoryNone;
  503. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  504. } else {
  505. NSString *date = [CCUtility dateDiff:metadata.date];
  506. NSString *length = [CCUtility transformedSize:metadata.size];
  507. if ([metadata.type isEqualToString: k_metadataType_template])
  508. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", date];
  509. if ([metadata.type isEqualToString: k_metadataType_file]) {
  510. BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID]];
  511. if (fileExists)
  512. cell.local.image = [UIImage imageNamed:@"local"];
  513. else
  514. cell.local.image = nil;
  515. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ %@", date, length];
  516. }
  517. cell.accessoryType = UITableViewCellAccessoryNone;
  518. }
  519. // ======== MGSwipe ========
  520. //configure left buttons : ONLY Root Favorites : Remove file/folder Favorites
  521. if (_serverUrl == nil) {
  522. 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]]];
  523. cell.leftExpansion.buttonIndex = 0;
  524. cell.leftExpansion.fillOnTrigger = NO;
  525. //centerIconOverText
  526. MGSwipeButton *favoriteButton = (MGSwipeButton *)[cell.leftButtons objectAtIndex:0];
  527. [favoriteButton centerIconOverText];
  528. }
  529. //configure right buttons
  530. 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]]];
  531. cell.rightSwipeSettings.transition = MGSwipeTransitionBorder;
  532. //centerIconOverText
  533. MGSwipeButton *deleteButton = (MGSwipeButton *)[cell.rightButtons objectAtIndex:0];
  534. MGSwipeButton *moreButton = (MGSwipeButton *)[cell.rightButtons objectAtIndex:1];
  535. [deleteButton centerIconOverText];
  536. [moreButton centerIconOverText];
  537. return cell;
  538. }
  539. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  540. {
  541. // deselect row
  542. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  543. _metadata = [self setSelfMetadataFromIndexPath:indexPath];
  544. // if is in download [do not touch]
  545. if ([_metadata.session length] > 0 && [_metadata.session containsString:@"download"])
  546. return;
  547. // File
  548. if (([_metadata.type isEqualToString: k_metadataType_file]) && _metadata.directory == NO) {
  549. // File do not exists
  550. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  551. if (serverUrl) {
  552. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, _metadata.fileID]]) {
  553. [self downloadFileSuccess:_metadata.fileID serverUrl:serverUrl selector:selectorLoadFileView selectorPost:nil];
  554. } else {
  555. [[CCNetworking sharedNetworking] downloadFile:_metadata.fileID serverUrl:serverUrl downloadData:YES downloadPlist:NO selector:selectorLoadFileView selectorPost:nil session:k_download_session taskStatus:k_taskStatusResume delegate:self];
  556. }
  557. }
  558. }
  559. // Model
  560. if ([self.metadata.type isEqualToString: k_metadataType_template])
  561. [self openModel:self.metadata];
  562. // Directory
  563. if (_metadata.directory)
  564. [self performSegueDirectoryWithControlPasscode];
  565. }
  566. -(void)performSegueDirectoryWithControlPasscode
  567. {
  568. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  569. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadata.directoryID];
  570. if (serverUrl) {
  571. vc.serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:_metadata.fileNameData];
  572. vc.titleViewControl = _metadata.fileNamePrint;
  573. [self.navigationController pushViewController:vc animated:YES];
  574. }
  575. }
  576. #pragma --------------------------------------------------------------------------------------------
  577. #pragma mark ===== Navigation ====
  578. #pragma --------------------------------------------------------------------------------------------
  579. - (BOOL)shouldPerformSegue
  580. {
  581. // if i am in background -> exit
  582. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  583. // if i am not window -> exit
  584. if (self.view.window == NO)
  585. return NO;
  586. // Collapsed but i am in detail -> exit
  587. if (self.splitViewController.isCollapsed)
  588. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  589. // Video in run -> exit
  590. if (self.detailViewController.photoBrowser.currentVideoPlayerViewController.isViewLoaded && self.detailViewController.photoBrowser.currentVideoPlayerViewController.view.window) return NO;
  591. return YES;
  592. }
  593. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  594. {
  595. id viewController = segue.destinationViewController;
  596. if ([viewController isKindOfClass:[UINavigationController class]]) {
  597. UINavigationController *nav = viewController;
  598. _detailViewController = (CCDetail *)nav.topViewController;
  599. } else {
  600. _detailViewController = segue.destinationViewController;
  601. }
  602. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  603. for (tableMetadata *metadata in _dataSource) {
  604. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  605. [allRecordsDataSourceImagesVideos addObject:metadata];
  606. }
  607. _detailViewController.metadataDetail = _metadata;
  608. _detailViewController.dateFilterQuery = nil;
  609. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  610. [_detailViewController setTitle:_metadata.fileNamePrint];
  611. }
  612. @end