CCFavorites.m 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 = [tableMetadata 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. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  127. // evitiamo il rimando della eventuale photo e/o video
  128. if (tableAccount.cameraUpload) {
  129. [[NCManageDatabase sharedInstance] setAccountCameraUploadDateAssetType:PHAssetMediaTypeImage assetDate:[NSDate date]];
  130. [[NCManageDatabase sharedInstance] setAccountCameraUploadDateAssetType:PHAssetMediaTypeVideo assetDate:[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.etag favorite:[metadataNet.options boolValue] activeAccount:app.activeAccount context:nil];
  154. [[NCManageDatabase sharedInstance] setMetadataFavorite:metadataNet.etag favorite:[metadataNet.options boolValue]];
  155. [self reloadDatasource];
  156. }
  157. #pragma --------------------------------------------------------------------------------------------
  158. #pragma mark ==== Download Thumbnail <Delegate> ====
  159. #pragma --------------------------------------------------------------------------------------------
  160. - (void)downloadThumbnailSuccess:(CCMetadataNet *)metadataNet
  161. {
  162. [self reloadDatasource];
  163. }
  164. #pragma --------------------------------------------------------------------------------------------
  165. #pragma mark ==== Download <Delegate> ====
  166. #pragma --------------------------------------------------------------------------------------------
  167. - (void)downloadFileFailure:(NSString *)etag serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  168. {
  169. [app messageNotification:@"_download_file_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  170. }
  171. - (void)downloadFileSuccess:(NSString *)etag serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  172. {
  173. //_metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(etag == %@) AND (account == %@)", etag, app.activeAccount] context:nil];
  174. _metadata = [[NCManageDatabase sharedInstance] getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(etag == %@) AND (account == %@)", etag, app.activeAccount]];
  175. if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_compress]) {
  176. [self performSelector:@selector(unZipFile:) withObject:_metadata.etag];
  177. } else if ([_metadata.typeFile isEqualToString: k_metadataTypeFile_unknown]) {
  178. [self openWith:_metadata];
  179. } else {
  180. if ([self shouldPerformSegue])
  181. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  182. }
  183. }
  184. #pragma --------------------------------------------------------------------------------------------
  185. #pragma mark ===== menu =====
  186. #pragma--------------------------------------------------------------------------------------------
  187. - (void)openModel:(tableMetadata *)metadata
  188. {
  189. UIViewController *viewController;
  190. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:app.activeAccount];
  191. if ([metadata.model isEqualToString:@"cartadicredito"])
  192. viewController = [[CCCartaDiCredito alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid etag:metadata.etag isLocal:NO serverUrl:serverUrl];
  193. if ([metadata.model isEqualToString:@"bancomat"])
  194. viewController = [[CCBancomat alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid etag:metadata.etag isLocal:NO serverUrl:serverUrl];
  195. if ([metadata.model isEqualToString:@"contocorrente"])
  196. viewController = [[CCContoCorrente alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid etag:metadata.etag isLocal:NO serverUrl:serverUrl];
  197. if ([metadata.model isEqualToString:@"accountweb"])
  198. viewController = [[CCAccountWeb alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid etag:metadata.etag isLocal:NO serverUrl:serverUrl];
  199. if ([metadata.model isEqualToString:@"patenteguida"])
  200. viewController = [[CCPatenteGuida alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid etag:metadata.etag isLocal:NO serverUrl:serverUrl];
  201. if ([metadata.model isEqualToString:@"cartaidentita"])
  202. viewController = [[CCCartaIdentita alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid etag:metadata.etag isLocal:NO serverUrl:serverUrl];
  203. if ([metadata.model isEqualToString:@"passaporto"])
  204. viewController = [[CCPassaporto alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid etag:metadata.etag isLocal:NO serverUrl:serverUrl];
  205. if ([metadata.model isEqualToString:@"note"]) {
  206. viewController = [[CCNote alloc] initWithDelegate:self fileName:metadata.fileName uuid:metadata.uuid etag:metadata.etag isLocal:NO serverUrl:serverUrl];
  207. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  208. [self presentViewController:navigationController animated:YES completion:nil];
  209. } else {
  210. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  211. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  212. [self presentViewController:navigationController animated:YES completion:nil];
  213. }
  214. }
  215. - (void)openWith:(tableMetadata *)metadata
  216. {
  217. NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.etag];
  218. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePath]) {
  219. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint] error:nil];
  220. [[NSFileManager defaultManager] linkItemAtPath:fileNamePath toPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint] error:nil];
  221. NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint]];
  222. _docController = [UIDocumentInteractionController interactionControllerWithURL:url];
  223. _docController.delegate = self;
  224. [_docController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  225. }
  226. }
  227. - (void)requestDeleteMetadata:(tableMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  228. {
  229. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  230. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  231. [[CCActions sharedInstance] deleteFileOrFolder:metadata delegate:self];
  232. [self reloadDatasource];
  233. }]];
  234. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  235. }]];
  236. alertController.popoverPresentationController.sourceView = self.view;
  237. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  238. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  239. [alertController.view layoutIfNeeded];
  240. [self presentViewController:alertController animated:YES completion:nil];
  241. }
  242. #pragma --------------------------------------------------------------------------------------------
  243. #pragma mark ===== UnZipFile =====
  244. #pragma --------------------------------------------------------------------------------------------
  245. - (void)unZipFile:(NSString *)etag
  246. {
  247. [_hudDeterminate visibleHudTitle:NSLocalizedString(@"_unzip_in_progress_", nil) mode:MBProgressHUDModeDeterminate color:nil];
  248. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  249. NSString *fileZip = [NSString stringWithFormat:@"%@/%@", app.directoryUser, etag];
  250. [SSZipArchive unzipFileAtPath:fileZip toDestination:[CCUtility getDirectoryLocal] overwrite:YES password:nil progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
  251. dispatch_async(dispatch_get_main_queue(), ^{
  252. float progress = (float) entryNumber / (float)total;
  253. [_hudDeterminate progress:progress];
  254. });
  255. } completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
  256. dispatch_async(dispatch_get_main_queue(), ^{
  257. [_hudDeterminate hideHud];
  258. if (succeeded) [app messageNotification:@"_info_" description:@"_file_unpacked_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeSuccess errorCode:0];
  259. else [app messageNotification:@"_error_" description:[NSString stringWithFormat:@"Error %ld", (long)error.code] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:0];
  260. });
  261. }];
  262. });
  263. }
  264. - (void)requestMoreMetadata:(tableMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  265. {
  266. UIImage *iconHeader;
  267. metadata = [_dataSource objectAtIndex:indexPath.row];
  268. AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithView:self.view title:nil];
  269. actionSheet.animationDuration = 0.2;
  270. actionSheet.blurRadius = 0.0f;
  271. actionSheet.blurTintColor = [UIColor colorWithWhite:0.0f alpha:0.50f];
  272. actionSheet.buttonHeight = 50.0;
  273. actionSheet.cancelButtonHeight = 50.0f;
  274. actionSheet.separatorHeight = 5.0f;
  275. actionSheet.automaticallyTintButtonImages = @(NO);
  276. actionSheet.encryptedButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].cryptocloud };
  277. actionSheet.buttonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  278. actionSheet.cancelButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[NCBrandColor sharedInstance].brand };
  279. actionSheet.disableButtonTextAttributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor] };
  280. actionSheet.separatorColor = [NCBrandColor sharedInstance].seperator;
  281. actionSheet.cancelButtonTitle = NSLocalizedString(@"_cancel_",nil);
  282. // assegnamo l'immagine anteprima se esiste, altrimenti metti quella standars
  283. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.etag]]) {
  284. iconHeader = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.etag]];
  285. } else {
  286. if (metadata.directory)
  287. iconHeader = [CCGraphics changeThemingColorImage:[UIImage imageNamed:metadata.iconName] color:[NCBrandColor sharedInstance].brand];
  288. else
  289. iconHeader = [UIImage imageNamed:metadata.iconName];
  290. }
  291. [actionSheet addButtonWithTitle: metadata.fileNamePrint image: iconHeader backgroundColor: [NCBrandColor sharedInstance].tabBar height: 50.0 type: AHKActionSheetButtonTypeDisabled handler: nil
  292. ];
  293. // ONLY Root Favorites : Remove file/folder Favorites
  294. if (_serverUrl == nil) {
  295. [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) {
  296. [self.tableView setEditing:NO animated:YES];
  297. [[CCActions sharedInstance] settingFavorite:metadata favorite:NO delegate:self];
  298. }];
  299. }
  300. // Share
  301. if (_metadata.cryptated == NO) {
  302. [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) {
  303. // close swipe
  304. [self setEditing:NO animated:YES];
  305. [app.activeMain openWindowShare:metadata];
  306. }];
  307. }
  308. // NO Directory - NO Template
  309. if (metadata.directory == NO && [metadata.type isEqualToString:k_metadataType_template] == NO) {
  310. [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) {
  311. [self.tableView setEditing:NO animated:YES];
  312. [self openWith:metadata];
  313. }];
  314. }
  315. [actionSheet show];
  316. }
  317. #pragma mark -
  318. #pragma --------------------------------------------------------------------------------------------
  319. #pragma mark ===== Swipe Tablet -> menu =====
  320. #pragma --------------------------------------------------------------------------------------------
  321. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  322. {
  323. return UITableViewCellEditingStyleDelete;
  324. }
  325. - (NSString *)tableView:(UITableView *)tableView titleForSwipeAccessoryButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
  326. return NSLocalizedString(@"_more_", nil);
  327. }
  328. - (void)tableView:(UITableView *)tableView swipeAccessoryButtonPushedForRowAtIndexPath:(NSIndexPath *)indexPath
  329. {
  330. [self requestMoreMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  331. }
  332. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  333. {
  334. return NSLocalizedString(@"_delete_", nil);
  335. }
  336. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  337. {
  338. if (editingStyle == UITableViewCellEditingStyleDelete) {
  339. [self requestDeleteMetadata:[_dataSource objectAtIndex:indexPath.row] indexPath:indexPath];
  340. }
  341. }
  342. #pragma --------------------------------------------------------------------------------------------
  343. #pragma mark ==== Table ====
  344. #pragma --------------------------------------------------------------------------------------------
  345. - (tableMetadata *)setSelfMetadataFromIndexPath:(NSIndexPath *)indexPath
  346. {
  347. tableMetadata *metadata;
  348. NSManagedObject *record = [_dataSource objectAtIndex:indexPath.row];
  349. //metadata = [CCCoreData getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(etag == %@) AND (account == %@)", [record valueForKey:@"etag"], app.activeAccount] context:nil];
  350. metadata = [[NCManageDatabase sharedInstance] getMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(etag == %@) AND (account == %@)", [record valueForKey:@"etag"], app.activeAccount]];
  351. return metadata;
  352. }
  353. - (void)readFolderWithForced:(BOOL)forced serverUrl:(NSString *)serverUrl
  354. {
  355. [self reloadDatasource];
  356. }
  357. - (void)reloadDatasource
  358. {
  359. NSMutableArray *metadatas = [NSMutableArray new];
  360. NSArray *recordsTableMetadata ;
  361. if (!_serverUrl) {
  362. //recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (favorite == 1)", app.activeAccount] context:nil];
  363. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPreficate:[NSPredicate predicateWithFormat:@"(account == %@) AND (favorite == 1)", app.activeAccount] sorted:nil ascending:NO];
  364. } else {
  365. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_serverUrl activeAccount:app.activeAccount];
  366. recordsTableMetadata = [[NCManageDatabase sharedInstance] getMetadatasWithPreficate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, directoryID] sorted:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  367. //recordsTableMetadata = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, directoryID] fieldOrder:[CCUtility getOrderSettings] ascending:[CCUtility getAscendingSettings]];
  368. }
  369. CCSectionDataSourceMetadata *sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:recordsTableMetadata listProgressMetadata:nil groupByField:nil replaceDateToExifDate:NO activeAccount:app.activeAccount];
  370. NSArray *etags = [sectionDataSource.sectionArrayRow objectForKey:@"_none_"];
  371. for (NSString *etag in etags)
  372. [metadatas addObject:[sectionDataSource.allRecordsDataSource objectForKey:etag]];
  373. _dataSource = [NSArray arrayWithArray:metadatas];
  374. [self.tableView reloadData];
  375. }
  376. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  377. {
  378. return 60;
  379. }
  380. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  381. {
  382. return 1;
  383. }
  384. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  385. {
  386. return [_dataSource count];
  387. }
  388. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  389. {
  390. CCFavoritesCell *cell = (CCFavoritesCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  391. tableMetadata *metadata;
  392. // separator
  393. cell.separatorInset = UIEdgeInsetsMake(0.f, 60.f, 0.f, 0.f);
  394. // Initialize
  395. cell.statusImageView.image = nil;
  396. cell.offlineImageView.image = nil;
  397. // change color selection
  398. UIView *selectionColor = [[UIView alloc] init];
  399. selectionColor.backgroundColor = [[NCBrandColor sharedInstance] getColorSelectBackgrond];
  400. cell.selectedBackgroundView = selectionColor;
  401. metadata = [_dataSource objectAtIndex:indexPath.row];
  402. cell.fileImageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.etag]];
  403. if (_serverUrl == nil)
  404. cell.offlineImageView.image = [UIImage imageNamed:@"favorite"];
  405. if (cell.fileImageView.image == nil && metadata.thumbnailExists)
  406. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  407. // encrypted color
  408. if (metadata.cryptated) {
  409. cell.labelTitle.textColor = [NCBrandColor sharedInstance].cryptocloud;
  410. } else {
  411. cell.labelTitle.textColor = [UIColor blackColor];
  412. }
  413. // File name
  414. cell.labelTitle.text = metadata.fileNamePrint;
  415. cell.labelInfoFile.text = @"";
  416. // Immagine del file, se non c'è l'anteprima mettiamo quella standard
  417. if (cell.fileImageView.image == nil) {
  418. if (metadata.directory) {
  419. cell.fileImageView.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:metadata.iconName] color:[NCBrandColor sharedInstance].brand];
  420. } else {
  421. cell.fileImageView.image = [UIImage imageNamed:metadata.iconName];
  422. }
  423. }
  424. // it's encrypted ???
  425. if (metadata.cryptated && [metadata.type isEqualToString: k_metadataType_template] == NO)
  426. cell.statusImageView.image = [UIImage imageNamed:@"lock"];
  427. // text and length
  428. if (metadata.directory) {
  429. cell.labelInfoFile.text = [CCUtility dateDiff:metadata.date];
  430. cell.accessoryType = UITableViewCellAccessoryNone;
  431. //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  432. } else {
  433. NSString *date = [CCUtility dateDiff:metadata.date];
  434. NSString *length = [CCUtility transformedSize:metadata.size];
  435. if ([metadata.type isEqualToString: k_metadataType_template])
  436. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@", date];
  437. if ([metadata.type isEqualToString: k_metadataType_file] || [metadata.type isEqualToString: k_metadataType_local]) {
  438. BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.etag]];
  439. if (fileExists)
  440. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ • %@", date, length];
  441. else
  442. cell.labelInfoFile.text = [NSString stringWithFormat:@"%@ ◦ %@", date, length];
  443. }
  444. cell.accessoryType = UITableViewCellAccessoryNone;
  445. }
  446. return cell;
  447. }
  448. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  449. {
  450. // deselect row
  451. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  452. _metadata = [self setSelfMetadataFromIndexPath:indexPath];
  453. // if is in download [do not touch]
  454. if ([_metadata.session length] > 0 && [_metadata.session containsString:@"download"])
  455. return;
  456. // File
  457. if (([_metadata.type isEqualToString: k_metadataType_file]) && _metadata.directory == NO) {
  458. // File do not exists
  459. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:_metadata.account];
  460. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, _metadata.etag]]) {
  461. [self downloadFileSuccess:_metadata.etag serverUrl:serverUrl selector:selectorLoadFileView selectorPost:nil];
  462. } else {
  463. [[CCNetworking sharedNetworking] downloadFile:_metadata.etag serverUrl:serverUrl downloadData:YES downloadPlist:NO selector:selectorLoadFileView selectorPost:nil session:k_download_session taskStatus:k_taskStatusResume delegate:self];
  464. }
  465. }
  466. // Model
  467. if ([self.metadata.type isEqualToString: k_metadataType_template])
  468. [self openModel:self.metadata];
  469. // Directory
  470. if (_metadata.directory)
  471. [self performSegueDirectoryWithControlPasscode];
  472. }
  473. -(void)performSegueDirectoryWithControlPasscode
  474. {
  475. CCFavorites *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CCFavorites"];
  476. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:_metadata.directoryID activeAccount:app.activeAccount];
  477. vc.serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:_metadata.fileNameData];
  478. vc.titleViewControl = _metadata.fileNamePrint;
  479. [self.navigationController pushViewController:vc animated:YES];
  480. }
  481. #pragma --------------------------------------------------------------------------------------------
  482. #pragma mark ===== Navigation ====
  483. #pragma --------------------------------------------------------------------------------------------
  484. - (BOOL)shouldPerformSegue
  485. {
  486. // if i am in background -> exit
  487. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) return NO;
  488. // if i am not window -> exit
  489. if (self.view.window == NO)
  490. return NO;
  491. // Collapsed but i am in detail -> exit
  492. if (self.splitViewController.isCollapsed)
  493. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window) return NO;
  494. // Video in run -> exit
  495. if (self.detailViewController.photoBrowser.currentVideoPlayerViewController.isViewLoaded && self.detailViewController.photoBrowser.currentVideoPlayerViewController.view.window) return NO;
  496. return YES;
  497. }
  498. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  499. {
  500. id viewController = segue.destinationViewController;
  501. if ([viewController isKindOfClass:[UINavigationController class]]) {
  502. UINavigationController *nav = viewController;
  503. _detailViewController = (CCDetail *)nav.topViewController;
  504. } else {
  505. _detailViewController = segue.destinationViewController;
  506. }
  507. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  508. for (tableMetadata *metadata in _dataSource) {
  509. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  510. [allRecordsDataSourceImagesVideos addObject:metadata];
  511. }
  512. _detailViewController.metadataDetail = _metadata;
  513. _detailViewController.dateFilterQuery = nil;
  514. _detailViewController.isCameraUpload = NO;
  515. _detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  516. [_detailViewController setTitle:_metadata.fileNamePrint];
  517. }
  518. @end