CCPhotos.m 33 KB

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