CCPhotos.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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 () <CCActionsDownloadThumbnailDelegate>
  29. {
  30. AppDelegate *appDelegate;
  31. tableMetadata *metadata;
  32. NSMutableArray *selectedMetadatas;
  33. CCSectionDataSourceMetadata *sectionDataSource;
  34. BOOL isSearchMode;
  35. BOOL isEditMode;
  36. TOScrollBar *scrollBar;
  37. NSMutableDictionary *saveEtagForStartDirectory;
  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. saveEtagForStartDirectory = [NSMutableDictionary new];
  69. selectedMetadatas = [NSMutableArray new];
  70. self.fileIDHide = [NSMutableArray new];
  71. self.addMetadatasFromUpload = [NSMutableArray new];
  72. // empty Data Source
  73. self.collectionView.emptyDataSetDelegate = self;
  74. self.collectionView.emptyDataSetSource = self;
  75. // scroll bar
  76. scrollBar = [TOScrollBar new];
  77. [self.collectionView to_addScrollBar:scrollBar];
  78. scrollBar.handleTintColor = [NCBrandColor sharedInstance].brand;
  79. scrollBar.handleWidth = 20;
  80. scrollBar.handleMinimiumHeight = 20;
  81. scrollBar.trackWidth = 0;
  82. scrollBar.edgeInset = 12;
  83. }
  84. // Apparirà
  85. - (void)viewWillAppear:(BOOL)animated
  86. {
  87. [super viewWillAppear:animated];
  88. // Color
  89. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  90. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  91. // Plus Button
  92. [appDelegate plusButtonVisibile:true];
  93. [self reloadDatasource];
  94. }
  95. - (void)viewSafeAreaInsetsDidChange
  96. {
  97. [super viewSafeAreaInsetsDidChange];
  98. self.collectionView.contentInset = self.view.safeAreaInsets;
  99. }
  100. - (void)changeTheming
  101. {
  102. if (self.isViewLoaded && self.view.window)
  103. [appDelegate changeTheming:self];
  104. scrollBar.handleTintColor = [NCBrandColor sharedInstance].brand;
  105. [self.collectionView reloadData];
  106. }
  107. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  108. {
  109. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  110. // Before rotation
  111. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  112. 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))) {
  113. // Portrait
  114. } else {
  115. // Landscape
  116. }
  117. }];
  118. }
  119. #pragma --------------------------------------------------------------------------------------------
  120. #pragma mark ===== Gestione Grafica Window =====
  121. #pragma --------------------------------------------------------------------------------------------
  122. - (void)setUINavigationBarDefault
  123. {
  124. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  125. // curront folder search
  126. NSString *directory = [[NCManageDatabase sharedInstance] getAccountStartDirectoryPhotosTab:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]];
  127. NSString *home = [CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl];
  128. NSString *folder = @"";
  129. if (home.length > 0) {
  130. folder = [directory stringByReplacingOccurrencesOfString:home withString:@""];
  131. }
  132. // Title
  133. self.navigationItem.titleView = nil;
  134. if (folder.length == 0) {
  135. self.navigationItem.title = NSLocalizedString(@"_photo_camera_", nil);
  136. } else {
  137. self.navigationItem.title = [NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"_photo_camera_", nil), [folder substringFromIndex:1]];
  138. }
  139. if (isSearchMode) {
  140. [CCGraphics addImageToTitle:self.navigationItem.title colorTitle:[NCBrandColor sharedInstance].brandText imageTitle:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"load"] multiplier:2 color:[NCBrandColor sharedInstance].brandText] navigationItem:self.navigationItem];
  141. }
  142. // Button Item
  143. UIImage *icon;
  144. icon = [UIImage imageNamed:@"select"];
  145. UIBarButtonItem *buttonSelect = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStylePlain target:self action:@selector(editingModeYES)];
  146. icon = [UIImage imageNamed:@"folderPhotos"];
  147. UIBarButtonItem *buttonStartDirectoryPhotosTab = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStylePlain target:self action:@selector(selectStartDirectoryPhotosTab)];
  148. if ([sectionDataSource.allRecordsDataSource count] > 0) {
  149. self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:buttonSelect, nil];
  150. } else {
  151. self.navigationItem.rightBarButtonItems = nil;
  152. }
  153. self.navigationItem.leftBarButtonItems = [[NSArray alloc] initWithObjects:buttonStartDirectoryPhotosTab, nil];
  154. }
  155. - (void)setUINavigationBarSelected
  156. {
  157. UIImage *icon;
  158. icon = [UIImage imageNamed:@"delete"];
  159. UIBarButtonItem *buttonDelete = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStylePlain target:self action:@selector(deleteSelectedFiles)];
  160. icon = [UIImage imageNamed:@"openFile"];
  161. UIBarButtonItem *buttonOpenWith = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStylePlain target:self action:@selector(openSelectedFiles)];
  162. UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(editingModeNO)];
  163. self.navigationItem.leftBarButtonItem = leftButton;
  164. self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:buttonDelete, buttonOpenWith, nil];
  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)cellSelect:(BOOL)select indexPath:(NSIndexPath *)indexPath metadata:(tableMetadata *)metadata
  169. {
  170. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
  171. UIVisualEffectView *effect = [cell viewWithTag:200];
  172. UIImageView *checked = [cell viewWithTag:300];
  173. if (select) {
  174. effect.hidden = NO;
  175. effect.alpha = 0.4;
  176. checked.hidden = NO;
  177. [selectedMetadatas addObject:metadata];
  178. } else {
  179. effect.hidden = YES;
  180. checked.hidden = YES;
  181. [selectedMetadatas removeObject:metadata];
  182. }
  183. // Title
  184. self.navigationItem.title = [NSString stringWithFormat:@"%@ : %lu / %lu", NSLocalizedString(@"_selected_", nil), (unsigned long)[selectedMetadatas count], (unsigned long)[sectionDataSource.allRecordsDataSource count]];
  185. }
  186. - (void)scrollToTop
  187. {
  188. [self.collectionView setContentOffset:CGPointMake(0, - self.collectionView.contentInset.top) animated:NO];
  189. }
  190. - (void)getGeoLocationForSection:(NSInteger)section
  191. {
  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"] multiplier:2 color:[NCBrandColor sharedInstance].graySoft];
  212. }
  213. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  214. {
  215. NSString *text;
  216. if (isSearchMode) {
  217. text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_search_in_progress_", nil)];
  218. } else {
  219. text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_photo_view_", nil)];
  220. }
  221. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  222. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  223. }
  224. #pragma --------------------------------------------------------------------------------------------
  225. #pragma mark ===== openSelectedFiles =====
  226. #pragma--------------------------------------------------------------------------------------------
  227. - (void)openSelectedFiles
  228. {
  229. NSMutableArray *dataToShare = [[NSMutableArray alloc] init];
  230. for (tableMetadata *metadata in selectedMetadatas) {
  231. NSString *fileNamePath = [CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileName:metadata.fileNameView];
  232. if ([CCUtility fileProviderStorageExists:metadata.fileID fileName:metadata.fileNameView]) {
  233. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image]) {
  234. NSData *data = [NSData dataWithData:UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:fileNamePath], 0.9)];
  235. [dataToShare addObject:data];
  236. }
  237. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video]) {
  238. [dataToShare addObject:[NSURL fileURLWithPath:fileNamePath]];
  239. }
  240. }
  241. }
  242. if ([dataToShare count] > 0) {
  243. UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
  244. // iPad
  245. activityViewController.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItems.lastObject;
  246. self.navigationItem.leftBarButtonItem.enabled = NO;
  247. self.navigationItem.rightBarButtonItem.enabled = NO;
  248. [self presentViewController:activityViewController animated:YES completion:^{
  249. [activityViewController setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
  250. self.navigationItem.leftBarButtonItem.enabled = YES;
  251. self.navigationItem.rightBarButtonItem.enabled = YES;
  252. [self editingModeNO];
  253. if (completed) {
  254. [self.collectionView reloadData];
  255. }
  256. }];
  257. }];
  258. }
  259. }
  260. #pragma --------------------------------------------------------------------------------------------
  261. #pragma mark ===== Download =====
  262. #pragma--------------------------------------------------------------------------------------------
  263. - (void)downloadFileSuccessFailure:(NSString *)fileName fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector errorMessage:(NSString *)errorMessage errorCode:(NSInteger)errorCode
  264. {
  265. if (errorCode == 0) {
  266. NSIndexPath *indexPath;
  267. BOOL existsIcon = NO;
  268. if (fileID) {
  269. existsIcon = [[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:fileID fileNameView:fileName]];
  270. indexPath = [sectionDataSource.fileIDIndexPath objectForKey:fileID];
  271. }
  272. if ([self indexPathIsValid:indexPath] && existsIcon) {
  273. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
  274. if (cell) {
  275. UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
  276. UIVisualEffectView *effect = [cell viewWithTag:200];
  277. UIImageView *checked = [cell viewWithTag:300];
  278. imageView.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:fileID fileNameView:fileName]];
  279. effect.hidden = YES;
  280. checked.hidden = YES;
  281. }
  282. }
  283. } else {
  284. [appDelegate messageNotification:@"_download_selected_files_" description:@"_error_download_photobrowser_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  285. }
  286. }
  287. #pragma --------------------------------------------------------------------------------------------
  288. #pragma mark ===== Delete =====
  289. #pragma--------------------------------------------------------------------------------------------
  290. - (void)deleteFile
  291. {
  292. NSInteger numDelete = selectedMetadatas.count;
  293. __block NSInteger cont = 0;
  294. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  295. for (tableMetadata *metadata in selectedMetadatas) {
  296. [self.fileIDHide addObject:metadata.fileID];
  297. [ocNetworking deleteFileOrFolder:metadata.fileName serverUrl:[[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID] success:^{
  298. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID] clearDateReadDirectoryID:metadata.directoryID];
  299. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  300. [[NCManageDatabase sharedInstance] deletePhotosWithFileID:metadata.fileID];
  301. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID] error:nil];
  302. if (++cont == numDelete) {
  303. [self reloadDatasource];
  304. }
  305. } failure:^(NSString *message, NSInteger errorCode) {
  306. [self.fileIDHide removeObject:metadata.fileID];
  307. if (++cont == numDelete) {
  308. [self reloadDatasource];
  309. }
  310. }];
  311. }
  312. [self editingModeNO];
  313. }
  314. - (void)deleteSelectedFiles
  315. {
  316. if ([selectedMetadatas count] == 0)
  317. return;
  318. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  319. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  320. style:UIAlertActionStyleDestructive
  321. handler:^(UIAlertAction *action) {
  322. [self deleteFile];
  323. }]];
  324. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  325. style:UIAlertActionStyleCancel
  326. handler:^(UIAlertAction *action) {
  327. [alertController dismissViewControllerAnimated:YES completion:nil];
  328. }]];
  329. alertController.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItems.firstObject;
  330. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  331. [alertController.view layoutIfNeeded];
  332. [self presentViewController:alertController animated:YES completion:NULL];
  333. }
  334. #pragma --------------------------------------------------------------------------------------------
  335. #pragma mark ==== Download Thumbnail Delegate ====
  336. #pragma --------------------------------------------------------------------------------------------
  337. - (void)downloadThumbnailSuccessFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  338. {
  339. // Check Active Account
  340. if (![metadataNet.account isEqualToString:appDelegate.activeAccount])
  341. return;
  342. if (errorCode == 0) {
  343. NSIndexPath *indexPath = [sectionDataSource.fileIDIndexPath objectForKey:metadataNet.fileID];
  344. if ([self indexPathIsValid:indexPath]) {
  345. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadataNet.fileID fileNameView:metadataNet.fileNameView]])
  346. [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
  347. }
  348. }
  349. }
  350. - (void)triggerProgressTask:(NSNotification *)notification
  351. {
  352. //NSDictionary *dict = notification.userInfo;
  353. //float progress = [[dict valueForKey:@"progress"] floatValue];
  354. }
  355. #pragma --------------------------------------------------------------------------------------------
  356. #pragma mark ==== Change Start directory ====
  357. #pragma --------------------------------------------------------------------------------------------
  358. - (void)moveServerUrlTo:(NSString *)serverUrlTo title:(NSString *)title
  359. {
  360. NSString *oldStartDirectoryPhotosTab = [[NCManageDatabase sharedInstance] getAccountStartDirectoryPhotosTab:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]];
  361. if (![serverUrlTo isEqualToString:oldStartDirectoryPhotosTab]) {
  362. // Save Start Directory
  363. [[NCManageDatabase sharedInstance] setAccountStartDirectoryPhotosTab:serverUrlTo];
  364. // search PhotoVideo with new start directory
  365. [self searchPhotoVideo];
  366. }
  367. }
  368. - (void)selectStartDirectoryPhotosTab
  369. {
  370. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"CCMove" bundle:nil] instantiateViewControllerWithIdentifier:@"CCMove"];
  371. CCMove *viewController = (CCMove *)navigationController.topViewController;
  372. viewController.delegate = self;
  373. viewController.move.title = NSLocalizedString(@"_select_dir_photos_tab_", nil);
  374. viewController.tintColor = [NCBrandColor sharedInstance].brandText;
  375. viewController.barTintColor = [NCBrandColor sharedInstance].brand;
  376. viewController.tintColorTitle = [NCBrandColor sharedInstance].brandText;
  377. viewController.networkingOperationQueue = appDelegate.netQueue;
  378. viewController.hideCreateFolder = YES;
  379. // E2EE
  380. viewController.includeDirectoryE2EEncryption = NO;
  381. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  382. [self presentViewController:navigationController animated:YES completion:nil];
  383. }
  384. #pragma --------------------------------------------------------------------------------------------
  385. #pragma mark ==== Search Photo/Video ====
  386. #pragma --------------------------------------------------------------------------------------------
  387. - (void)searchSuccessFailure:(CCMetadataNet *)metadataNet metadatas:(NSArray *)metadatas message:(NSString *)message errorCode:(NSInteger)errorCode
  388. {
  389. isSearchMode = NO;
  390. if (![metadataNet.account isEqualToString:appDelegate.activeAccount] || errorCode != 0) {
  391. [self reloadDatasource];
  392. } else {
  393. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  394. [[NCManageDatabase sharedInstance] createTablePhotos:metadatas];
  395. dispatch_async(dispatch_get_main_queue(), ^{
  396. [self reloadDatasource];
  397. });
  398. // Update date
  399. [[NCManageDatabase sharedInstance] setAccountDateSearchContentTypeImageVideo:[NSDate date]];
  400. // Save etag
  401. [saveEtagForStartDirectory setObject:metadataNet.etag forKey:metadataNet.serverUrl];
  402. });
  403. }
  404. }
  405. - (void)searchPhotoVideo
  406. {
  407. // test
  408. if (appDelegate.activeAccount.length == 0 || isSearchMode)
  409. return;
  410. // WAITING FOR d:creationdate
  411. //
  412. // tableAccount *account = [[NCManageDatabase sharedInstance] getAccountActive];
  413. // account.dateSearchContentTypeImageVideo
  414. NSString *startDirectory = [[NCManageDatabase sharedInstance] getAccountStartDirectoryPhotosTab:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]];
  415. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:self metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  416. [ocNetworking readFile:nil serverUrl:startDirectory account:appDelegate.activeAccount success:^(tableMetadata *metadata) {
  417. if (![metadata.etag isEqualToString:[saveEtagForStartDirectory objectForKey:startDirectory]] || sectionDataSource.allRecordsDataSource.count == 0) {
  418. isSearchMode = YES;
  419. [self editingModeNO];
  420. // Clear all Hardcoded
  421. [self.fileIDHide removeAllObjects];
  422. [self.addMetadatasFromUpload removeAllObjects];
  423. [[CCActions sharedInstance] search:startDirectory fileName:@"" etag:metadata.etag depth:@"infinity" date:[NSDate distantPast] contenType:@[@"image/%", @"video/%"] selector:selectorSearchContentType delegate:self];
  424. } else {
  425. [self reloadDatasource];
  426. }
  427. } failure:^(NSString *message, NSInteger errorCode) {
  428. }];
  429. }
  430. #pragma --------------------------------------------------------------------------------------------
  431. #pragma mark ==== Datasource ====
  432. #pragma --------------------------------------------------------------------------------------------
  433. - (void)reloadDatasource
  434. {
  435. // test
  436. if (appDelegate.activeAccount.length == 0) {
  437. return;
  438. }
  439. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  440. NSArray *metadatas = [[NCManageDatabase sharedInstance] getTablePhotosWithAddMetadatasFromUpload:self.addMetadatasFromUpload];
  441. sectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:metadatas listProgressMetadata:nil groupByField:@"date" fileIDHide:self.fileIDHide activeAccount:appDelegate.activeAccount];
  442. dispatch_async(dispatch_get_main_queue(), ^{
  443. if (isEditMode)
  444. [self setUINavigationBarSelected];
  445. else
  446. [self setUINavigationBarDefault];
  447. [self.collectionView reloadData];
  448. });
  449. });
  450. }
  451. - (void)editingModeYES
  452. {
  453. [self.collectionView setAllowsMultipleSelection:true];
  454. isEditMode = true;
  455. [selectedMetadatas removeAllObjects];
  456. [self setUINavigationBarSelected];
  457. [self.collectionView reloadData];
  458. }
  459. - (void)editingModeNO
  460. {
  461. [self.collectionView setAllowsMultipleSelection:false];
  462. isEditMode = false;
  463. [selectedMetadatas removeAllObjects];
  464. [self setUINavigationBarDefault];
  465. [self.collectionView reloadData];
  466. }
  467. #pragma --------------------------------------------------------------------------------------------
  468. #pragma mark ==== Collection ====
  469. #pragma --------------------------------------------------------------------------------------------
  470. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  471. {
  472. return [[sectionDataSource.sectionArrayRow allKeys] count];
  473. }
  474. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  475. {
  476. return [[sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:section]] count];
  477. }
  478. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  479. {
  480. UIInterfaceOrientation orientationOnLunch = [[UIApplication sharedApplication] statusBarOrientation];
  481. if (orientationOnLunch == UIInterfaceOrientationPortrait)
  482. return CGSizeMake(collectionView.frame.size.width / 5.1f, collectionView.frame.size.width / 5.1f);
  483. else
  484. return CGSizeMake(collectionView.frame.size.width / 7.1f, collectionView.frame.size.width / 7.1f);
  485. }
  486. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  487. {
  488. if ([sectionDataSource.sections count] - 1 == section)
  489. return CGSizeMake(collectionView.frame.size.width, 50);
  490. return CGSizeZero;
  491. }
  492. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  493. {
  494. if (kind == UICollectionElementKindSectionHeader) {
  495. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
  496. //headerView.backgroundColor = COLOR_GROUPBY_BAR_NO_BLUR;
  497. [self getGeoLocationForSection:indexPath.section];
  498. UILabel *titleLabel = (UILabel *)[headerView viewWithTag:100];
  499. titleLabel.textColor = [UIColor blackColor];
  500. if (sectionDataSource.sections.count > indexPath.section)
  501. titleLabel.text = [CCUtility getTitleSectionDate:[sectionDataSource.sections objectAtIndex:indexPath.section]];
  502. return headerView;
  503. }
  504. if (kind == UICollectionElementKindSectionFooter) {
  505. UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
  506. UILabel *titleLabel = (UILabel *)[footerView viewWithTag:100];
  507. titleLabel.textColor = [UIColor grayColor];
  508. titleLabel.text = [NSString stringWithFormat:@"%lu %@, %lu %@", (long)sectionDataSource.image, NSLocalizedString(@"photo", nil), (long)sectionDataSource.video, NSLocalizedString(@"_video_", nil)];
  509. return footerView;
  510. }
  511. return nil;
  512. }
  513. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  514. {
  515. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  516. UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
  517. UIVisualEffectView *effect = [cell viewWithTag:200];
  518. UIImageView *checked = [cell viewWithTag:300];
  519. checked.image = [UIImage imageNamed:@"checked"];
  520. NSArray *metadatasForKey = [sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:indexPath.section]];
  521. if ([metadatasForKey count] > indexPath.row) {
  522. NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
  523. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:fileID];
  524. // Image
  525. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]]) {
  526. // insert Image
  527. imageView.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  528. } else {
  529. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"directoryID == %@", metadata.directoryID]];
  530. // Thumbnail not present
  531. if (directory.e2eEncrypted) {
  532. imageView.image = [UIImage imageNamed:@"file_photo_encrypted"];
  533. } else {
  534. imageView.image = [UIImage imageNamed:@"file_photo"];
  535. if (metadata.thumbnailExists)
  536. [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
  537. }
  538. }
  539. // Cheched
  540. if (cell.selected) {
  541. checked.hidden = NO;
  542. effect.hidden = NO;
  543. effect.alpha = 0.4;
  544. } else {
  545. checked.hidden = YES;
  546. effect.hidden = YES;
  547. }
  548. }
  549. return cell;
  550. }
  551. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  552. {
  553. NSArray *metadatasForKey = [sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:indexPath.section]];
  554. if ([metadatasForKey count] > indexPath.row) {
  555. NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
  556. metadata = [sectionDataSource.allRecordsDataSource objectForKey:fileID];
  557. if (isEditMode) {
  558. [self cellSelect:YES indexPath:indexPath metadata:metadata];
  559. } else {
  560. if ([self shouldPerformSegue])
  561. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  562. }
  563. }
  564. }
  565. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  566. {
  567. // test
  568. if (isEditMode == NO)
  569. return;
  570. NSArray *metadatasForKey = [sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:indexPath.section]];
  571. if ([metadatasForKey count] > indexPath.row) {
  572. NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
  573. metadata = [sectionDataSource.allRecordsDataSource objectForKey:fileID];
  574. [self cellSelect:NO indexPath:indexPath metadata:metadata];
  575. }
  576. }
  577. - (BOOL)indexPathIsValid:(NSIndexPath *)indexPath
  578. {
  579. if (!indexPath)
  580. return NO;
  581. NSInteger section = indexPath.section;
  582. NSInteger row = indexPath.row;
  583. NSInteger lastSectionIndex = [self numberOfSectionsInCollectionView:self.collectionView] - 1;
  584. if (section > lastSectionIndex || lastSectionIndex < 0)
  585. return NO;
  586. NSInteger rowCount = [self.collectionView numberOfItemsInSection:indexPath.section] - 1;
  587. if (rowCount < 0)
  588. return NO;
  589. return row <= rowCount;
  590. }
  591. #pragma --------------------------------------------------------------------------------------------
  592. #pragma mark ===== Navigation ====
  593. #pragma --------------------------------------------------------------------------------------------
  594. - (BOOL)shouldPerformSegue
  595. {
  596. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground)
  597. return NO;
  598. // Not in first plain ? exit
  599. if (self.view.window == NO)
  600. return NO;
  601. // Collapsed but in first plain in detail exit
  602. if (self.splitViewController.isCollapsed)
  603. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window)
  604. return NO;
  605. // ok perform segue
  606. return YES;
  607. }
  608. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  609. {
  610. id controller = segue.destinationViewController;
  611. if ([controller isKindOfClass:[UINavigationController class]]) {
  612. UINavigationController *navigationController = controller;
  613. self.detailViewController = (CCDetail *)navigationController.topViewController;
  614. } else {
  615. self.detailViewController = segue.destinationViewController;
  616. }
  617. NSMutableArray *allRecordsDataSourceImagesVideos = [NSMutableArray new];
  618. for (NSString *fileID in sectionDataSource.allEtag) {
  619. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:fileID];
  620. [allRecordsDataSourceImagesVideos addObject:metadata];
  621. }
  622. self.detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
  623. self.detailViewController.metadataDetail = metadata;
  624. self.detailViewController.dateFilterQuery = metadata.date;
  625. [self.detailViewController setTitle:metadata.fileName];
  626. }
  627. @end