CCMedia.m 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. //
  2. // CCMedia.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 29/07/15.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 "CCMedia.h"
  24. #import "AppDelegate.h"
  25. #import "CCManageAutoUpload.h"
  26. #import "TOScrollBar.h"
  27. #import "NCBridgeSwift.h"
  28. @interface CCMedia () <NCSelectDelegate>
  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 collectionViewReloadDataInProgress;
  43. // Remenu
  44. REMenu *menu;
  45. REMenuItem *menuSelectMediaFolder;
  46. REMenuItem *menuFilterImage;
  47. REMenuItem *menuFilterVideo;
  48. REMenuItem *menuSelectItems;
  49. }
  50. @end
  51. @implementation CCMedia
  52. #pragma --------------------------------------------------------------------------------------------
  53. #pragma mark ===== Init =====
  54. #pragma --------------------------------------------------------------------------------------------
  55. - (id)initWithCoder:(NSCoder *)aDecoder
  56. {
  57. if (self = [super initWithCoder:aDecoder]) {
  58. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  59. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:@"NotificationProgressTask" object:nil];
  60. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  61. appDelegate.activeMedia = self;
  62. }
  63. return self;
  64. }
  65. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  66. {
  67. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  68. if (self) {
  69. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  70. }
  71. return self;
  72. }
  73. #pragma --------------------------------------------------------------------------------------------
  74. #pragma mark ===== View =====
  75. #pragma --------------------------------------------------------------------------------------------
  76. - (void)viewDidLoad
  77. {
  78. [super viewDidLoad];
  79. saveEtagForStartDirectory = [NSMutableDictionary new];
  80. selectedMetadatas = [NSMutableArray new];
  81. self.addMetadatasFromUpload = [NSMutableArray new];
  82. // empty Data Source
  83. self.collectionView.emptyDataSetDelegate = self;
  84. self.collectionView.emptyDataSetSource = self;
  85. // scroll bar
  86. scrollBar = [TOScrollBar new];
  87. [self.collectionView to_addScrollBar:scrollBar];
  88. scrollBar.handleTintColor = [NCBrandColor sharedInstance].brand;
  89. scrollBar.handleWidth = 20;
  90. scrollBar.handleMinimiumHeight = 20;
  91. scrollBar.trackWidth = 0;
  92. scrollBar.edgeInset = 12;
  93. // Query data source
  94. [self queryDatasourceWithReloadData:YES];
  95. }
  96. - (void)viewWillAppear:(BOOL)animated
  97. {
  98. [super viewWillAppear:animated];
  99. // Color
  100. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  101. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  102. // Plus Button
  103. [appDelegate plusButtonVisibile:true];
  104. }
  105. - (void)viewDidAppear:(BOOL)animated
  106. {
  107. [super viewDidAppear:animated];
  108. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  109. [self reloadDatasource:nil action:k_action_NULL];
  110. });
  111. }
  112. - (void)viewSafeAreaInsetsDidChange
  113. {
  114. [super viewSafeAreaInsetsDidChange];
  115. self.collectionView.contentInset = self.view.safeAreaInsets;
  116. }
  117. - (void)changeTheming
  118. {
  119. if (self.isViewLoaded && self.view.window)
  120. [appDelegate changeTheming:self];
  121. scrollBar.handleTintColor = [NCBrandColor sharedInstance].brand;
  122. [self.collectionView reloadData];
  123. }
  124. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  125. {
  126. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  127. [menu close];
  128. // Before rotation
  129. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
  130. 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))) {
  131. // Portrait
  132. } else {
  133. // Landscape
  134. }
  135. [self.collectionView reloadData];
  136. }];
  137. }
  138. #pragma --------------------------------------------------------------------------------------------
  139. #pragma mark ===== Gestione Grafica Window =====
  140. #pragma --------------------------------------------------------------------------------------------
  141. - (void)openMenu
  142. {
  143. if (menu.isOpen) {
  144. [menu close];
  145. } else {
  146. [self createReMainMenu];
  147. [menu showFromNavigationController:self.navigationController];
  148. // Backgroun reMenu & (Gesture)
  149. [self createReMenuBackgroundView:menu];
  150. self.singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openMenu)];
  151. [self.reMenuBackgroundView addGestureRecognizer:_singleFingerTap];
  152. }
  153. }
  154. - (void)createReMenuBackgroundView:(REMenu *)menu
  155. {
  156. CGFloat safeAreaBottom = 0;
  157. CGFloat safeAreaTop = 0;
  158. CGFloat statusBar = 0;
  159. if (@available(iOS 11, *)) {
  160. safeAreaTop = [UIApplication sharedApplication].delegate.window.safeAreaInsets.top;
  161. safeAreaBottom = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
  162. }
  163. if ([UIApplication sharedApplication].isStatusBarHidden) {
  164. statusBar = 13;
  165. }
  166. CGFloat computeNavigationBarOffset = [menu computeNavigationBarOffset];
  167. UIViewController *rootController = [[[[UIApplication sharedApplication]delegate] window] rootViewController];
  168. CGRect globalPositionMenu = [menu.menuView convertRect:menu.menuView.bounds toView:rootController.view];
  169. self.reMenuBackgroundView = [UIView new];
  170. self.reMenuBackgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
  171. self.reMenuBackgroundView.frame = CGRectMake(0, computeNavigationBarOffset, globalPositionMenu.size.width, rootController.view.frame.size.height);
  172. [UIView animateWithDuration:0.2 animations:^{
  173. CGFloat minimum = safeAreaBottom + self.tabBarController.tabBar.frame.size.height;
  174. CGFloat y = rootController.view.frame.size.height - menu.menuView.frame.size.height - globalPositionMenu.origin.y + statusBar;
  175. if (y>minimum) {
  176. self.reMenuBackgroundView.frame = CGRectMake(0, rootController.view.frame.size.height, globalPositionMenu.size.width, - y);
  177. [self.tabBarController.view addSubview:self.reMenuBackgroundView];
  178. }
  179. }];
  180. }
  181. - (void)createReMainMenu
  182. {
  183. menuSelectItems = [[REMenuItem alloc] initWithTitle:NSLocalizedString(@"_select_", nil)subtitle:@"" image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"select"] multiplier:2 color:[NCBrandColor sharedInstance].icon] highlightedImage:nil action:^(REMenuItem *item) {
  184. if ([sectionDataSource.allRecordsDataSource count] > 0) {
  185. [self editingModeYES];
  186. }
  187. }];
  188. menuSelectMediaFolder = [[REMenuItem alloc] initWithTitle:NSLocalizedString(@"_select_media_folder_", nil)subtitle:@"" image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folderMedia"] multiplier:2 color:[NCBrandColor sharedInstance].icon] highlightedImage:nil action:^(REMenuItem *item) {
  189. [self selectStartDirectoryPhotosTab];
  190. }];
  191. if (filterTypeFileImage) {
  192. menuFilterImage = [[REMenuItem alloc] initWithTitle:NSLocalizedString(@"_media_viewimage_show_", nil)subtitle:@"" image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"imageno"] multiplier:2 color:[NCBrandColor sharedInstance].icon] highlightedImage:nil action:^(REMenuItem *item) {
  193. filterTypeFileImage = NO;
  194. [self reloadDatasource:nil action:k_action_NULL];
  195. }];
  196. } else {
  197. menuFilterImage = [[REMenuItem alloc] initWithTitle:NSLocalizedString(@"_media_viewimage_hide_", nil)subtitle:@"" image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"imageyes"] multiplier:2 color:[NCBrandColor sharedInstance].icon] highlightedImage:nil action:^(REMenuItem *item) {
  198. filterTypeFileImage = YES;
  199. [self reloadDatasource:nil action:k_action_NULL];
  200. }];
  201. }
  202. if (filterTypeFileVideo) {
  203. menuFilterVideo = [[REMenuItem alloc] initWithTitle:NSLocalizedString(@"_media_viewvideo_show_", nil)subtitle:@"" image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"videono"] multiplier:2 color:[NCBrandColor sharedInstance].icon] highlightedImage:nil action:^(REMenuItem *item) {
  204. filterTypeFileVideo = NO;
  205. [self reloadDatasource:nil action:k_action_NULL];
  206. }];
  207. } else {
  208. menuFilterVideo = [[REMenuItem alloc] initWithTitle:NSLocalizedString(@"_media_viewvideo_hide_", nil)subtitle:@"" image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"videoyes"] multiplier:2 color:[NCBrandColor sharedInstance].icon] highlightedImage:nil action:^(REMenuItem *item) {
  209. filterTypeFileVideo = YES;
  210. [self reloadDatasource:nil action:k_action_NULL];
  211. }];
  212. }
  213. // REMENU --------------------------------------------------------------------------------------------------------------
  214. menu = [[REMenu alloc] initWithItems:@[menuSelectItems, menuSelectMediaFolder, menuFilterImage, menuFilterVideo]];
  215. menu.imageOffset = CGSizeMake(5, -1);
  216. menu.separatorOffset = CGSizeMake(50.0, 0.0);
  217. menu.imageOffset = CGSizeMake(0, 0);
  218. menu.waitUntilAnimationIsComplete = NO;
  219. menu.separatorHeight = 0.5;
  220. menu.separatorColor = [NCBrandColor sharedInstance].seperator;
  221. menu.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  222. menu.textColor = [UIColor blackColor];
  223. menu.textAlignment = NSTextAlignmentLeft;
  224. menu.textShadowColor = nil;
  225. menu.textOffset = CGSizeMake(50, 0.0);
  226. menu.font = [UIFont systemFontOfSize:14.0];
  227. menu.highlightedBackgroundColor = [[NCBrandColor sharedInstance] getColorSelectBackgrond];
  228. menu.highlightedSeparatorColor = nil;
  229. menu.highlightedTextColor = [UIColor blackColor];
  230. menu.highlightedTextShadowColor = nil;
  231. menu.highlightedTextShadowOffset = CGSizeMake(0, 0);
  232. menu.subtitleTextColor = [UIColor colorWithWhite:0.425 alpha:1];
  233. menu.subtitleTextAlignment = NSTextAlignmentLeft;
  234. menu.subtitleTextShadowColor = nil;
  235. menu.subtitleTextShadowOffset = CGSizeMake(0, 0.0);
  236. menu.subtitleTextOffset = CGSizeMake(50, 0.0);
  237. menu.subtitleFont = [UIFont systemFontOfSize:12.0];
  238. menu.subtitleHighlightedTextColor = [UIColor lightGrayColor];
  239. menu.subtitleHighlightedTextShadowColor = nil;
  240. menu.subtitleHighlightedTextShadowOffset = CGSizeMake(0, 0);
  241. menu.borderWidth = 0.3;
  242. menu.borderColor = [UIColor lightGrayColor];
  243. menu.animationDuration = 0.2;
  244. menu.closeAnimationDuration = 0.2;
  245. menu.bounce = NO;
  246. __weak typeof(self) weakSelf = self;
  247. [menu setClosePreparationBlock:^{
  248. // Backgroun reMenu (Gesture)
  249. [weakSelf.reMenuBackgroundView removeFromSuperview];
  250. [weakSelf.reMenuBackgroundView removeGestureRecognizer:weakSelf.singleFingerTap];
  251. }];
  252. }
  253. - (void)setUINavigationBarDefault
  254. {
  255. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  256. // curront folder search
  257. NSString *directory = [[NCManageDatabase sharedInstance] getAccountStartDirectoryMediaTabView:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]];
  258. NSString *home = [CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl];
  259. NSString *folder = @"";
  260. if (home.length > 0) {
  261. folder = [directory stringByReplacingOccurrencesOfString:home withString:@""];
  262. }
  263. // Title
  264. NSString *title;
  265. if (folder.length == 0) {
  266. title = NSLocalizedString(@"_media_", nil);
  267. } else {
  268. title = [NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"_media_", nil), [folder substringFromIndex:1]];
  269. }
  270. self.navigationItem.titleView = nil;
  271. self.navigationItem.title = title;
  272. if (isSearchMode) {
  273. [CCGraphics addImageToTitle:title colorTitle:[NCBrandColor sharedInstance].brandText imageTitle:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"load"] multiplier:2 color:[NCBrandColor sharedInstance].brandText] imageRight:NO navigationItem:self.navigationItem];
  274. }
  275. // Button Item RIGHT
  276. UIBarButtonItem *buttonMore = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navigationControllerMenu"] style:UIBarButtonItemStylePlain target:self action:@selector(openMenu)];
  277. self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:buttonMore, nil];
  278. self.navigationItem.leftBarButtonItems = nil;
  279. }
  280. - (void)setUINavigationBarSelected
  281. {
  282. UIBarButtonItem *buttonDelete = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"delete"] style:UIBarButtonItemStylePlain target:self action:@selector(deleteSelectedFiles)];
  283. // UIBarButtonItem *buttonOpenIn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"openFile"] style:UIBarButtonItemStylePlain target:self action:@selector(openSelectedFiles)];
  284. UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(editingModeNO)];
  285. self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:cancelButton, nil];
  286. self.navigationItem.leftBarButtonItems = [[NSArray alloc] initWithObjects:buttonDelete, nil];
  287. // Title
  288. self.navigationItem.title = [NSString stringWithFormat:@"%@ : %lu / %lu", NSLocalizedString(@"_selected_", nil), (unsigned long)[selectedMetadatas count], (unsigned long)[sectionDataSource.allRecordsDataSource count]];
  289. }
  290. - (void)cellSelect:(BOOL)select indexPath:(NSIndexPath *)indexPath metadata:(tableMetadata *)metadata
  291. {
  292. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
  293. UIVisualEffectView *effect = [cell viewWithTag:200];
  294. UIImageView *checked = [cell viewWithTag:300];
  295. if (select) {
  296. effect.hidden = NO;
  297. effect.alpha = 0.4;
  298. checked.hidden = NO;
  299. [selectedMetadatas addObject:metadata];
  300. } else {
  301. effect.hidden = YES;
  302. checked.hidden = YES;
  303. [selectedMetadatas removeObject:metadata];
  304. }
  305. // Title
  306. self.navigationItem.title = [NSString stringWithFormat:@"%@ : %lu / %lu", NSLocalizedString(@"_selected_", nil), (unsigned long)[selectedMetadatas count], (unsigned long)[sectionDataSource.allRecordsDataSource count]];
  307. }
  308. - (void)scrollToTop
  309. {
  310. [self.collectionView setContentOffset:CGPointMake(0, - self.collectionView.contentInset.top) animated:NO];
  311. }
  312. - (void)getGeoLocationForSection:(NSInteger)section
  313. {
  314. NSString *addLocation = @"";
  315. NSArray *fileIDsForKey = [sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:section]];
  316. for (NSString *fileID in fileIDsForKey) {
  317. tableLocalFile *localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", fileID]];
  318. if ([localFile.exifLatitude floatValue] > 0 || [localFile.exifLongitude floatValue] > 0) {
  319. NSString *location = [[NCManageDatabase sharedInstance] getLocationFromGeoLatitude:localFile.exifLatitude longitude:localFile.exifLongitude];
  320. addLocation = [NSString stringWithFormat:@"%@, %@", addLocation, location];
  321. }
  322. }
  323. }
  324. #pragma --------------------------------------------------------------------------------------------
  325. #pragma mark ==== DZNEmptyDataSetSource Methods ====
  326. #pragma --------------------------------------------------------------------------------------------
  327. - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
  328. {
  329. return [NCBrandColor sharedInstance].backgroundView;
  330. }
  331. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
  332. {
  333. return [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"mediaNoRecord"] multiplier:1 color:[NCBrandColor sharedInstance].graySoft];
  334. }
  335. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
  336. {
  337. NSString *text;
  338. if (isSearchMode) {
  339. text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_search_in_progress_", nil)];
  340. } else {
  341. text = [NSString stringWithFormat:@"\n%@", NSLocalizedString(@"_tutorial_photo_view_", nil)];
  342. }
  343. NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor lightGrayColor]};
  344. return [[NSAttributedString alloc] initWithString:text attributes:attributes];
  345. }
  346. /*
  347. #pragma --------------------------------------------------------------------------------------------
  348. #pragma mark ===== openSelectedFiles =====
  349. #pragma--------------------------------------------------------------------------------------------
  350. - (void)openSelectedFiles
  351. {
  352. NSMutableArray *dataToShare = [[NSMutableArray alloc] init];
  353. for (tableMetadata *metadata in selectedMetadatas) {
  354. NSString *fileNamePath = [CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileName:metadata.fileNameView];
  355. if ([CCUtility fileProviderStorageExists:metadata.fileID fileName:metadata.fileNameView]) {
  356. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image]) {
  357. NSData *data = [NSData dataWithData:UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:fileNamePath], 0.9)];
  358. [dataToShare addObject:data];
  359. }
  360. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video]) {
  361. [dataToShare addObject:[NSURL fileURLWithPath:fileNamePath]];
  362. }
  363. }
  364. }
  365. if ([dataToShare count] > 0) {
  366. UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
  367. // iPad
  368. activityViewController.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItems.lastObject;
  369. self.navigationItem.leftBarButtonItem.enabled = NO;
  370. self.navigationItem.rightBarButtonItem.enabled = NO;
  371. [self presentViewController:activityViewController animated:YES completion:^{
  372. [activityViewController setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
  373. self.navigationItem.leftBarButtonItem.enabled = YES;
  374. self.navigationItem.rightBarButtonItem.enabled = YES;
  375. [self editingModeNO];
  376. if (completed) {
  377. [self.collectionView reloadData];
  378. }
  379. }];
  380. }];
  381. }
  382. }
  383. */
  384. #pragma --------------------------------------------------------------------------------------------
  385. #pragma mark ===== Download =====
  386. #pragma--------------------------------------------------------------------------------------------
  387. - (void)triggerProgressTask:(NSNotification *)notification
  388. {
  389. //NSDictionary *dict = notification.userInfo;
  390. //float progress = [[dict valueForKey:@"progress"] floatValue];
  391. }
  392. - (void)downloadFileSuccessFailure:(NSString *)fileName fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector errorMessage:(NSString *)errorMessage errorCode:(NSInteger)errorCode
  393. {
  394. if (errorCode == 0) {
  395. NSIndexPath *indexPath;
  396. BOOL existsIcon = NO;
  397. if (fileID) {
  398. existsIcon = [[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:fileID fileNameView:fileName]];
  399. indexPath = [sectionDataSource.fileIDIndexPath objectForKey:fileID];
  400. }
  401. if ([self indexPathIsValid:indexPath] && existsIcon) {
  402. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
  403. if (cell) {
  404. UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
  405. UIVisualEffectView *effect = [cell viewWithTag:200];
  406. UIImageView *checked = [cell viewWithTag:300];
  407. imageView.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:fileID fileNameView:fileName]];
  408. effect.hidden = YES;
  409. checked.hidden = YES;
  410. }
  411. }
  412. } else {
  413. [appDelegate messageNotification:@"_download_selected_files_" description:@"_error_download_photobrowser_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  414. }
  415. }
  416. #pragma --------------------------------------------------------------------------------------------
  417. #pragma mark ===== Delete =====
  418. #pragma--------------------------------------------------------------------------------------------
  419. - (void)deleteFile:(NSArray *)metadatas e2ee:(BOOL)e2ee
  420. {
  421. [[NCMainCommon sharedInstance ] deleteFileWithMetadatas:metadatas e2ee:false serverUrl:@"" folderFileID:@"" completion:^(NSInteger errorCode, NSString *message) {
  422. [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:nil fileID:nil action:k_action_NULL];
  423. }];
  424. [self editingModeNO];
  425. }
  426. - (void)deleteSelectedFiles
  427. {
  428. if ([selectedMetadatas count] == 0)
  429. return;
  430. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  431. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil)
  432. style:UIAlertActionStyleDestructive
  433. handler:^(UIAlertAction *action) {
  434. [self deleteFile:selectedMetadatas e2ee:false];
  435. }]];
  436. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil)
  437. style:UIAlertActionStyleCancel
  438. handler:^(UIAlertAction *action) {
  439. }]];
  440. alertController.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItems.firstObject;
  441. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  442. [alertController.view layoutIfNeeded];
  443. [self presentViewController:alertController animated:YES completion:NULL];
  444. }
  445. #pragma --------------------------------------------------------------------------------------------
  446. #pragma mark ==== Download Thumbnail ====
  447. #pragma --------------------------------------------------------------------------------------------
  448. - (void)downloadThumbnail:(tableMetadata *)metadata indexPath:(NSIndexPath *)indexPath
  449. {
  450. if (![saveDirectoryID isEqualToString:metadata.directoryID]) {
  451. saveDirectoryID = metadata.directoryID;
  452. saveServerUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  453. if (!saveServerUrl)
  454. return;
  455. }
  456. counterThumbnail++;
  457. CGFloat width = [[NCUtility sharedInstance] getScreenWidthForPreview];
  458. CGFloat height = [[NCUtility sharedInstance] getScreenHeightForPreview];
  459. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:nil metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  460. [ocNetworking downloadPreviewWithMetadata:metadata serverUrl:saveServerUrl withWidth:width andHeight:height completion:^(NSString *message, NSInteger errorCode) {
  461. counterThumbnail--;
  462. if (errorCode == 0 && [[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]] && [self indexPathIsValid:indexPath] && !collectionViewReloadDataInProgress) {
  463. [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
  464. }
  465. }];
  466. }
  467. #pragma --------------------------------------------------------------------------------------------
  468. #pragma mark ==== Change Start directory ====
  469. #pragma --------------------------------------------------------------------------------------------
  470. - (void)dismissSelectWithServerUrl:(NSString *)serverUrl metadata:(tableMetadata *)metadata type:(NSString *)type
  471. {
  472. if ([type isEqualToString:@"mediaFolder"]) {
  473. NSString *oldStartDirectoryMediaTabView = [[NCManageDatabase sharedInstance] getAccountStartDirectoryMediaTabView:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]];
  474. if (![serverUrl isEqualToString:oldStartDirectoryMediaTabView]) {
  475. // Save Start Directory
  476. [[NCManageDatabase sharedInstance] setAccountStartDirectoryMediaTabView:serverUrl];
  477. // search PhotoVideo with new start directory
  478. [self searchPhotoVideo];
  479. }
  480. }
  481. /*
  482. if ([type isEqualToString:@"automaticUploadFolder"]) {
  483. if (title == nil) {
  484. [appDelegate messageNotification:@"_error_" description:@"_media_error_select_folder_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:0];
  485. return;
  486. }
  487. NSString *serverUrl = [NSURL URLWithString:serverUrlTo].URLByDeletingLastPathComponent.absoluteString;
  488. if ([[serverUrl substringFromIndex:[serverUrl length] - 1] isEqualToString:@"/"])
  489. serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  490. // Clear data (old) Auto Upload
  491. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:[[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:appDelegate.activeUrl] directoryID:nil];
  492. // Settings new folder Automatatic upload
  493. [[NCManageDatabase sharedInstance] setAccountAutoUploadFileName:title];
  494. [[NCManageDatabase sharedInstance] setAccountAutoUploadDirectory:serverUrl activeUrl:appDelegate.activeUrl];
  495. // Clear data new Auto Upload
  496. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:serverUrl directoryID:nil];
  497. }
  498. */
  499. }
  500. - (void)selectStartDirectoryPhotosTab
  501. {
  502. UINavigationController *navigationController = [[UIStoryboard storyboardWithName:@"NCSelect" bundle:nil] instantiateInitialViewController];
  503. NCSelect *viewController = (NCSelect *)navigationController.topViewController;
  504. viewController.delegate = self;
  505. viewController.hideButtonCreateFolder = true;
  506. viewController.selectFile = false;
  507. viewController.includeDirectoryE2EEncryption = false;
  508. viewController.includeImages = false;
  509. viewController.type = @"mediaFolder";
  510. viewController.titleButtonDone = NSLocalizedString(@"_select_", nil);
  511. viewController.layoutViewSelect = k_layout_view_move;
  512. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  513. [self presentViewController:navigationController animated:YES completion:nil];
  514. }
  515. #pragma --------------------------------------------------------------------------------------------
  516. #pragma mark ==== Search Photo/Video ====
  517. #pragma --------------------------------------------------------------------------------------------
  518. - (void)searchSuccessFailure:(CCMetadataNet *)metadataNet metadatas:(NSArray *)metadatas message:(NSString *)message errorCode:(NSInteger)errorCode
  519. {
  520. isSearchMode = NO;
  521. if (![metadataNet.account isEqualToString:appDelegate.activeAccount] || errorCode != 0) {
  522. [self reloadDatasource:nil action:k_action_NULL];
  523. } else {
  524. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  525. // Clear all Hardcoded new foto/video from CCNetworking
  526. [self.addMetadatasFromUpload removeAllObjects];
  527. [[NCManageDatabase sharedInstance] createTablePhotos:metadatas];
  528. dispatch_async(dispatch_get_main_queue(), ^{
  529. [self reloadDatasource:nil action:k_action_NULL];
  530. });
  531. // Update date
  532. [[NCManageDatabase sharedInstance] setAccountDateSearchContentTypeImageVideo:[NSDate date]];
  533. // Save etag
  534. [saveEtagForStartDirectory setObject:metadataNet.etag forKey:metadataNet.serverUrl];
  535. });
  536. }
  537. }
  538. - (void)searchPhotoVideo
  539. {
  540. // test
  541. if (appDelegate.activeAccount.length == 0 || isSearchMode)
  542. return;
  543. // WAITING FOR d:creationdate
  544. //
  545. // tableAccount *account = [[NCManageDatabase sharedInstance] getAccountActive];
  546. // account.dateSearchContentTypeImageVideo
  547. NSString *startDirectory = [[NCManageDatabase sharedInstance] getAccountStartDirectoryMediaTabView:[CCUtility getHomeServerUrlActiveUrl:appDelegate.activeUrl]];
  548. OCnetworking *ocNetworking = [[OCnetworking alloc] initWithDelegate:self metadataNet:nil withUser:appDelegate.activeUser withUserID:appDelegate.activeUserID withPassword:appDelegate.activePassword withUrl:appDelegate.activeUrl];
  549. [ocNetworking readFile:nil serverUrl:startDirectory account:appDelegate.activeAccount success:^(tableMetadata *metadata) {
  550. if (![metadata.etag isEqualToString:[saveEtagForStartDirectory objectForKey:startDirectory]] || sectionDataSource.allRecordsDataSource.count == 0) {
  551. isSearchMode = YES;
  552. [self editingModeNO];
  553. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  554. metadataNet.action = actionSearch;
  555. metadataNet.contentType = @[@"image/%", @"video/%"];
  556. metadataNet.date = [NSDate distantPast];
  557. metadataNet.directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:startDirectory];
  558. metadataNet.fileName = @"";
  559. metadataNet.etag = metadata.etag;
  560. metadataNet.depth = @"infinity";
  561. metadataNet.priority = NSOperationQueuePriorityHigh;
  562. metadataNet.selector = selectorSearchContentType;
  563. metadataNet.serverUrl = startDirectory;
  564. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  565. } else {
  566. [self reloadDatasource:nil action:k_action_NULL];
  567. }
  568. } failure:^(NSString *message, NSInteger errorCode) {
  569. }];
  570. }
  571. #pragma --------------------------------------------------------------------------------------------
  572. #pragma mark ==== Datasource ====
  573. #pragma --------------------------------------------------------------------------------------------
  574. - (void)reloadDatasource:(NSString *)fileID action:(NSInteger)action
  575. {
  576. // test
  577. if (appDelegate.activeAccount.length == 0 || self.view.window == nil) {
  578. return;
  579. }
  580. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  581. collectionViewReloadDataInProgress = YES;
  582. CCSectionDataSourceMetadata *sectionDataSourceTemp = [self queryDatasourceWithReloadData:NO];
  583. dispatch_async(dispatch_get_main_queue(), ^{
  584. if (isEditMode)
  585. [self setUINavigationBarSelected];
  586. else
  587. [self setUINavigationBarDefault];
  588. sectionDataSource = sectionDataSourceTemp;
  589. [self.collectionView reloadData];
  590. [self.collectionView performBatchUpdates:^{} completion:^(BOOL finished) {
  591. collectionViewReloadDataInProgress = NO;
  592. }];
  593. });
  594. });
  595. }
  596. - (CCSectionDataSourceMetadata *)queryDatasourceWithReloadData:(BOOL)withReloadData
  597. {
  598. // test
  599. if (appDelegate.activeAccount.length == 0) {
  600. return nil;
  601. }
  602. CCSectionDataSourceMetadata *sectionDataSourceTemp = [CCSectionDataSourceMetadata new];
  603. NSArray *metadatas = [[NCManageDatabase sharedInstance] getTablePhotosWithAddMetadatasFromUpload:self.addMetadatasFromUpload];
  604. sectionDataSourceTemp = [CCSectionMetadata creataDataSourseSectionMetadata:metadatas listProgressMetadata:nil groupByField:@"date" filterFileID:appDelegate.filterFileID filterTypeFileImage:filterTypeFileImage filterTypeFileVideo:filterTypeFileVideo activeAccount:appDelegate.activeAccount];
  605. if (withReloadData) {
  606. sectionDataSource = sectionDataSourceTemp;
  607. [self.collectionView reloadData];
  608. }
  609. return sectionDataSourceTemp;
  610. }
  611. - (void)editingModeYES
  612. {
  613. [self.collectionView setAllowsMultipleSelection:true];
  614. isEditMode = true;
  615. [selectedMetadatas removeAllObjects];
  616. [self setUINavigationBarSelected];
  617. collectionViewReloadDataInProgress = YES;
  618. [self.collectionView reloadData];
  619. [self.collectionView performBatchUpdates:^{} completion:^(BOOL finished) {
  620. collectionViewReloadDataInProgress = NO;
  621. }];
  622. }
  623. - (void)editingModeNO
  624. {
  625. [self.collectionView setAllowsMultipleSelection:false];
  626. isEditMode = false;
  627. [selectedMetadatas removeAllObjects];
  628. [self setUINavigationBarDefault];
  629. collectionViewReloadDataInProgress = YES;
  630. [self.collectionView reloadData];
  631. [self.collectionView performBatchUpdates:^{} completion:^(BOOL finished) {
  632. collectionViewReloadDataInProgress = NO;
  633. }];
  634. }
  635. #pragma --------------------------------------------------------------------------------------------
  636. #pragma mark ==== Collection ====
  637. #pragma --------------------------------------------------------------------------------------------
  638. - (BOOL)indexPathIsValid:(NSIndexPath *)indexPath
  639. {
  640. return indexPath.section < [self numberOfSectionsInCollectionView:self.collectionView] && indexPath.row < [self collectionView:self.collectionView numberOfItemsInSection:indexPath.section];
  641. }
  642. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  643. {
  644. return [[sectionDataSource.sectionArrayRow allKeys] count];
  645. }
  646. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  647. {
  648. return [[sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:section]] count];
  649. }
  650. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  651. {
  652. UIInterfaceOrientation orientationOnLunch = [[UIApplication sharedApplication] statusBarOrientation];
  653. if (orientationOnLunch == UIInterfaceOrientationPortrait)
  654. return CGSizeMake(collectionView.frame.size.width / 5.1f, collectionView.frame.size.width / 5.1f);
  655. else
  656. return CGSizeMake(collectionView.frame.size.width / 7.1f, collectionView.frame.size.width / 7.1f);
  657. }
  658. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  659. {
  660. if ([sectionDataSource.sections count] - 1 == section)
  661. return CGSizeMake(collectionView.frame.size.width, 50);
  662. return CGSizeZero;
  663. }
  664. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  665. {
  666. if (kind == UICollectionElementKindSectionHeader) {
  667. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
  668. //headerView.backgroundColor = COLOR_GROUPBY_BAR_NO_BLUR;
  669. [self getGeoLocationForSection:indexPath.section];
  670. UILabel *titleLabel = (UILabel *)[headerView viewWithTag:100];
  671. titleLabel.textColor = [UIColor blackColor];
  672. if (sectionDataSource.sections.count > indexPath.section)
  673. titleLabel.text = [CCUtility getTitleSectionDate:[sectionDataSource.sections objectAtIndex:indexPath.section]];
  674. return headerView;
  675. }
  676. if (kind == UICollectionElementKindSectionFooter) {
  677. UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
  678. UILabel *titleLabel = (UILabel *)[footerView viewWithTag:100];
  679. titleLabel.textColor = [UIColor grayColor];
  680. titleLabel.text = [NSString stringWithFormat:@"%lu %@, %lu %@", (long)sectionDataSource.image, NSLocalizedString(@"photo", nil), (long)sectionDataSource.video, NSLocalizedString(@"_video_", nil)];
  681. return footerView;
  682. }
  683. return nil;
  684. }
  685. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  686. {
  687. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  688. UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
  689. UIVisualEffectView *effect = [cell viewWithTag:200];
  690. UIImageView *checkedOverlay = [cell viewWithTag:300];
  691. checkedOverlay.image = [CCGraphics changeThemingColorImage:[UIImage imageNamed:@"checkedYes"] multiplier:2 color:[NCBrandColor sharedInstance].brand];
  692. UIImageView *videoOverlay = [cell viewWithTag:400];
  693. videoOverlay.image = [UIImage imageNamed:@"VideoOverlay"];
  694. NSArray *metadatasForKey = [sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:indexPath.section]];
  695. if ([metadatasForKey count] > indexPath.row) {
  696. NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
  697. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:fileID];
  698. // Image
  699. if ([[NSFileManager defaultManager] fileExistsAtPath:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]]) {
  700. // insert Image
  701. imageView.image = [UIImage imageWithContentsOfFile:[CCUtility getDirectoryProviderStorageIconFileID:metadata.fileID fileNameView:metadata.fileNameView]];
  702. } else {
  703. imageView.image = [UIImage imageNamed:@"file_photo"];
  704. if (metadata.hasPreview == 1 && ![CCUtility fileProviderStorageIconExists:metadata.fileID fileNameView:metadata.fileNameView]) {
  705. [self downloadThumbnail:metadata indexPath:indexPath];
  706. }
  707. }
  708. // Cheched Overlay
  709. if (cell.selected) {
  710. checkedOverlay.hidden = NO;
  711. effect.hidden = NO;
  712. effect.alpha = 0.4;
  713. } else {
  714. checkedOverlay.hidden = YES;
  715. effect.hidden = YES;
  716. }
  717. // Video Overlay
  718. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_video]) {
  719. videoOverlay.hidden = NO;
  720. } else {
  721. videoOverlay.hidden = YES;
  722. }
  723. }
  724. return cell;
  725. }
  726. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  727. {
  728. NSArray *metadatasForKey = [sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:indexPath.section]];
  729. if ([metadatasForKey count] > indexPath.row) {
  730. NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
  731. self.metadata = [sectionDataSource.allRecordsDataSource objectForKey:fileID];
  732. if (isEditMode) {
  733. [self cellSelect:YES indexPath:indexPath metadata:self.metadata];
  734. } else {
  735. if ([self shouldPerformSegue])
  736. [self performSegueWithIdentifier:@"segueDetail" sender:self];
  737. }
  738. }
  739. }
  740. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
  741. {
  742. // test
  743. if (isEditMode == NO)
  744. return;
  745. NSArray *metadatasForKey = [sectionDataSource.sectionArrayRow objectForKey:[sectionDataSource.sections objectAtIndex:indexPath.section]];
  746. if ([metadatasForKey count] > indexPath.row) {
  747. NSString *fileID = [metadatasForKey objectAtIndex:indexPath.row];
  748. self.metadata = [sectionDataSource.allRecordsDataSource objectForKey:fileID];
  749. [self cellSelect:NO indexPath:indexPath metadata:self.metadata];
  750. }
  751. }
  752. #pragma --------------------------------------------------------------------------------------------
  753. #pragma mark ===== Navigation ====
  754. #pragma --------------------------------------------------------------------------------------------
  755. - (BOOL)shouldPerformSegue
  756. {
  757. if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground)
  758. return NO;
  759. // Not in first plain ? exit
  760. if (self.view.window == NO)
  761. return NO;
  762. // Collapsed but in first plain in detail exit
  763. if (self.splitViewController.isCollapsed)
  764. if (self.detailViewController.isViewLoaded && self.detailViewController.view.window)
  765. return NO;
  766. // check if metadata is invalidated
  767. if ([[NCManageDatabase sharedInstance] isTableInvalidated:self.metadata]) {
  768. return NO;
  769. }
  770. // ok perform segue
  771. return YES;
  772. }
  773. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  774. {
  775. id controller = segue.destinationViewController;
  776. if ([controller isKindOfClass:[UINavigationController class]]) {
  777. UINavigationController *navigationController = controller;
  778. self.detailViewController = (CCDetail *)navigationController.topViewController;
  779. } else {
  780. self.detailViewController = segue.destinationViewController;
  781. }
  782. NSMutableArray *photoDataSource = [NSMutableArray new];
  783. for (NSString *fileID in sectionDataSource.allFileID) {
  784. tableMetadata *metadata = [sectionDataSource.allRecordsDataSource objectForKey:fileID];
  785. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  786. [photoDataSource addObject:metadata];
  787. }
  788. self.detailViewController.photoDataSource = photoDataSource;
  789. self.detailViewController.metadataDetail = self.metadata;
  790. self.detailViewController.dateFilterQuery = self.metadata.date;
  791. [self.detailViewController setTitle:self.metadata.fileName];
  792. }
  793. @end