CCPhotos.m 29 KB

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