CCPhotos.m 36 KB

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