CCFavorites.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. #ifdef CUSTOM_BUILD
  26. #import "CustomSwift.h"
  27. #else
  28. #import "Nextcloud-Swift.h"
  29. #endif
  30. @interface CCFavorites () <CCActionsDeleteDelegate, CCActionsSettingFavoriteDelegate>
  31. {
  32. NSArray *_dataSource;
  33. BOOL _reloadDataSource;
  34. CCHud *_hudDeterminate;
  35. }
  36. @end
  37. @implementation CCFavorites
  38. #pragma --------------------------------------------------------------------------------------------
  39. #pragma mark ===== Init =====
  40. #pragma --------------------------------------------------------------------------------------------
  41. - (id)initWithCoder:(NSCoder *)aDecoder
  42. {
  43. if (self = [super initWithCoder:aDecoder]) {
  44. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  45. }
  46. return self;
  47. }
  48. - (void)viewDidLoad
  49. {
  50. [super viewDidLoad];
  51. // Custom Cell
  52. [self.tableView registerNib:[UINib nibWithNibName:@"CCFavoritesCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
  53. // dataSource
  54. _dataSource = [NSMutableArray new];
  55. // Metadata
  56. _metadata = [CCMetadata new];
  57. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
  58. self.tableView.separatorColor = [NCColorBrand sharedInstance].seperator;
  59. self.tableView.emptyDataSetDelegate = self;
  60. self.tableView.emptyDataSetSource = self;
  61. self.tableView.delegate = self;
  62. // calculate _serverUrl
  63. if (!_serverUrl)
  64. _serverUrl = nil;
  65. // Title
  66. if (_titleViewControl)
  67. self.title = _titleViewControl;
  68. else
  69. self.title = NSLocalizedString(@"_favorites_", nil);
  70. }
  71. // Apparirà
  72. - (void)viewWillAppear:(BOOL)animated
  73. {
  74. [super viewWillAppear:animated];
  75. // Color
  76. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  77. [app aspectTabBar:self.tabBarController.tabBar hidden:NO];
  78. // Plus Button
  79. [app plusButtonVisibile:true];
  80. [self reloadDatasource];
  81. }
  82. // E' arrivato
  83. - (void)viewDidAppear:(BOOL)animated
  84. {
  85. [super viewDidAppear:animated];
  86. }
  87. - (void)didReceiveMemoryWarning {
  88. [super didReceiveMemoryWarning];
  89. }
  90. - (void)triggerProgressTask:(NSNotification *)notification
  91. {
  92. NSDictionary *dict = notification.userInfo;
  93. float progress = [[dict valueForKey:@"progress"] floatValue];
  94. if (progress == 0)
  95. [self.navigationController cancelCCProgress];
  96. else
  97. [self.navigationController setCCProgressPercentage:progress*100 andTintColor:[NCColorBrand sharedInstance].navigationBarProgress];
  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:image_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 ===== UIDocumentInteractionController <delegate> =====
  127. #pragma --------------------------------------------------------------------------------------------
  128. - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
  129. {
  130. // evitiamo il rimando della eventuale photo e/o video
  131. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) {
  132. [CCCoreData setCameraUploadDatePhoto:[NSDate date]];
  133. [CCCoreData setCameraUploadDateVideo:[NSDate date]];
  134. }
  135. }
  136. #pragma --------------------------------------------------------------------------------------------
  137. #pragma mark ===== Delete <delegate> =====
  138. #pragma--------------------------------------------------------------------------------------------
  139. - (void)deleteFileOrFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  140. {
  141. NSLog(@"[LOG] Delete error %@", message);
  142. }
  143. - (void)deleteFileOrFolderSuccess:(CCMetadataNet *)metadataNet
  144. {
  145. [self reloadDatasource];
  146. }
  147. #pragma --------------------------------------------------------------------------------------------
  148. #pragma mark ===== Favorite <delegate> =====
  149. #pragma--------------------------------------------------------------------------------------------
  150. - (void)settingFavoriteFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  151. {
  152. NSLog(@"[LOG] Remove Favorite error %@", message);
  153. }
  154. - (void)settingFavoriteSuccess:(CCMetadataNet *)metadataNet
  155. {
  156. [CCCoreData setMetadataFavoriteFileID:metadataNet.fileID favorite:[metadataNet.options boolValue] activeAccount:app.activeAccount context:nil];
  157. [self reloadDatasource];
  158. }
  159. #pragma --------------------------------------------------------------------------------------------
  160. #pragma mark ==== Download Thumbnail <Delegate> ====
  161. #pragma --------------------------------------------------------------------------------------------
  162. - (void)downloadThumbnailSuccess:(CCMetadataNet *)metadataNet
  163. {
  164. [self reloadDatasource];
  165. }
  166. #pragma --------------------------------------------------------------------------------------------
  167. #pragma mark ==== Download <Delegate> ====
  168. #pragma --------------------------------------------------------------------------------------------
  169. - (void)downloadFileFailure:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  170. {
  171. [app messageNotification:@"_download_file_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  172. }
  173. - (void)downloadFileSuccess:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  174. {
  175. _metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, app.activeAccount] context:nil];
  176. if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_compress]) {
  177. [self performSelector:@selector(unZipFile:) withObject:_metadata.fileID];
  178. } else if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_unknown]) {
  179. [self openWith:_metadata];
  180. } else {
  181. if ([self shouldPerformSegue])
  182. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  183. }
  184. }
  185. #pragma --------------------------------------------------------------------------------------------
  186. #pragma mark ===== menu =====
  187. #pragma--------------------------------------------------------------------------------------------
  188. - (void)openModel:(CCMetadata *)metadata
  189. {
  190. UIViewController *viewController;
  191. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:app.activeAccount];
  192. if ([metadata.model isEqualToString:@"cartadicredito"])
  193. viewController = [[CCCartaDiCredito alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  194. if ([metadata.model isEqualToString:@"bancomat"])
  195. viewController = [[CCBancomat alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  196. if ([metadata.model isEqualToString:@"contocorrente"])
  197. viewController = [[CCContoCorrente alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  198. if ([metadata.model isEqualToString:@"accountweb"])
  199. viewController = [[CCAccountWeb alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  200. if ([metadata.model isEqualToString:@"patenteguida"])
  201. viewController = [[CCPatenteGuida alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  202. if ([metadata.model isEqualToString:@"cartaidentita"])
  203. viewController = [[CCCartaIdentita alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  204. if ([metadata.model isEqualToString:@"passaporto"])
  205. viewController = [[CCPassaporto alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  206. if ([metadata.model isEqualToString:@"note"]) {
  207. viewController = [[CCNote alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid fileID:metadata.fileID isLocal:NO serverUrl:serverUrl];
  208. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  209. [self presentViewController:navigationController animated:YES completion:nil];
  210. } else {
  211. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  212. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  213. [self presentViewController:navigationController animated:YES completion:nil];
  214. }
  215. }
  216. - (void)openWith:(CCMetadata *)metadata
  217. {
  218. NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID];
  219. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePath]) {
  220. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint] error:nil];
  221. [[NSFileManager defaultManager] linkItemAtPath:fileNamePath toPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint] error:nil];
  222. NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint]];
  223. _docController = [UIDocumentInteractionController interactionControllerWithURL:url];
  224. _docController.delegate = self;
  225. [_docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  226. }
  227. }
  228. - (void)requestDeleteMetadata:(CCMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  229. {
  230. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  231. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  232. [[CCActions sharedInstance] deleteFileOrFolder:metadata delegate:self];
  233. [self reloadDatasource];
  234. }]];
  235. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  236. }]];
  237. alertController.popoverPresentationController.sourceView = self.view;
  238. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  239. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  240. [alertController.view layoutIfNeeded];
  241. [self presentViewController:alertController animated:YES completion:nil];
  242. }
  243. #pragma --------------------------------------------------------------------------------------------
  244. #pragma mark ===== UnZipFile =====
  245. #pragma --------------------------------------------------------------------------------------------
  246. - (void)unZipFile:(NSString *)fileID
  247. {
  248. [_hudDeterminate visibleHudTitle:NSLocalizedString(@"_unzip_in_progress_", nil) mode:MBProgressHUDModeDeterminate color:nil];
  249. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  250. NSString *fileZip = [NSString stringWithFormat:@"%@/%@", app.directoryUser, fileID];
  251. [SSZipArchive unzipFileAtPath:fileZip toDestination:[CCUtility getDirectoryLocal] overwrite:YES password:nil progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
  252. dispatch_async(dispatch_get_main_queue(), ^{
  253. float progress = (float) entryNumber / (float)total;
  254. [_hudDeterminate progress:progress];
  255. });
  256. } completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
  257. dispatch_async(dispatch_get_main_queue(), ^{
  258. [_hudDeterminate hideHud];
  259. if (succeeded) [app messageNotification:@"_info_" description:@"_file_unpacked_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeSuccess];
  260. else [app messageNotification:@"_error_" description:[NSString stringWithFormat:@"Error %ld", (long)error.code] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  261. });
  262. }];
  263. });
  264. }
  265. - (void)requestMoreMetadata:(CCMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  266. {
  267. UIImage *iconHeader;
  268. metadata = [_dataSource objectAtIndex:indexPath.row];
  269. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.view title:nil];
  270. actionSheet.animationDuration = 0.2;
  271. actionSheet.blurRadius = 0.0f;
  272. actionSheet.blurTintColor = [UIColor colorWithWhite:0.0f alpha:0.50f];
  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:[NCColorBrand sharedInstance].cryptocloud };
  278. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  279. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[NCColorBrand sharedInstance].brand };
  280. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  281. actionSheet.separatorColor = [NCColorBrand 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. iconHeader = [UIImage imageNamed:metadata.iconName];
  288. [actionSheet addButtonWithTitle: metadata.fileNamePrint
  289. image: iconHeader
  290. backgroundColor: [NCColorBrand sharedInstance].tabBar
  291. height: 50.0
  292. type: AHKActionSheetButtonTypeDisabled
  293. handler: nil
  294. ];
  295. // ONLY Root Favorites : Remove file/folder Favorites
  296. if (_serverUrl == nil) {
  297. [actionSheet addButtonWithTitle:NSLocalizedString(@"_remove_favorites_", nil) image:[UIImage imageNamed:image_actionSheetOffline] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  298. [self.tableView setEditing:NO animated:YES];
  299. [[CCActions sharedInstance] settingFavorite:metadata favorite:NO delegate:self];
  300. }];
  301. }
  302. // Share
  303. if (_metadata.cryptated == NO && app.hasServerShareSupport) {
  304. [actionSheet addButtonWithTitle:NSLocalizedString(@"_share_", nil)
  305. image:[UIImage imageNamed:image_actionSheetShare]
  306. backgroundColor:[UIColor whiteColor]
  307. height: 50.0
  308. type:AHKActionSheetButtonTypeDefault
  309. handler:^(AHKActionSheet *as) {
  310. // close swipe
  311. [self setEditing:NO animated:YES];
  312. [app.activeMain openWindowShare:metadata];
  313. }];
  314. }
  315. // NO Directory - NO Template
  316. if (metadata.directory == NO && [metadata.type isEqualToString:k_metadataType_template] == NO) {
  317. [actionSheet addButtonWithTitle:NSLocalizedString(@"_open_in_", nil) image:[UIImage imageNamed:image_actionSheetOpenIn] backgroundColor:[UIColor whiteColor] height: 50.0 type:AHKActionSheetButtonTypeDefault handler:^(AHKActionSheet *as) {
  318. [self.tableView setEditing:NO animated:YES];
  319. [self openWith:metadata];
  320. }];
  321. }
  322. [actionSheet show];
  323. }
  324. #pragma mark -
  325. #pragma --------------------------------------------------------------------------------------------
  326. #pragma mark ===== Swipe Tablet -> menu =====
  327. #pragma --------------------------------------------------------------------------------------------
  328. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  329. {
  330. return UITableViewCellEditingStyleDelete;
  331. }
  332. - (NSString *)tableView:(UITableView *)tableView titleForSwipeAccessoryButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
  333. return NSLocalizedString(@"_more_", nil);
  334. }
  335. - (void)tableView:(UITableView *)tableView swipeAccessoryButtonPushedForRowAtIndexPath:(NSIndexPath *)indexPath
  336. {
  337. [self requestMoreMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  338. }
  339. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  340. {
  341. return NSLocalizedString(@"_delete_", nil);
  342. }
  343. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  344. {
  345. if (editingStyle == UITableViewCellEditingStyleDelete) {
  346. [self requestDeleteMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  347. }
  348. }
  349. #pragma --------------------------------------------------------------------------------------------
  350. #pragma mark ==== Table ====
  351. #pragma --------------------------------------------------------------------------------------------
  352. - (CCMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  353. {
  354. CCMetadata *metadata;
  355. NSManagedObject *record = [_dataSource objectAtIndex:indexPath.row];
  356. metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", [record valueForKey:@"fileID"], app.activeAccount] context:nil];
  357. return metadata;
  358. }
  359. - (void)readFolderWithForced:(BOOL)forced serverUrl:(NSString *)serverUrl
  360. {
  361. [self reloadDatasource];
  362. }
  363. - (void)reloadDatasource
  364. {
  365. NSMutableArray *metadatas = [NSMutableArray new];
  366. NSArray *recordsTableMetadata ;
  367. if (!_serverUrl) {
  368. recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (favorite == 1)", app.activeAccount] context:nil];
  369. } else {
  370. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_serverUrl activeAccount:app.activeAccount];
  371. recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, directoryID] fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  372. }
  373. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil replaceDateToExifDate:NO activeAccount:app.activeAccount];
  374. NSArray *fileIDs = [sectionDataSource.sectionArrayRow objectForKey:@"_none_"];
  375. for (NSString *fileID in fileIDs)
  376. [metadatas addObject:[sectionDataSource.allRecordsDataSource objectForKey:fileID]];
  377. _dataSource = [NSArray arrayWithArray:metadatas];
  378. [self.tableView reloadData];
  379. }
  380. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  381. {
  382. return 60;
  383. }
  384. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  385. {
  386. return 1;
  387. }
  388. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  389. {
  390. return [_dataSource count];
  391. }
  392. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  393. {
  394. CCFavoritesCell *cell = (CCFavoritesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  395. CCMetadata *metadata;
  396. // separator
  397. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  398. // Initialize
  399. cell.statusImageView.image = nil;
  400. cell.offlineImageView.image = nil;
  401. // change color selection
  402. UIView *selectionColor = [[UIView alloc] init];
  403. selectionColor.backgroundColor = [NCColorBrand sharedInstance].selectBackgrond;
  404. cell.selectedBackgroundView = selectionColor;
  405. metadata = [_dataSource objectAtIndex:indexPath.row];
  406. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  407. if (_serverUrl == nil)
  408. cell.offlineImageView.image = [UIImage imageNamed:image_favorite];
  409. if (cell.fileImageView.image == nil && metadata.thumbnailExists)
  410. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  411. // encrypted color
  412. if (metadata.cryptated) {
  413. cell.labelTitle.textColor = [NCColorBrand sharedInstance].cryptocloud;
  414. } else {
  415. cell.labelTitle.textColor = [UIColor blackColor];
  416. }
  417. // File name
  418. cell.labelTitle.text = metadata.fileNamePrint;
  419. cell.labelInfoFile.text = @"";
  420. // Immagine del file, se non c'è l'anteprima mettiamo quella standard
  421. if (cell.fileImageView.image == nil)
  422. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  423. // it's encrypted ???
  424. if (metadata.cryptated && [metadata.type isEqualToString: k_metadataType_template] == NO)
  425. cell.statusImageView.image = [UIImage imageNamed:image_lock];
  426. // text and length
  427. if (metadata.directory) {
  428. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  429. cell.accessoryType = UITableViewCellAccessoryNone;
  430. //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  431. } else {
  432. NSString *date = [CCUtility dateDiff:metadata.date];
  433. NSString *length = [CCUtility transformedSize:metadata.size];
  434. if ([metadata.type isEqualToString: k_metadataType_template])
  435. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", date];
  436. if ([metadata.type isEqualToString: k_metadataType_file] || [metadata.type isEqualToString: k_metadataType_local]) {
  437. BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID]];
  438. if (fileExists)
  439. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ • %@", date, length];
  440. else
  441. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ ◦ %@", date, length];
  442. }
  443. cell.accessoryType = UITableViewCellAccessoryNone;
  444. }
  445. return cell;
  446. }
  447. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  448. {
  449. // deselect row
  450. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  451. _metadata = [self setSelfMetadataFromIndexPath:indexPath];
  452. // if is in download [do not touch]
  453. if ([_metadata.session length] > 0 && [_metadata.session containsString:@"download"])
  454. return;
  455. // File
  456. if (([_metadata.type isEqualToString: k_metadataType_file]) && _metadata.directory == NO) {
  457. // File do not exists
  458. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:_metadata.account];
  459. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, _metadata.fileID]]) {
  460. [self downloadFileSuccess:_metadata.fileID serverUrl:serverUrl selector:selectorLoadFileView selectorPost:nil];
  461. } else {
  462. [[CCNetworking sharedNetworking] downloadFile:_metadata serverUrl:serverUrl downloadData:YES downloadPlist:NO selector:selectorLoadFileView selectorPost:nil session:k_download_session taskStatus:k_taskStatusResume delegate:self];
  463. }
  464. }
  465. // Model
  466. if ([self.metadata.type isEqualToString: k_metadataType_template])
  467. [self openModel:self.metadata];
  468. // Directory
  469. if (_metadata.directory)
  470. [self performSegueDirectoryWithControlPasscode];
  471. }
  472. -(void)performSegueDirectoryWithControlPasscode
  473. {
  474. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  475. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:app.activeAccount];
  476. vc.serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:_metadata.fileNameData];
  477. vc.titleViewControl = _metadata.fileNamePrint;
  478. [self.navigationController pushViewController:vc animated:YES];
  479. }
  480. #pragma --------------------------------------------------------------------------------------------
  481. #pragma mark ===== Navigation ====
  482. #pragma --------------------------------------------------------------------------------------------
  483. - (BOOL)shouldPerformSegue
  484. {
  485. // if i am in background -> exit
  486. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  487. // if i am not window -> exit
  488. if (self.view.window == NO)
  489. return NO;
  490. // Collapsed but i am in detail -> exit
  491. if (self.splitViewController.isCollapsed)
  492. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  493. // Video in run -> exit
  494. if (self.detailViewController.photoBrowser.currentVideoPlayerViewController.isViewLoaded && self.detailViewController.photoBrowser.currentVideoPlayerViewController.view.window) return NO;
  495. return YES;
  496. }
  497. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  498. {
  499. id viewController = segue.destinationViewController;
  500. if ([viewController isKindOfClass:[UINavigationController class]]) {
  501. UINavigationController *nav = viewController;
  502. _detailViewController = (CCDetail *)nav.topViewController;
  503. } else {
  504. _detailViewController = segue.destinationViewController;
  505. }
  506. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  507. for (CCMetadata *metadata in _dataSource) {
  508. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  509. [allRecordsDataSourceImagesVideos addObject:metadata];
  510. }
  511. _detailViewController.metadataDetail = _metadata;
  512. _detailViewController.dateFilterQuery = nil;
  513. _detailViewController.isCameraUpload = NO;
  514. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  515. [_detailViewController setTitle:_metadata.fileNamePrint];
  516. }
  517. @end