CCPhotos.m 33 KB

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