CCPhotos.m 30 KB

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