CCFavorites.m 30 KB

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