CCFavorites.m 28 KB

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