CCPhotos.m 30 KB

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