123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936 |
- //
- // CCPhotos.m
- // Nextcloud iOS
- //
- // Created by Marino Faggiana on 29/07/15.
- // Copyright (c) 2017 TWS. All rights reserved.
- //
- // Author Marino Faggiana <m.faggiana@twsweb.it>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- #import "CCPhotos.h"
- #import "AppDelegate.h"
- #import "CCManageAutoUpload.h"
- #import "TOScrollBar.h"
- #import "NCBridgeSwift.h"
- @interface CCPhotos () <CCActionsDeleteDelegate, CCActionsDownloadThumbnailDelegate>
- {
- AppDelegate *appDelegate;
- tableMetadata *_metadata;
- NSMutableArray *_queueMetadatas;
- NSMutableArray *_selectedMetadatas;
- NSUInteger _numSelectedMetadatas;
-
- CCSectionDataSourceMetadata *_sectionDataSource;
-
- CCHud *_hud;
-
- TOScrollBar *_scrollBar;
- NSMutableDictionary *_saveEtagForStartDirectory;
- }
- @end
- @implementation CCPhotos
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== Init =====
- #pragma --------------------------------------------------------------------------------------------
- - (id)initWithCoder:(NSCoder *)aDecoder
- {
- if (self = [super initWithCoder:aDecoder]) {
-
- appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
-
- appDelegate.activePhotos = self;
- }
-
- return self;
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
-
- if (self) {
-
- appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
- }
-
- return self;
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== View =====
- #pragma --------------------------------------------------------------------------------------------
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- _queueMetadatas = [NSMutableArray new];
- _selectedMetadatas = [NSMutableArray new];
- _saveEtagForStartDirectory = [NSMutableDictionary new];
- _hud = [[CCHud alloc] initWithView:[[[UIApplication sharedApplication] delegate] window]];
-
- // empty Data Source
- self.collectionView.emptyDataSetDelegate = self;
- self.collectionView.emptyDataSetSource = self;
- // scroll bar
- _scrollBar = [TOScrollBar new];
- [self.collectionView to_addScrollBar:_scrollBar];
-
- _scrollBar.handleTintColor = [NCBrandColor sharedInstance].brand;
- _scrollBar.handleWidth = 20;
- _scrollBar.handleMinimiumHeight = 20;
- _scrollBar.trackWidth = 0;
- _scrollBar.edgeInset = 12;
- }
- // Apparirà
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
-
- // Color
- [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
- [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
-
- // Plus Button
- [appDelegate plusButtonVisibile:true];
- if(!_isSearchMode && !_isEditMode)
- [self reloadDatasourceFromSearch:NO];
- }
- - (void)viewSafeAreaInsetsDidChange
- {
- [super viewSafeAreaInsetsDidChange];
-
- self.collectionView.contentInset = self.view.safeAreaInsets;
- }
- - (void)changeTheming
- {
- if (self.isViewLoaded && self.view.window)
- [appDelegate changeTheming:self];
-
- _scrollBar.handleTintColor = [NCBrandColor sharedInstance].brand;
-
- if(!_isSearchMode && !_isEditMode)
- [self.collectionView reloadData];
- }
- - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
- {
- [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
-
- // Before rotation
-
- [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
-
- 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))) {
-
- // Portrait
-
- } else {
-
- // Landscape
- }
-
- }];
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== Gestione Grafica Window =====
- #pragma --------------------------------------------------------------------------------------------
- - (void)setUINavigationBarDefault
- {
- [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
-
- // curront folder search
- NSString *directory = [[NCManageDatabase sharedInstance] getAccountStartDirectoryPhotosTab:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]];
- NSString *folder = [directory stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl] withString:@""];
-
- // Title
- self.navigationItem.titleView = nil;
- if (folder.length == 0) {
- self.navigationItem.title = NSLocalizedString(@"_photo_camera_", nil);
- } else {
- self.navigationItem.title = [NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"_photo_camera_", nil), [folder substringFromIndex:1]];
- }
-
- if (_isSearchMode) {
- [CCGraphics addImageToTitle:self.navigationItem.title colorTitle:[NCBrandColor sharedInstance].brandText imageTitle:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"loadingTitle"] color:[NCBrandColor sharedInstance].brandText] navigationItem:self.navigationItem];
- [self.collectionView reloadData];
- return;
- }
-
- // Button Item
- UIImage *icon;
- icon = [UIImage imageNamed:@"seleziona"];
- UIBarButtonItem *buttonSelect = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStylePlain target:self action:@selector(editingModeYES)];
- icon = [UIImage imageNamed:@"startDirectoryPhotosTab"];
- UIBarButtonItem *buttonStartDirectoryPhotosTab = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStylePlain target:self action:@selector(selectStartDirectoryPhotosTab)];
- if ([_sectionDataSource.allRecordsDataSource count] > 0) {
- self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:buttonSelect, nil];
- } else {
- self.navigationItem.rightBarButtonItems = nil;
- }
- self.navigationItem.leftBarButtonItems = [[NSArray alloc] initWithObjects:buttonStartDirectoryPhotosTab, nil];
-
- [self.collectionView reloadData];
- }
- - (void)setUINavigationBarSelected
- {
- UIImage *icon;
-
- icon = [UIImage imageNamed:@"deleteSelectedFiles"];
- UIBarButtonItem *buttonDelete = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStylePlain target:self action:@selector(deleteSelectedFiles)];
-
- icon = [UIImage imageNamed:@"openSelectedFiles"];
- UIBarButtonItem *buttonOpenWith = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStylePlain target:self action:@selector(openSelectedFiles)];
-
- UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(editingModeNO)];
-
- self.navigationItem.leftBarButtonItem = leftButton;
- self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:buttonDelete, buttonOpenWith, nil];
-
- // Title
- self.navigationItem.title = [NSString stringWithFormat:@"%@ : %lu / %lu", NSLocalizedString(@"_selected_", nil), (unsigned long)[_selectedMetadatas count], (unsigned long)[_sectionDataSource.allRecordsDataSource count]];
-
- [self.collectionView reloadData];
- }
- - (void)cellSelect:(BOOL)select indexPath:(NSIndexPath *)indexPath metadata:(tableMetadata *)metadata
- {
- UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
- UIVisualEffectView *effect = [cell viewWithTag:200];
- UIImageView *checked = [cell viewWithTag:300];
-
- if (select) {
- effect.hidden = NO;
- effect.alpha = 0.4;
- checked.hidden = NO;
- [_selectedMetadatas addObject:metadata];
-
- } else {
- effect.hidden = YES;
- checked.hidden = YES;
- [_selectedMetadatas removeObject:metadata];
- }
-
- // Title
- self.navigationItem.title = [NSString stringWithFormat:@"%@ : %lu / %lu", NSLocalizedString(@"_selected_", nil), (unsigned long)[_selectedMetadatas count], (unsigned long)[_sectionDataSource.allRecordsDataSource count]];
- }
- - (void)scrollToTop
- {
- [self.collectionView setContentOffset:CGPointMake(0, - self.collectionView.contentInset.top) animated:NO];
- }
- - (void)getGeoLocationForSection:(NSInteger)section
- {
- NSString *addLocation = @"";
-
- NSArray *fileIDsForKey = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:section]];
-
- for (NSString *fileID in fileIDsForKey) {
-
- tableLocalFile *localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", fileID]];
-
- if ([localFile.exifLatitude floatValue] > 0 || [localFile.exifLongitude floatValue] > 0) {
-
- NSString *location = [[NCManageDatabase sharedInstance] getLocationFromGeoLatitude:localFile.exifLatitude longitude:localFile.exifLongitude];
-
- addLocation = [NSString stringWithFormat:@"%@, %@", addLocation, location];
-
- }
- }
- }
- - (void)searchInProgress:(BOOL)search
- {
- if (search) {
- _isSearchMode = YES;
- [self.navigationItem.leftBarButtonItems[0] setEnabled:NO];
- [self.navigationItem.rightBarButtonItems[0] setEnabled:NO];
- } else {
- _isSearchMode = NO;
- [self.navigationItem.leftBarButtonItems[0] setEnabled:YES];
- [self.navigationItem.rightBarButtonItems[0] setEnabled:YES];
- }
- [self setUINavigationBarDefault];
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ==== DZNEmptyDataSetSource Methods ====
- #pragma --------------------------------------------------------------------------------------------
- - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
- {
- return [NCBrandColor sharedInstance].backgroundView;
- }
- - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
- {
- return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"photosNoRecord"] color:[NCBrandColor sharedInstance].brandElement];
- }
- - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
- {
- NSString *text;
-
- if (_isSearchMode) {
- text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_search_in_progress_", nil)];
- } else {
- text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_photo_view_", nil)];
- }
-
- NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
-
- return [[NSAttributedString alloc] initWithString:text attributes:attributes];
- }
- /*
- - (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
- {
- NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
- paragraph.lineBreakMode = NSLineBreakByWordWrapping;
- paragraph.alignment = NSTextAlignmentCenter;
- NSString *text;
- tableAccount *account = [[NCManageDatabase sharedInstance] getAccountActive];
-
- if (account.autoUpload)
- text = [NSString stringWithFormat:@"%@", @"\n\n\n\n"];
- else
- text = [NSString stringWithFormat:@"\n%@\n", NSLocalizedString(@"_tutorial_autoupload_view_", nil)];
-
- NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
- return [[NSAttributedString alloc] initWithString:text attributes:attributes];
- }
- - (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
- {
- tableAccount *account = [[NCManageDatabase sharedInstance] getAccountActive];
-
- if (!account.autoUpload) {
-
- UIImage *buttonImage = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"button1000x200"] color:[NCBrandColor sharedInstance].brandElement];
-
- return [CCGraphics drawText:NSLocalizedString(@"_activate_autoupload_", nil) inImage:buttonImage colorText:[UIColor whiteColor] sizeOfFont:26];
-
- } else return nil;
- }
- - (void)emptyDataSetDidTapButton:(UIScrollView *)scrollView
- {
- CCManageAutoUpload *viewController = [[CCManageAutoUpload alloc] initWithNibName:nil bundle:nil];
- UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
-
- [navigationController setModalPresentationStyle:UIModalPresentationFullScreen];
- [self presentViewController:navigationController animated:YES completion:nil];
- }
- */
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== openSelectedFiles =====
- #pragma--------------------------------------------------------------------------------------------
- - (void)openSelectedFiles
- {
- NSMutableArray *dataToShare = [[NSMutableArray alloc] init];
-
- for (tableMetadata *metadata in _selectedMetadatas) {
-
- NSString *fileNamePath = [NSTemporaryDirectory() stringByAppendingString:metadata.fileName];
-
- [[NSFileManager defaultManager] linkItemAtPath:[NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, metadata.fileID] toPath:fileNamePath error:nil];
-
- if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePath]) {
-
- if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image]) {
-
- NSData *data = [NSData dataWithData:UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:fileNamePath], 0.9)];
- [dataToShare addObject:data];
- }
-
- if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video]) {
-
- [dataToShare addObject:[NSURL fileURLWithPath:fileNamePath]];
- }
- }
- }
-
- if ([dataToShare count] > 0) {
-
- UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
-
- // iPad
- activityViewController.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItems.lastObject;
-
- self.navigationItem.leftBarButtonItem.enabled = NO;
- self.navigationItem.rightBarButtonItem.enabled = NO;
-
- [self presentViewController:activityViewController animated:YES completion:^{
-
- [activityViewController setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
-
- self.navigationItem.leftBarButtonItem.enabled = YES;
- self.navigationItem.rightBarButtonItem.enabled = YES;
-
- if (completed) {
- [self.collectionView reloadData];
- }
- }];
- }];
- }
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== Download =====
- #pragma--------------------------------------------------------------------------------------------
- - (void)downloadFileSuccessFailure:(NSString *)fileName fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost errorMessage:(NSString *)errorMessage errorCode:(NSInteger)errorCode
- {
- if (errorCode == 0) {
-
- NSIndexPath *indexPath;
- BOOL existsIcon = NO;
-
- if (fileID) {
- existsIcon = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, fileID]];
- indexPath = [_sectionDataSource.fileIDIndexPath objectForKey:fileID];
- }
-
- if ([self indexPathIsValid:indexPath] && existsIcon) {
-
- UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
-
- if (cell) {
- UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
- UIVisualEffectView *effect = [cell viewWithTag:200];
- UIImageView *checked = [cell viewWithTag:300];
-
- imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, fileID]];
- effect.hidden = YES;
- checked.hidden = YES;
- }
- }
-
- } else {
-
- [appDelegate messageNotification:@"_download_selected_files_" description:@"_error_download_photobrowser_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
- }
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== Delete =====
- #pragma--------------------------------------------------------------------------------------------
- - (void)deleteFileOrFolderSuccessFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
- {
- [_queueMetadatas removeObject:metadataNet.selector];
-
- if ([_queueMetadatas count] == 0) {
-
- [_hud hideHud];
- if ([_selectedMetadatas count] > 0) {
-
- [_selectedMetadatas removeObjectAtIndex:0];
-
- if ([_selectedMetadatas count] > 0) {
-
- [self deleteFileOrFolder:[_selectedMetadatas objectAtIndex:0] numFile:[_selectedMetadatas count] ofFile:_numSelectedMetadatas];
-
- } else {
-
- [self reloadDatasourceFromSearch:NO];
- }
-
- } else {
-
- [self reloadDatasourceFromSearch:NO];
- }
- }
- }
- - (void)deleteFileOrFolder:(tableMetadata *)metadata numFile:(NSInteger)numFile ofFile:(NSInteger)ofFile
- {
- [_queueMetadatas addObject:selectorDelete];
-
- [[CCActions sharedInstance] deleteFileOrFolder:metadata delegate:self hud:_hud hudTitled:[NSString stringWithFormat:NSLocalizedString(@"_delete_file_n_", nil), ofFile - numFile + 1, ofFile]];
- }
- - (void)deleteSelectedFiles
- {
- [_queueMetadatas removeAllObjects];
-
- _numSelectedMetadatas = [_selectedMetadatas count];
-
- if ([_selectedMetadatas count] == 0)
- return;
-
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
-
- [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
- style:UIAlertActionStyleDestructive
- handler:^(UIAlertAction *action) {
- [self deleteFileOrFolder:[_selectedMetadatas objectAtIndex:0] numFile:[_selectedMetadatas count] ofFile:_numSelectedMetadatas];
- }]];
-
- [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
- style:UIAlertActionStyleCancel
- handler:^(UIAlertAction *action) {
- [alertController dismissViewControllerAnimated:YES completion:nil];
- }]];
-
- alertController.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItems.firstObject;
-
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
- [alertController.view layoutIfNeeded];
-
- [self presentViewController:alertController animated:YES completion:NULL];
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ==== Download Thumbnail Delegate ====
- #pragma --------------------------------------------------------------------------------------------
- - (void)downloadThumbnailSuccessFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
- {
- // Check Active Account
- if (![metadataNet.account isEqualToString:appDelegate.activeAccount])
- return;
-
- if (errorCode == 0) {
-
- NSIndexPath *indexPath = [_sectionDataSource.fileIDIndexPath objectForKey:metadataNet.fileID];
-
- if ([self indexPathIsValid:indexPath]) {
-
- if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, metadataNet.fileID]])
- [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
- }
- }
- }
- - (void)triggerProgressTask:(NSNotification *)notification
- {
- //NSDictionary *dict = notification.userInfo;
- //float progress = [[dict valueForKey:@"progress"] floatValue];
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ==== Change Start directory ====
- #pragma --------------------------------------------------------------------------------------------
- - (void)moveServerUrlTo:(NSString *)serverUrlTo title:(NSString *)title
- {
- NSString *oldStartDirectoryPhotosTab = [[NCManageDatabase sharedInstance] getAccountStartDirectoryPhotosTab:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]];
-
- if (![serverUrlTo isEqualToString:oldStartDirectoryPhotosTab]) {
-
- // Save Start Directory
- [[NCManageDatabase sharedInstance] setAccountStartDirectoryPhotosTab:serverUrlTo];
-
- // search PhotoVideo with new start directory
- [self searchPhotoVideo];
- }
- }
- - (void)selectStartDirectoryPhotosTab
- {
- UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"CCMove" bundle:nil] instantiateViewControllerWithIdentifier:@"CCMove"];
-
- CCMove *viewController = (CCMove *)navigationController.topViewController;
-
- viewController.delegate = self;
- viewController.move.title = NSLocalizedString(@"_select_dir_photos_tab_", nil);
- viewController.tintColor = [NCBrandColor sharedInstance].brandText;
- viewController.barTintColor = [NCBrandColor sharedInstance].brand;
- viewController.tintColorTitle = [NCBrandColor sharedInstance].brandText;
- viewController.networkingOperationQueue = appDelegate.netQueue;
- viewController.hideCreateFolder = YES;
- // E2EE
- viewController.includeDirectoryE2EEncryption = NO;
-
- [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
- [self presentViewController:navigationController animated:YES completion:nil];
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ==== Search Photo/Video ====
- #pragma --------------------------------------------------------------------------------------------
- - (void)searchSuccessFailure:(CCMetadataNet *)metadataNet metadatas:(NSArray *)metadatas message:(NSString *)message errorCode:(NSInteger)errorCode
- {
- // Check Active Account
- if (![metadataNet.account isEqualToString:appDelegate.activeAccount]) {
- [self searchInProgress:NO];
- return;
- }
-
- if (errorCode == 0) {
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
-
- NSString *startDirectory = [[NCManageDatabase sharedInstance] getAccountStartDirectoryPhotosTab:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]];
- (void)[[NCManageDatabase sharedInstance] updateTableMetadatasContentTypeImageVideo:metadatas startDirectory:startDirectory activeUrl:appDelegate.activeUrl];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [self reloadDatasourceFromSearch:YES];
- });
-
- // Update date
- [[NCManageDatabase sharedInstance] setAccountDateSearchContentTypeImageVideo:[NSDate date]];
- // Save etag
- [_saveEtagForStartDirectory setObject:metadataNet.etag forKey:metadataNet.serverUrl];
- });
-
- } else {
- [self searchInProgress:NO];
- }
- }
- - (void)searchPhotoVideo
- {
- // test
- if (appDelegate.activeAccount.length == 0 || _isSearchMode || _isEditMode)
- return;
-
- // WAITING FOR d:creationdate
- //
- // tableAccount *account = [[NCManageDatabase sharedInstance] getAccountActive];
- // account.dateSearchContentTypeImageVideo
-
- NSString *startDirectory = [[NCManageDatabase sharedInstance] getAccountStartDirectoryPhotosTab:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]];
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
-
- NSArray *items;
- NSError *error = [[NCNetworkingSync sharedManager] readFile:startDirectory user:appDelegate.activeUser userID:appDelegate.activeUserID password:appDelegate.activePassword items:&items];
-
- if (error == nil && items.count > 0) {
-
- OCFileDto *fileStartDirectory = items[0];
-
- if (![fileStartDirectory.etag isEqualToString:[_saveEtagForStartDirectory objectForKey:startDirectory]]) {
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[CCActions sharedInstance] search:startDirectory fileName:@"" etag:fileStartDirectory.etag depth:@"infinity" date:[NSDate distantPast] contenType:@[@"image/%", @"video/%"] selector:selectorSearchContentType delegate:self];
- [self searchInProgress:YES];
- [self editingModeNO];
- });
- } else {
- [self reloadDatasourceFromSearch:YES];
- }
- } else {
- [self reloadDatasourceFromSearch:YES];
- }
- });
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ==== Datasource ====
- #pragma --------------------------------------------------------------------------------------------
- - (void)reloadDatasourceFromSearch:(BOOL)fromSearch
- {
- @synchronized(self) {
- // test
- if (appDelegate.activeAccount.length == 0) {
- [self searchInProgress:NO];
- return;
- }
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- NSString *startDirectory = [[NCManageDatabase sharedInstance] getAccountStartDirectoryPhotosTab:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]];
- NSArray *metadatasDBImageVideo = [[NCManageDatabase sharedInstance] getTableMetadatasContentTypeImageVideo:startDirectory activeUrl:appDelegate.activeUrl];
- CCSectionDataSourceMetadata *tempSectionDataSource = [CCSectionMetadata creataDataSourseSectionMetadata:metadatasDBImageVideo listProgressMetadata:nil groupByField:@"date" activeAccount:appDelegate.activeAccount];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- // OPTIMIZED
- if (tempSectionDataSource.totalSize != _sectionDataSource.totalSize || tempSectionDataSource.files != _sectionDataSource.files) {
- _sectionDataSource = [tempSectionDataSource copy];
- [self.collectionView reloadData];
- }
- if (fromSearch) {
- [self searchInProgress:NO];
- }
- });
- });
- }
- }
- - (void)editingModeYES
- {
- [self.collectionView setAllowsMultipleSelection:true];
- _isEditMode = true;
- [self setUINavigationBarSelected];
- }
- - (void)editingModeNO
- {
- [self.collectionView setAllowsMultipleSelection:false];
- _isEditMode = false;
- [_selectedMetadatas removeAllObjects];
- [self setUINavigationBarDefault];
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ==== Collection ====
- #pragma --------------------------------------------------------------------------------------------
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
- {
- return [[_sectionDataSource.sectionArrayRow allKeys] count];
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- {
- return [[_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:section]] count];
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- UIInterfaceOrientation orientationOnLunch = [[UIApplication sharedApplication] statusBarOrientation];
-
- if (orientationOnLunch == UIInterfaceOrientationPortrait)
- return CGSizeMake(collectionView.frame.size.width / 5.1f, collectionView.frame.size.width / 5.1f);
- else
- return CGSizeMake(collectionView.frame.size.width / 7.1f, collectionView.frame.size.width / 7.1f);
- }
- -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
- {
- if ([_sectionDataSource.sections count] - 1 == section)
- return CGSizeMake(collectionView.frame.size.width, 50);
-
- return CGSizeZero;
- }
- - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
- {
- if (kind == UICollectionElementKindSectionHeader) {
-
- UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
-
- //headerView.backgroundColor = COLOR_GROUPBY_BAR_NO_BLUR;
-
- [self getGeoLocationForSection:indexPath.section];
-
- UILabel *titleLabel = (UILabel *)[headerView viewWithTag:100];
- titleLabel.textColor = [UIColor blackColor];
- if (_sectionDataSource.sections.count > indexPath.section)
- titleLabel.text = [CCUtility getTitleSectionDate:[_sectionDataSource.sections objectAtIndex:indexPath.section]];
- return headerView;
- }
-
- if (kind == UICollectionElementKindSectionFooter) {
-
- UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
-
- UILabel *titleLabel = (UILabel *)[footerView viewWithTag:100];
- titleLabel.textColor = [UIColor grayColor];
- titleLabel.text = [NSString stringWithFormat:@"%lu %@, %lu %@", (long)_sectionDataSource.image, NSLocalizedString(@"photo", nil), (long)_sectionDataSource.video, NSLocalizedString(@"_video_", nil)];
-
- return footerView;
- }
-
- return nil;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
- UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
- UIVisualEffectView *effect = [cell viewWithTag:200];
- UIImageView *checked = [cell viewWithTag:300];
- checked.image = [UIImage imageNamed:@"checked"];
- NSArray *metadatasForKey = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]];
-
- if ([metadatasForKey count] > indexPath.row) {
-
- NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
- tableMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
-
- // Image
- if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, metadata.fileID]]) {
-
- // insert Image
- imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, metadata.fileID]];
-
- } else {
-
- tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@", appDelegate.activeAccount, metadata.directoryID]];
- // Thumbnail not present
- if (directory.e2eEncrypted) {
-
- imageView.image = [UIImage imageNamed:@"file_photo_encrypted"];
-
- } else {
-
- imageView.image = [UIImage imageNamed:@"file_photo"];
- if (metadata.thumbnailExists)
- [[CCActions sharedInstance] downloadTumbnail:metadata delegate:self];
- }
- }
-
- // Cheched
- if (cell.selected) {
- checked.hidden = NO;
- effect.hidden = NO;
- effect.alpha = 0.4;
- } else {
- checked.hidden = YES;
- effect.hidden = YES;
- }
- }
-
- return cell;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- {
- NSArray *metadatasForKey = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]];
-
- if ([metadatasForKey count] > indexPath.row) {
-
- NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
- _metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
-
- if (_isEditMode) {
-
- [self cellSelect:YES indexPath:indexPath metadata:_metadata];
-
- } else {
-
- if ([self shouldPerformSegue])
- [self performSegueWithIdentifier:@"segueDetail" sender:self];
- }
- }
- }
- - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
- {
- // test
- if (_isEditMode == NO)
- return;
-
- NSArray *metadatasForKey = [_sectionDataSource.sectionArrayRow objectForKey:[_sectionDataSource.sections objectAtIndex:indexPath.section]];
-
- if ([metadatasForKey count] > indexPath.row) {
-
- NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
- _metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
-
- [self cellSelect:NO indexPath:indexPath metadata:_metadata];
- }
- }
- - (BOOL)indexPathIsValid:(NSIndexPath *)indexPath
- {
- if (!indexPath)
- return NO;
-
- NSInteger section = indexPath.section;
- NSInteger row = indexPath.row;
-
- NSInteger lastSectionIndex = [self numberOfSectionsInCollectionView:self.collectionView] - 1;
-
- if (section > lastSectionIndex || lastSectionIndex < 0)
- return NO;
-
- NSInteger rowCount = [self.collectionView numberOfItemsInSection:indexPath.section] - 1;
-
- if (rowCount < 0)
- return NO;
-
- return row <= rowCount;
- }
- #pragma --------------------------------------------------------------------------------------------
- #pragma mark ===== Navigation ====
- #pragma --------------------------------------------------------------------------------------------
- - (BOOL)shouldPerformSegue
- {
- // Test
-
- // Background ? exit
- if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground)
- return NO;
-
- // Not in first plain ? exit
- if (self.view.window == NO)
- return NO;
-
- // Collapsed but in first plain in detail exit
- if (self.splitViewController.isCollapsed)
- if (self.detailViewController.isViewLoaded && self.detailViewController.view.window)
- return NO;
-
- // ok perform segue
- return YES;
- }
- -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
- {
- id controller = segue.destinationViewController;
-
- if ([controller isKindOfClass:[UINavigationController class]]) {
- UINavigationController *navigationController = controller;
- self.detailViewController = (CCDetail *)navigationController.topViewController;
- } else {
- self.detailViewController = segue.destinationViewController;
- }
-
- NSMutableArray *allRecordsDataSourceImagesVideos = [[NSMutableArray alloc] init];
- for (NSString *fileID in _sectionDataSource.allEtag) {
- tableMetadata *metadata = [_sectionDataSource.allRecordsDataSource objectForKey:fileID];
- if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image] || [metadata.typeFile isEqualToString: k_metadataTypeFile_video])
- [allRecordsDataSourceImagesVideos addObject:metadata];
- }
-
- self.detailViewController.dataSourceImagesVideos = allRecordsDataSourceImagesVideos;
- self.detailViewController.metadataDetail = _metadata;
- self.detailViewController.dateFilterQuery = _metadata.date;
-
- [self.detailViewController setTitle:_metadata.fileName];
- }
- @end
|