CCFavorites.m 30 KB

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