CCFavorites.m 27 KB

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