CCPhotos.m 29 KB

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