CCFavorites.m 30 KB

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