CCPhotos.m 29 KB

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