CCFavorites.m 30 KB

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