CCPhotos.m 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. //
  2. // CCPhotos.m
  3. // Nextcloud iOS
  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 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 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.fileName];
  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. [_queueMetadatas addObject:selectorDelete];
  296. [[CCActions sharedInstance] deleteFileOrFolder:metadata delegate:self];
  297. [_hud visibleHudTitle:[NSString stringWithFormat:NSLocalizedString(@"_delete_file_n_", nil), ofFile - numFile + 1, ofFile] mode:MBProgressHUDModeIndeterminate color:nil];
  298. }
  299. - (void)deleteSelectedFiles
  300. {
  301. [_queueMetadatas removeAllObjects];
  302. _numSelectedMetadatas = [_selectedMetadatas count];
  303. if ([_selectedMetadatas count] == 0)
  304. return;
  305. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  306. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  307. style:UIAlertActionStyleDestructive
  308. handler:^(UIAlertAction *action) {
  309. [self deleteFileOrFolder:[_selectedMetadatas objectAtIndex:0] numFile:[_selectedMetadatas count] ofFile:_numSelectedMetadatas];
  310. }]];
  311. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  312. style:UIAlertActionStyleCancel
  313. handler:^(UIAlertAction *action) {
  314. [alertController dismissViewControllerAnimated:YES completion:nil];
  315. }]];
  316. alertController.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItems.firstObject;
  317. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  318. [alertController.view layoutIfNeeded];
  319. [self presentViewController:alertController animated:YES completion:NULL];
  320. }
  321. #pragma --------------------------------------------------------------------------------------------
  322. #pragma mark ==== Download Thumbnail Delegate ====
  323. #pragma --------------------------------------------------------------------------------------------
  324. - (void)downloadThumbnailSuccess:(CCMetadataNet *)metadataNet
  325. {
  326. NSIndexPath *indexPath = [_sectionDataSource.fileIDIndexPath objectForKey:metadataNet.fileID];
  327. if ([self indexPathIsValid:indexPath]) {
  328. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadataNet.fileID]])
  329. [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
  330. }
  331. }
  332. - (void)triggerProgressTask:(NSNotification *)notification
  333. {
  334. NSDictionary *dict = notification.userInfo;
  335. float progress = [[dict valueForKey:@"progress"] floatValue];
  336. if (progress == 0)
  337. [self.navigationController cancelCCProgress];
  338. else
  339. [self.navigationController setCCProgressPercentage:progress*100 andTintColor:[NCBrandColor sharedInstance].navigationBarProgress];
  340. }
  341. #pragma --------------------------------------------------------------------------------------------
  342. #pragma mark ==== Collection ====
  343. #pragma --------------------------------------------------------------------------------------------
  344. - (void)reloadDatasourceForced
  345. {
  346. [CCSectionMetadata removeAllObjectsSectionDataSource:_sectionDataSource];
  347. _dateReadDataSource = nil;
  348. [self reloadDatasource];
  349. }
  350. - (void)reloadDatasource
  351. {
  352. // test
  353. if (app.activeAccount.length == 0)
  354. return;
  355. NSString *autoUploadPath = [[NCManageDatabase sharedInstance] getAccountAutoUploadPath:app.activeUrl];
  356. NSDate *dateDateRecordDirectory = nil;
  357. NSArray *directories = [[NCManageDatabase sharedInstance] getTablesDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl BEGINSWITH %@", app.activeAccount, autoUploadPath] sorted:@"dateReadDirectory" ascending:false];
  358. if ([directories count] > 0) {
  359. tableDirectory *directory = [directories objectAtIndex:0];
  360. dateDateRecordDirectory = directory.dateReadDirectory;
  361. }
  362. if ([dateDateRecordDirectory compare:_dateReadDataSource] == NSOrderedDescending || dateDateRecordDirectory == nil || _dateReadDataSource == nil) {
  363. NSLog(@"[LOG] Photos rebuild Data Source serverUrl : %@", autoUploadPath);
  364. _dateReadDataSource = [NSDate date];
  365. NSArray *results = [[NCManageDatabase sharedInstance] getTableMetadatasPhotosWithServerUrl:autoUploadPath];
  366. _sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:results listProgressMetadata:nil groupByField:@"date" activeAccount:app.activeAccount];
  367. [self reloadCollection];
  368. }
  369. }
  370. - (void)reloadCollection
  371. {
  372. [self.collectionView reloadData];
  373. [_selectedMetadatas removeAllObjects];
  374. [self collectionSelect:NO];
  375. }
  376. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  377. {
  378. return [[_sectionDataSource.sectionArrayRow allKeys] count];
  379. }
  380. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  381. {
  382. return [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:section]] count];
  383. }
  384. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  385. {
  386. UIInterfaceOrientation orientationOnLunch = [[UIApplication sharedApplication] statusBarOrientation];
  387. if (orientationOnLunch == UIInterfaceOrientationPortrait)
  388. return CGSizeMake(collectionView.frame.size.width / 5.1f, collectionView.frame.size.width / 5.1f);
  389. else
  390. return CGSizeMake(collectionView.frame.size.width / 7.1f, collectionView.frame.size.width / 7.1f);
  391. }
  392. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  393. {
  394. if ([_sectionDataSource.sections count] - 1 == section)
  395. return CGSizeMake(collectionView.frame.size.width, 50);
  396. return CGSizeZero;
  397. }
  398. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  399. {
  400. if (kind == UICollectionElementKindSectionHeader) {
  401. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
  402. //headerView.backgroundColor = COLOR_GROUPBY_BAR_NO_BLUR;
  403. [self getGeoLocationForSection:indexPath.section];
  404. UILabel *titleLabel = (UILabel *)[headerView viewWithTag:100];
  405. titleLabel.textColor = [UIColor blackColor];
  406. titleLabel.text = [CCUtility getTitleSectionDate:[_sectionDataSource.sections objectAtIndex:indexPath.section]];
  407. return headerView;
  408. }
  409. if (kind == UICollectionElementKindSectionFooter) {
  410. UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
  411. UILabel *titleLabel = (UILabel *)[footerView viewWithTag:100];
  412. titleLabel.textColor = [UIColor grayColor];
  413. titleLabel.text = [NSString stringWithFormat:@"%lu %@, %lu %@", (long)_sectionDataSource.image, NSLocalizedString(@"photo", nil), (long)_sectionDataSource.video, NSLocalizedString(@"_video_", nil)];
  414. return footerView;
  415. }
  416. return nil;
  417. }
  418. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  419. {
  420. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  421. UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
  422. UIVisualEffectView *effect = [cell viewWithTag:200];
  423. UIImageView *checked = [cell viewWithTag:300];
  424. checked.image = [UIImage imageNamed:@"checked"];
  425. NSArray *metadatasForKey = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]];
  426. if ([metadatasForKey count] > indexPath.row) {
  427. NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
  428. tableMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  429. // Image
  430. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]]) {
  431. // insert Image
  432. imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID]];
  433. } else {
  434. // Thumbnail not present
  435. imageView.image = [UIImage imageNamed:@"file_photo"];
  436. if (metadata.thumbnailExists)
  437. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  438. }
  439. // Cheched
  440. if (cell.selected) {
  441. checked.hidden = NO;
  442. effect.hidden = NO;
  443. effect.alpha = 0.4;
  444. } else {
  445. checked.hidden = YES;
  446. effect.hidden = YES;
  447. }
  448. }
  449. return cell;
  450. }
  451. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  452. {
  453. NSArray *metadatasForKey = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]];
  454. if ([metadatasForKey count] > indexPath.row) {
  455. NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
  456. _metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  457. if (_cellEditing) {
  458. [self cellSelect:YES indexPath:indexPath metadata:_metadata];
  459. } else {
  460. if ([self shouldPerformSegue])
  461. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  462. }
  463. }
  464. }
  465. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  466. {
  467. if (_cellEditing == NO)
  468. return;
  469. NSArray *metadatasForKey = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]];
  470. if ([metadatasForKey count] > indexPath.row) {
  471. NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
  472. _metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  473. [self cellSelect:NO indexPath:indexPath metadata:_metadata];
  474. }
  475. }
  476. - (BOOL)indexPathIsValid:(NSIndexPath *)indexPath
  477. {
  478. if (!indexPath)
  479. return NO;
  480. NSInteger section = indexPath.section;
  481. NSInteger row = indexPath.row;
  482. NSInteger lastSectionIndex = [self numberOfSectionsInCollectionView:self.collectionView] - 1;
  483. if (section > lastSectionIndex || lastSectionIndex < 0)
  484. return NO;
  485. NSInteger rowCount = [self.collectionView numberOfItemsInSection:indexPath.section] - 1;
  486. if (rowCount < 0)
  487. return NO;
  488. return row <= rowCount;
  489. }
  490. #pragma --------------------------------------------------------------------------------------------
  491. #pragma mark ===== Navigation ====
  492. #pragma --------------------------------------------------------------------------------------------
  493. - (BOOL)shouldPerformSegue
  494. {
  495. // Test
  496. // Background ? exit
  497. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground)
  498. return NO;
  499. // Not in first plain ? exit
  500. if (self.view.window == NO)
  501. return NO;
  502. // Collapsed but in first plain in detail exit
  503. if (self.splitViewController.isCollapsed)
  504. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window)
  505. return NO;
  506. // ok perform segue
  507. return YES;
  508. }
  509. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  510. {
  511. id controller = segue.destinationViewController;
  512. if ([controller isKindOfClass:[UINavigationController class]]) {
  513. UINavigationController *navigationController = controller;
  514. self.detailViewController = (CCDetail *)navigationController.topViewController;
  515. } else {
  516. self.detailViewController = segue.destinationViewController;
  517. }
  518. NSMutableArray *allRecordsDataSourceImagesVideos = [[NSMutableArray alloc] init];
  519. for (NSString *fileID in _sectionDataSource.allEtag) {
  520. tableMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
  521. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
  522. [allRecordsDataSourceImagesVideos addObject:metadata];
  523. }
  524. self.detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  525. self.detailViewController.metadataDetail = _metadata;
  526. self.detailViewController.dateFilterQuery = _metadata.date;
  527. [self.detailViewController setTitle:_metadata.fileName];
  528. }
  529. @end