CCPhotos.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. //
  2. // CCPhotos.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 29/07/15.
  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 "CCPhotos.h"
  24. #import "AppDelegate.h"
  25. #import "CCManageAutoUpload.h"
  26. #import "NCBridgeSwift.h"
  27. @interface CCPhotos () <CCActionsDeleteDelegate, CCActionsDownloadThumbnailDelegate>
  28. {
  29. tableMetadata *_metadata;
  30. BOOL _cellEditing;
  31. NSMutableArray *_queueMetadatas;
  32. NSMutableArray *_selectedMetadatas;
  33. NSUInteger _numSelectedMetadatas;
  34. CCSectionDataSourceMetadata *_sectionDataSource;
  35. CCHud *_hud;
  36. }
  37. @end
  38. @implementation CCPhotos
  39. #pragma --------------------------------------------------------------------------------------------
  40. #pragma mark ===== Init =====
  41. #pragma --------------------------------------------------------------------------------------------
  42. - (id)initWithCoder:(NSCoder *)aDecoder
  43. {
  44. if (self = [super initWithCoder:aDecoder]) {
  45. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  46. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  47. app.activePhotos = self;
  48. }
  49. return self;
  50. }
  51. #pragma --------------------------------------------------------------------------------------------
  52. #pragma mark ===== View =====
  53. #pragma --------------------------------------------------------------------------------------------
  54. - (void)viewDidLoad
  55. {
  56. [super viewDidLoad];
  57. _queueMetadatas = [[NSMutableArray alloc] init];
  58. _selectedMetadatas = [[NSMutableArray alloc] init];
  59. _hud = [[CCHud alloc] initWithView:[[[UIApplication sharedApplication] delegate] window]];
  60. // empty Data Source
  61. self.collectionView.emptyDataSetDelegate = self;
  62. self.collectionView.emptyDataSetSource = self;
  63. [self reloadDatasource];
  64. }
  65. // Apparirà
  66. - (void)viewWillAppear:(BOOL)animated
  67. {
  68. [super viewWillAppear:animated];
  69. // Color
  70. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  71. [app aspectTabBar:self.tabBarController.tabBar hidden:NO];
  72. // Plus Button
  73. [app plusButtonVisibile:true];
  74. }
  75. // E' arrivato
  76. - (void)viewDidAppear:(BOOL)animated
  77. {
  78. [super viewDidAppear:animated];
  79. [self reloadDatasource];
  80. }
  81. - (void)changeTheming
  82. {
  83. if (self.isViewLoaded && self.view.window)
  84. [app changeTheming:self];
  85. [self.collectionView reloadData];
  86. }
  87. #pragma --------------------------------------------------------------------------------------------
  88. #pragma mark ===== Gestione Grafica Window =====
  89. #pragma --------------------------------------------------------------------------------------------
  90. - (void)setUINavigationBarDefault
  91. {
  92. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  93. // select
  94. UIImage *icon = [UIImage imageNamed:@"seleziona"];
  95. UIBarButtonItem *buttonSelect = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStylePlain target:self action:@selector(collectionSelectYES)];
  96. if ([_sectionDataSource.allRecordsDataSource count] > 0) {
  97. self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:buttonSelect, nil];
  98. } else {
  99. self.navigationItem.rightBarButtonItems = nil;
  100. }
  101. self.navigationItem.leftBarButtonItem = nil;
  102. // Title
  103. self.navigationItem.title = NSLocalizedString(@"_photo_camera_", nil);
  104. }
  105. - (void)setUINavigationBarSelected
  106. {
  107. UIImage *icon;
  108. icon = [UIImage imageNamed:@"deleteSelectedFiles"];
  109. UIBarButtonItem *buttonDelete = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStylePlain target:self action:@selector(deleteSelectedFiles)];
  110. icon = [UIImage imageNamed:@"openSelectedFiles"];
  111. UIBarButtonItem *buttonOpenWith = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStylePlain target:self action:@selector(openSelectedFiles)];
  112. UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(reloadCollection)];
  113. self.navigationItem.leftBarButtonItem = leftButton;
  114. self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:buttonDelete, buttonOpenWith, nil];
  115. // Title
  116. self.navigationItem.title = [NSString stringWithFormat:@"%@ : %lu / %lu", NSLocalizedString(@"_selected_", nil), (unsigned long)[_selectedMetadatas count], (unsigned long)[_sectionDataSource.allRecordsDataSource count]];
  117. }
  118. - (void)collectionSelect:(BOOL)edit
  119. {
  120. [self.collectionView setAllowsMultipleSelection:edit];
  121. _cellEditing = edit;
  122. if (edit)
  123. [self setUINavigationBarSelected];
  124. else
  125. [self setUINavigationBarDefault];
  126. }
  127. - (void)collectionSelectYES
  128. {
  129. [self collectionSelect:YES];
  130. }
  131. - (void)cellSelect:(BOOL)select indexPath:(NSIndexPath *)indexPath metadata:(tableMetadata *)metadata
  132. {
  133. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
  134. UIVisualEffectView *effect = [cell viewWithTag:200];
  135. UIImageView *checked = [cell viewWithTag:300];
  136. if (select) {
  137. effect.hidden = NO;
  138. effect.alpha = 0.4;
  139. checked.hidden = NO;
  140. [_selectedMetadatas addObject:metadata];
  141. } else {
  142. effect.hidden = YES;
  143. checked.hidden = YES;
  144. [_selectedMetadatas removeObject:metadata];
  145. }
  146. // Title
  147. self.navigationItem.title = [NSString stringWithFormat:@"%@ : %lu / %lu", NSLocalizedString(@"_selected_", nil), (unsigned long)[_selectedMetadatas count], (unsigned long)[_sectionDataSource.allRecordsDataSource count]];
  148. }
  149. - (void)scrollToTop
  150. {
  151. [self.collectionView setContentOffset:CGPointMake(0, - self.collectionView.contentInset.top) animated:NO];
  152. }
  153. - (void)getGeoLocationForSection:(NSInteger)section
  154. {
  155. NSString *addLocation = @"";
  156. NSArray *fileIDsForKey = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:section]];
  157. for (NSString *fileID in fileIDsForKey) {
  158. tableLocalFile *localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", fileID]];
  159. if ([localFile.exifLatitude floatValue] > 0 || [localFile.exifLongitude floatValue] > 0) {
  160. NSString *location = [[NCManageDatabase sharedInstance] getLocationFromGeoLatitude:localFile.exifLatitude longitude:localFile.exifLongitude];
  161. addLocation = [NSString stringWithFormat:@"%@, %@", addLocation, location];
  162. }
  163. }
  164. }
  165. #pragma --------------------------------------------------------------------------------------------
  166. #pragma mark ==== DZNEmptyDataSetSource Methods ====
  167. #pragma --------------------------------------------------------------------------------------------
  168. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  169. {
  170. return [UIColor whiteColor];
  171. }
  172. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  173. {
  174. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"photosNoRecord"] color:[NCBrandColor sharedInstance].brand];
  175. }
  176. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  177. {
  178. NSString *text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_photo_view_", nil)];
  179. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  180. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  181. }
  182. - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
  183. {
  184. NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  185. paragraph.lineBreakMode = NSLineBreakByWordWrapping;
  186. paragraph.alignment = NSTextAlignmentCenter;
  187. NSString *text;
  188. tableAccount *account = [[NCManageDatabase sharedInstance] getAccountActive];
  189. if (account.autoUpload)
  190. text = [NSString stringWithFormat:@"%@", @"\n\n\n\n"];
  191. else
  192. text = [NSString stringWithFormat:@"\n%@\n", NSLocalizedString(@"_tutorial_autoupload_view_", nil)];
  193. NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
  194. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  195. }
  196. - (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
  197. {
  198. tableAccount *account = [[NCManageDatabase sharedInstance] getAccountActive];
  199. if (!account.autoUpload) {
  200. UIImage *buttonImage = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"button500x100"] color:[NCBrandColor sharedInstance].brand];
  201. return [CCGraphics drawText:NSLocalizedString(@"_activate_autoupload_", nil) inImage:buttonImage colorText:[UIColor whiteColor] sizeOfFont:26];
  202. } else return nil;
  203. }
  204. - (void)emptyDataSetDidTapButton:(UIScrollView *)scrollView
  205. {
  206. CCManageAutoUpload *viewController = [[CCManageAutoUpload alloc] initWithNibName:nil bundle:nil];
  207. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  208. [navigationController setModalPresentationStyle:UIModalPresentationFullScreen];
  209. [self presentViewController:navigationController animated:YES completion:nil];
  210. }
  211. #pragma --------------------------------------------------------------------------------------------
  212. #pragma mark ===== openSelectedFiles =====
  213. #pragma--------------------------------------------------------------------------------------------
  214. - (void)openSelectedFiles
  215. {
  216. NSMutableArray *dataToShare = [[NSMutableArray alloc] init];
  217. for (tableMetadata *metadata in _selectedMetadatas) {
  218. NSString *fileNamePath = [NSTemporaryDirectory() stringByAppendingString:metadata.fileNamePrint];
  219. [[NSFileManager defaultManager] linkItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID] toPath:fileNamePath error:nil];
  220. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePath]) {
  221. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image]) {
  222. NSData *data = [NSData dataWithData:UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:fileNamePath], 0.9)];
  223. [dataToShare addObject:data];
  224. }
  225. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video]) {
  226. [dataToShare addObject:[NSURL fileURLWithPath:fileNamePath]];
  227. }
  228. }
  229. }
  230. if ([dataToShare count] > 0) {
  231. UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
  232. // iPad
  233. activityViewController.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItems.lastObject;
  234. self.navigationItem.leftBarButtonItem.enabled = NO;
  235. self.navigationItem.rightBarButtonItem.enabled = NO;
  236. [self presentViewController:activityViewController animated:YES completion:^{
  237. [activityViewController setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
  238. self.navigationItem.leftBarButtonItem.enabled = YES;
  239. self.navigationItem.rightBarButtonItem.enabled = YES;
  240. if (completed) {
  241. [dataToShare enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  242. if ([obj isKindOfClass:[UIImage class]])
  243. [[NCManageDatabase sharedInstance] setAccountAutoUploadDateAssetType:PHAssetMediaTypeImage assetDate:[NSDate date]];
  244. if ([obj isKindOfClass:[NSURL class]])
  245. [[NCManageDatabase sharedInstance] setAccountAutoUploadDateAssetType:PHAssetMediaTypeVideo assetDate:[NSDate date]];
  246. }];
  247. [self performSelector:@selector(reloadCollection) withObject:nil];
  248. }
  249. }];
  250. }];
  251. }
  252. }
  253. #pragma --------------------------------------------------------------------------------------------
  254. #pragma mark ===== Download =====
  255. #pragma--------------------------------------------------------------------------------------------
  256. - (void)downloadFileFailure:(NSInteger)errorCode
  257. {
  258. [app messageNotification:@"_download_selected_files_" description:@"_error_download_photobrowser_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  259. }
  260. - (void)downloadFileSuccess:(tableMetadata *)metadata
  261. {
  262. NSIndexPath *indexPath;
  263. BOOL existsIcon = NO;
  264. if (metadata.fileID) {
  265. existsIcon = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  266. indexPath = [_sectionDataSource.fileIDIndexPath objectForKey:metadata.fileID];
  267. }
  268. if (indexPath && existsIcon) {
  269. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
  270. if (cell) {
  271. UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
  272. UIVisualEffectView *effect = [cell viewWithTag:200];
  273. UIImageView *checked = [cell viewWithTag:300];
  274. imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  275. effect.hidden = YES;
  276. checked.hidden = YES;
  277. [app.icoImagesCache setObject:imageView.image forKey:metadata.fileID];
  278. }
  279. }
  280. }
  281. #pragma --------------------------------------------------------------------------------------------
  282. #pragma mark ===== Delete =====
  283. #pragma--------------------------------------------------------------------------------------------
  284. - (void)deleteFileOrFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  285. {
  286. [self deleteFileOrFolderSuccess:metadataNet];
  287. }
  288. - (void)deleteFileOrFolderSuccess:(CCMetadataNet *)metadataNet
  289. {
  290. [_queueMetadatas removeObject:metadataNet.selector];
  291. if ([_queueMetadatas count] == 0) {
  292. [_hud hideHud];
  293. if ([_selectedMetadatas count] > 0) {
  294. [_selectedMetadatas removeObjectAtIndex:0];
  295. if ([_selectedMetadatas count] > 0) {
  296. [self deleteFileOrFolder:[_selectedMetadatas objectAtIndex:0] numFile:[_selectedMetadatas count] ofFile:_numSelectedMetadatas];
  297. } else {
  298. [self reloadDatasource];
  299. }
  300. } else {
  301. [self reloadDatasource];
  302. }
  303. }
  304. }
  305. - (void)deleteFileOrFolder:(tableMetadata *)metadata numFile:(NSInteger)numFile ofFile:(NSInteger)ofFile
  306. {
  307. if (metadata.cryptated) {
  308. [_queueMetadatas addObject:selectorDeleteCrypto];
  309. [_queueMetadatas addObject:selectorDeletePlist];
  310. } else {
  311. [_queueMetadatas addObject:selectorDelete];
  312. }
  313. [[CCActions sharedInstance] deleteFileOrFolder:metadata delegate:self];
  314. [_hud visibleHudTitle:[NSString stringWithFormat:NSLocalizedString(@"_delete_file_n_", nil), ofFile - numFile + 1, ofFile] mode:MBProgressHUDModeIndeterminate color:nil];
  315. }
  316. - (void)deleteSelectedFiles
  317. {
  318. [_queueMetadatas removeAllObjects];
  319. _numSelectedMetadatas = [_selectedMetadatas count];
  320. if ([_selectedMetadatas count] == 0)
  321. return;
  322. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  323. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  324. style:UIAlertActionStyleDestructive
  325. handler:^(UIAlertAction *action) {
  326. [self deleteFileOrFolder:[_selectedMetadatas objectAtIndex:0] numFile:[_selectedMetadatas count] ofFile:_numSelectedMetadatas];
  327. }]];
  328. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  329. style:UIAlertActionStyleCancel
  330. handler:^(UIAlertAction *action) {
  331. [alertController dismissViewControllerAnimated:YES completion:nil];
  332. }]];
  333. alertController.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItems.firstObject;
  334. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  335. [alertController.view layoutIfNeeded];
  336. [self presentViewController:alertController animated:YES completion:NULL];
  337. }
  338. #pragma --------------------------------------------------------------------------------------------
  339. #pragma mark ==== Download Thumbnail Delegate ====
  340. #pragma --------------------------------------------------------------------------------------------
  341. - (void)downloadThumbnailSuccess:(CCMetadataNet *)metadataNet
  342. {
  343. NSIndexPath *indexPath = [_sectionDataSource.fileIDIndexPath objectForKey:metadataNet.fileID];
  344. if (indexPath && [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadataNet.fileID]])
  345. [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
  346. }
  347. - (void)triggerProgressTask:(NSNotification *)notification
  348. {
  349. NSDictionary *dict = notification.userInfo;
  350. float progress = [[dict valueForKey:@"progress"] floatValue];
  351. if (progress == 0)
  352. [self.navigationController cancelCCProgress];
  353. else
  354. [self.navigationController setCCProgressPercentage:progress*100 andTintColor:[NCBrandColor sharedInstance].navigationBarProgress];
  355. }
  356. #pragma --------------------------------------------------------------------------------------------
  357. #pragma mark ==== Collection ====
  358. #pragma --------------------------------------------------------------------------------------------
  359. - (void)reloadDatasourceForced
  360. {
  361. [CCSectionMetadata removeAllObjectsSectionDataSource:_sectionDataSource];
  362. [self reloadDatasource];
  363. }
  364. - (void)reloadDatasource
  365. {
  366. // test
  367. if (app.activeAccount.length == 0)
  368. return;
  369. NSString *autoUploadPath = [[NCManageDatabase sharedInstance] getAccountAutoUploadPath:app.activeUrl];
  370. if (_sectionDataSource) {
  371. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  372. NSArray *metadatas = [[NCManageDatabase sharedInstance] getTableMetadatasPhotosWithServerUrl:autoUploadPath];
  373. _sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:metadatas listProgressMetadata:nil groupByField:@"date" replaceDateToExifDate:YES activeAccount:app.activeAccount];
  374. dispatch_async(dispatch_get_main_queue(), ^{
  375. [self reloadCollection];
  376. });
  377. });
  378. } else {
  379. NSArray *results = [[NCManageDatabase sharedInstance] getTableMetadatasPhotosWithServerUrl:autoUploadPath];
  380. _sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:results listProgressMetadata:nil groupByField:@"date" replaceDateToExifDate:YES activeAccount:app.activeAccount];
  381. [self reloadCollection];
  382. }
  383. }
  384. - (void)reloadCollection
  385. {
  386. [self.collectionView reloadData];
  387. [_selectedMetadatas removeAllObjects];
  388. [self collectionSelect:NO];
  389. }
  390. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  391. {
  392. return [[_sectionDataSource.sectionArrayRow allKeys] count];
  393. }
  394. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  395. {
  396. return [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:section]] count];
  397. }
  398. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  399. {
  400. UIInterfaceOrientation orientationOnLunch = [[UIApplication sharedApplication] statusBarOrientation];
  401. if (orientationOnLunch == UIInterfaceOrientationPortrait)
  402. return CGSizeMake(collectionView.frame.size.width / 5.3f, collectionView.frame.size.width / 5.3f);
  403. else
  404. return CGSizeMake(collectionView.frame.size.width / 7.3f, collectionView.frame.size.width / 7.3f);
  405. }
  406. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  407. {
  408. if ([_sectionDataSource.sections count] - 1 == section)
  409. return CGSizeMake(collectionView.frame.size.width, 50);
  410. return CGSizeZero;
  411. }
  412. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  413. {
  414. if (kind == UICollectionElementKindSectionHeader) {
  415. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
  416. //headerView.backgroundColor = COLOR_GROUPBY_BAR_NO_BLUR;
  417. [self getGeoLocationForSection:indexPath.section];
  418. UILabel *titleLabel = (UILabel *)[headerView viewWithTag:100];
  419. titleLabel.textColor = [UIColor blackColor];
  420. titleLabel.text = [CCUtility getTitleSectionDate:[_sectionDataSource.sections objectAtIndex:indexPath.section]];
  421. return headerView;
  422. }
  423. if (kind == UICollectionElementKindSectionFooter) {
  424. UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
  425. UILabel *titleLabel = (UILabel *)[footerView viewWithTag:100];
  426. titleLabel.textColor = [UIColor grayColor];
  427. titleLabel.text = [NSString stringWithFormat:@"%lu %@, %lu %@", (long)_sectionDataSource.image, NSLocalizedString(@"photo", nil), (long)_sectionDataSource.video, NSLocalizedString(@"_video_", nil)];
  428. return footerView;
  429. }
  430. return nil;
  431. }
  432. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  433. {
  434. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  435. UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
  436. UIVisualEffectView *effect = [cell viewWithTag:200];
  437. UIImageView *checked = [cell viewWithTag:300];
  438. checked.image = [UIImage imageNamed:@"checked"];
  439. NSArray *metadatasForKey = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]];
  440. NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
  441. tableMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  442. // Image
  443. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]]) {
  444. imageView.image = [app.icoImagesCache objectForKey:metadata.fileID];
  445. if (imageView.image == nil) {
  446. // insert Image
  447. UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  448. imageView.image = image;
  449. [app.icoImagesCache setObject:image forKey:metadata.fileID];
  450. }
  451. } else {
  452. // Thumbnail not present
  453. imageView.image = [UIImage imageNamed:@"file_photo"];
  454. if (metadata.thumbnailExists)
  455. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  456. }
  457. // Cheched
  458. if (cell.selected) {
  459. checked.hidden = NO;
  460. effect.hidden = NO;
  461. effect.alpha = 0.4;
  462. } else {
  463. checked.hidden = YES;
  464. effect.hidden = YES;
  465. }
  466. return cell;
  467. }
  468. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  469. {
  470. NSArray *metadatasForKey = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]];
  471. NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
  472. _metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  473. //UICollectionViewCell *cell =[collectionView cellForItemAtIndexPath:indexPath];
  474. if (_cellEditing) {
  475. [self cellSelect:YES indexPath:indexPath metadata:_metadata];
  476. } else {
  477. if ([self shouldPerformSegue])
  478. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  479. }
  480. }
  481. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  482. {
  483. if (_cellEditing == NO)
  484. return;
  485. //UICollectionViewCell *cell =[collectionView cellForItemAtIndexPath:indexPath];
  486. NSArray *metadatasForKey = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]];
  487. NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
  488. _metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  489. [self cellSelect:NO indexPath:indexPath metadata:_metadata];
  490. }
  491. #pragma --------------------------------------------------------------------------------------------
  492. #pragma mark ===== Navigation ====
  493. #pragma --------------------------------------------------------------------------------------------
  494. - (BOOL)shouldPerformSegue
  495. {
  496. // Test
  497. // Background ? exit
  498. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground)
  499. return NO;
  500. // Not in first plain ? exit
  501. if (self.view.window == NO)
  502. return NO;
  503. // Collapsed but in first plain in detail exit
  504. if (self.splitViewController.isCollapsed)
  505. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window)
  506. return NO;
  507. // Video running exit
  508. if (self.detailViewController.photoBrowser.currentVideoPlayerViewController.isViewLoaded && self.detailViewController.photoBrowser.currentVideoPlayerViewController.view.window)
  509. return NO;
  510. // ok perform segue
  511. return YES;
  512. }
  513. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  514. {
  515. id controller = segue.destinationViewController;
  516. if ([controller isKindOfClass:[UINavigationController class]]) {
  517. UINavigationController *navigationController = controller;
  518. self.detailViewController = (CCDetail *)navigationController.topViewController;
  519. } else {
  520. self.detailViewController = segue.destinationViewController;
  521. }
  522. NSMutableArray *allRecordsDataSourceImagesVideos = [[NSMutableArray alloc] init];
  523. for (NSString *fileID in _sectionDataSource.allEtag) {
  524. tableMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  525. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  526. [allRecordsDataSourceImagesVideos addObject:metadata];
  527. }
  528. self.detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  529. self.detailViewController.metadataDetail = _metadata;
  530. self.detailViewController.dateFilterQuery = _metadata.date;
  531. [self.detailViewController setTitle:_metadata.fileNamePrint];
  532. }
  533. @end