NCMedia.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. //
  2. // NCMedia.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/02/2019.
  6. // Copyright © 2018 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 Foundation
  24. import Sheeeeeeeeet
  25. class NCMedia: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate, DropdownMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  26. @IBOutlet weak var collectionView: UICollectionView!
  27. @IBOutlet weak var menuButtonMore: UIButton!
  28. @IBOutlet weak var menuButtonSwitch: UIButton!
  29. @IBOutlet weak var menuView: UIView!
  30. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. private var metadataPush: tableMetadata?
  32. private var isEditMode = false
  33. private var selectFileID = [String]()
  34. private var filterTypeFileImage = false;
  35. private var filterTypeFileVideo = false;
  36. private var sectionDatasource = CCSectionDataSourceMetadata()
  37. private var autoUploadFileName = ""
  38. private var autoUploadDirectory = ""
  39. private var gridLayout: NCGridLayout!
  40. private var actionSheet: ActionSheet?
  41. private let headerMenuHeight: CGFloat = 50
  42. private let sectionHeaderHeight: CGFloat = 20
  43. private let footerHeight: CGFloat = 50
  44. private var addWidth: CGFloat = 10
  45. private var readRetry = 0
  46. private var isDistantPast = false
  47. private let refreshControl = UIRefreshControl()
  48. private var loadingSearch = false
  49. required init?(coder aDecoder: NSCoder) {
  50. super.init(coder: aDecoder)
  51. appDelegate.activeMedia = self
  52. }
  53. override func viewDidLoad() {
  54. super.viewDidLoad()
  55. // Cell
  56. collectionView.register(UINib.init(nibName: "NCGridMediaCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  57. // Header
  58. collectionView.register(UINib.init(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
  59. // Footer
  60. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  61. collectionView.alwaysBounceVertical = true
  62. gridLayout = NCGridLayout()
  63. gridLayout.heightLabelPlusButton = 0
  64. gridLayout.preferenceWidth = 80
  65. gridLayout.sectionInset = UIEdgeInsets(top: 10, left: 1, bottom: 10, right: 1)
  66. gridLayout.sectionHeadersPinToVisibleBounds = true
  67. collectionView.collectionViewLayout = gridLayout
  68. // Add Refresh Control
  69. collectionView.refreshControl = refreshControl
  70. // Configure Refresh Control
  71. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  72. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brand
  73. refreshControl.addTarget(self, action: #selector(loadNetworkDatasource), for: .valueChanged)
  74. // empty Data Source
  75. collectionView.emptyDataSetDelegate = self;
  76. collectionView.emptyDataSetSource = self;
  77. }
  78. override func viewWillAppear(_ animated: Bool) {
  79. super.viewWillAppear(animated)
  80. // Color
  81. appDelegate.aspectNavigationControllerBar(self.navigationController?.navigationBar, online: appDelegate.reachability.isReachable(), hidden: false)
  82. appDelegate.aspectTabBar(self.tabBarController?.tabBar, hidden: false)
  83. self.navigationItem.title = NSLocalizedString("_media_", comment: "")
  84. // get auto upload folder
  85. autoUploadFileName = NCManageDatabase.sharedInstance.getAccountAutoUploadFileName()
  86. autoUploadDirectory = NCManageDatabase.sharedInstance.getAccountAutoUploadDirectory(appDelegate.activeUrl)
  87. // clear variable
  88. isDistantPast = false
  89. readRetry = 0
  90. loadNetworkDatasource()
  91. collectionViewReloadDataSource()
  92. }
  93. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  94. super.viewWillTransition(to: size, with: coordinator)
  95. coordinator.animate(alongsideTransition: nil) { _ in
  96. self.collectionView.collectionViewLayout.invalidateLayout()
  97. self.actionSheet?.viewDidLayoutSubviews()
  98. }
  99. }
  100. // MARK: DZNEmpty
  101. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  102. return NCBrandColor.sharedInstance.backgroundView
  103. }
  104. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  105. return CCGraphics.changeThemingColorImage(UIImage.init(named: "mediaNoRecord"), multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
  106. }
  107. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  108. var text = "\n" + NSLocalizedString("_tutorial_photo_view_", comment: "")
  109. if loadingSearch {
  110. text = "\n" + NSLocalizedString("_search_in_progress_", comment: "")
  111. }
  112. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  113. return NSAttributedString.init(string: text, attributes: attributes)
  114. }
  115. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  116. return true
  117. }
  118. // MARK: IBAction
  119. @IBAction func touchUpInsideMenuButtonSwitch(_ sender: Any) {
  120. let itemSizeStart = self.gridLayout.itemSize
  121. UIView.animate(withDuration: 0.0, animations: {
  122. if self.gridLayout.numItems == 1 && self.addWidth > 0 {
  123. self.addWidth = -10
  124. } else if itemSizeStart.width < 50 {
  125. self.addWidth = 10
  126. }
  127. repeat {
  128. self.gridLayout.preferenceWidth = self.gridLayout.preferenceWidth + self.addWidth
  129. } while (self.gridLayout.itemSize == itemSizeStart)
  130. self.collectionView.collectionViewLayout.invalidateLayout()
  131. })
  132. }
  133. @IBAction func touchUpInsideMenuButtonMore(_ sender: Any) {
  134. var menuView: DropdownMenu?
  135. if !isEditMode {
  136. let item0 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "select"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_select_", comment: ""))
  137. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "folderMedia"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_select_media_folder_", comment: ""))
  138. var item2: DropdownItem
  139. if filterTypeFileImage {
  140. item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "imageno"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_media_viewimage_show_", comment: ""))
  141. } else {
  142. item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "imageyes"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_media_viewimage_hide_", comment: ""))
  143. }
  144. var item3: DropdownItem
  145. if filterTypeFileVideo {
  146. item3 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "videono"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_media_viewvideo_show_", comment: ""))
  147. } else {
  148. item3 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "videoyes"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_media_viewvideo_hide_", comment: ""))
  149. }
  150. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item0,item1,item2,item3], selectedRow: -1)
  151. menuView?.token = "menuButtonMore"
  152. } else {
  153. let item0 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "select"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_cancel_", comment: ""))
  154. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_delete_", comment: ""))
  155. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item0, item1], selectedRow: -1)
  156. menuView?.token = "menuButtonMoreSelect"
  157. }
  158. menuView?.delegate = self
  159. menuView?.rowHeight = 45
  160. menuView?.highlightColor = NCBrandColor.sharedInstance.brand
  161. menuView?.tableView.alwaysBounceVertical = false
  162. menuView?.tableViewBackgroundColor = UIColor.white
  163. let header = (sender as? UIButton)?.superview
  164. let headerRect = self.collectionView.convert(header!.bounds, from: self.view)
  165. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  166. menuView?.topOffsetY = CGFloat(menuOffsetY)
  167. menuView?.showMenu()
  168. }
  169. // MARK: DROP-DOWN-MENU
  170. func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
  171. if dropdownMenu.token == "menuButtonMore" {
  172. switch indexPath.row {
  173. case 0:
  174. isEditMode = true
  175. case 1: break
  176. case 2:
  177. filterTypeFileImage = !filterTypeFileImage
  178. collectionViewReloadDataSource()
  179. case 3:
  180. filterTypeFileVideo = !filterTypeFileVideo
  181. collectionViewReloadDataSource()
  182. default: ()
  183. }
  184. }
  185. if dropdownMenu.token == "menuButtonMoreSelect" {
  186. switch indexPath.section {
  187. case 0:
  188. isEditMode = false
  189. selectFileID.removeAll()
  190. collectionView.reloadData()
  191. case 1: break
  192. default: ()
  193. }
  194. }
  195. }
  196. // MARK: NC API
  197. func deleteItem(with metadata: tableMetadata, sender: Any) {
  198. var items = [ActionSheetItem]()
  199. guard let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == serverUrl", appDelegate.activeAccount, metadata.serverUrl)) else {
  200. return
  201. }
  202. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  203. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  204. actionSheet = ActionSheet(items: items) { sheet, item in
  205. if item is ActionSheetDangerButton {
  206. NCMainCommon.sharedInstance.deleteFile(metadatas: [metadata], e2ee: tableDirectory.e2eEncrypted, serverUrl: tableDirectory.serverUrl, folderFileID: tableDirectory.fileID) { (errorCode, message) in
  207. self.collectionViewReloadDataSource()
  208. }
  209. }
  210. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  211. }
  212. let headerView = NCActionSheetHeader.sharedInstance.actionSheetHeader(isDirectory: metadata.directory, iconName: metadata.iconName, fileID: metadata.fileID, fileNameView: metadata.fileNameView, text: metadata.fileNameView)
  213. actionSheet?.headerView = headerView
  214. actionSheet?.headerView?.frame.size.height = 50
  215. actionSheet?.present(in: self, from: sender as! UIButton)
  216. }
  217. func search(lteDate: Date, gteDate: Date, addPast: Bool, setDistantPast: Bool) {
  218. if appDelegate.activeAccount.count == 0 {
  219. return
  220. }
  221. if addPast && isDistantPast {
  222. return
  223. }
  224. if !addPast && loadingSearch {
  225. return
  226. }
  227. if setDistantPast {
  228. isDistantPast = true
  229. }
  230. if addPast {
  231. CCGraphics.addImage(toTitle: NSLocalizedString("_media_", comment: ""), colorTitle: NCBrandColor.sharedInstance.brandText, imageTitle: CCGraphics.changeThemingColorImage(UIImage.init(named: "load"), multiplier: 2, color: NCBrandColor.sharedInstance.brandText), imageRight: false, navigationItem: self.navigationItem)
  232. }
  233. loadingSearch = true
  234. let startDirectory = NCManageDatabase.sharedInstance.getAccountStartDirectoryMediaTabView(CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl))
  235. OCNetworking.sharedManager()?.search(withAccount: appDelegate.activeAccount, fileName: "", serverUrl: startDirectory, contentType: ["image/%", "video/%"], lteDateLastModified: lteDate, gteDateLastModified: gteDate, depth: "infinity", completion: { (account, metadatas, message, errorCode) in
  236. self.loadingSearch = false
  237. self.refreshControl.endRefreshing()
  238. self.navigationItem.titleView = nil
  239. self.navigationItem.title = NSLocalizedString("_media_", comment: "")
  240. if errorCode == 0 && account == self.appDelegate.activeAccount {
  241. var differenceInsert: Int64 = 0
  242. if metadatas != nil && metadatas!.count > 0 {
  243. differenceInsert = NCManageDatabase.sharedInstance.createTablePhotos(metadatas as! [tableMetadata], lteDate: lteDate, gteDate: gteDate, account: account!)
  244. }
  245. print("[LOG] Different Insert \(differenceInsert)]")
  246. if differenceInsert != 0 {
  247. self.readRetry = 0
  248. self.collectionViewReloadDataSource()
  249. }
  250. if differenceInsert == 0 && addPast {
  251. self.readRetry += 1
  252. switch self.readRetry {
  253. case 1:
  254. var gteDate = Calendar.current.date(byAdding: .day, value: -90, to: gteDate)!
  255. gteDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: gteDate)!
  256. self.search(lteDate: lteDate, gteDate: gteDate, addPast: addPast, setDistantPast: false)
  257. print("[LOG] Media search 90 gg]")
  258. case 2:
  259. var gteDate = Calendar.current.date(byAdding: .day, value: -180, to: gteDate)!
  260. gteDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: gteDate)!
  261. self.search(lteDate: lteDate, gteDate: gteDate, addPast: addPast, setDistantPast: false)
  262. print("[LOG] Media search 180 gg]")
  263. case 3:
  264. var gteDate = Calendar.current.date(byAdding: .day, value: -360, to: gteDate)!
  265. gteDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: gteDate)!
  266. self.search(lteDate: lteDate, gteDate: gteDate, addPast: addPast, setDistantPast: false)
  267. print("[LOG] Media search 360 gg]")
  268. default:
  269. self.search(lteDate: lteDate, gteDate: NSDate.distantPast, addPast: addPast, setDistantPast: true)
  270. print("[LOG] Media search distant pass]")
  271. }
  272. }
  273. self.collectionView?.reloadData()
  274. }
  275. })
  276. }
  277. @objc func loadNetworkDatasource() {
  278. if appDelegate.activeAccount.count == 0 {
  279. return
  280. }
  281. if self.sectionDatasource.allRecordsDataSource.count == 0 {
  282. let gteDate = Calendar.current.date(byAdding: .day, value: -30, to: Date())
  283. self.search(lteDate: Date(), gteDate: gteDate!, addPast: true, setDistantPast: false)
  284. } else {
  285. let gteDate = NCManageDatabase.sharedInstance.getTablePhotoDate(account: self.appDelegate.activeAccount, order: .orderedAscending)
  286. self.search(lteDate: Date(), gteDate: gteDate, addPast: false, setDistantPast: false)
  287. }
  288. self.collectionView?.reloadData()
  289. }
  290. // MARK: COLLECTIONVIEW METHODS
  291. func collectionViewReloadDataSource() {
  292. DispatchQueue.global().async {
  293. let metadatas = NCManageDatabase.sharedInstance.getTablePhotos(predicate: NSPredicate(format: "account == %@", self.appDelegate.activeAccount))
  294. self.sectionDatasource = CCSectionMetadata.creataDataSourseSectionMetadata(metadatas, listProgressMetadata: nil, groupByField: "date", filterFileID: nil, filterTypeFileImage: self.filterTypeFileImage, filterTypeFileVideo: self.filterTypeFileVideo, activeAccount: self.appDelegate.activeAccount)
  295. DispatchQueue.main.async {
  296. self.collectionView?.reloadData()
  297. }
  298. }
  299. }
  300. public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  301. //caused by user
  302. selectSearchSections()
  303. }
  304. public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  305. if (!decelerate) {
  306. //cause by user
  307. selectSearchSections()
  308. }
  309. }
  310. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  311. if kind == UICollectionView.elementKindSectionFooter {
  312. let sizeCollection = collectionView.bounds.size.height
  313. let sizeContent = collectionView.contentSize.height
  314. if sizeContent <= sizeCollection {
  315. selectSearchSections()
  316. }
  317. }
  318. if kind == UICollectionView.elementKindSectionHeader {
  319. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
  320. header.backgroundColor = .clear
  321. header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
  322. return header
  323. } else {
  324. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  325. footer.setTitleLabel(sectionDatasource: sectionDatasource)
  326. return footer
  327. }
  328. }
  329. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  330. if section == 0 {
  331. return CGSize(width: collectionView.frame.width, height: headerMenuHeight + sectionHeaderHeight)
  332. } else {
  333. return CGSize(width: collectionView.frame.width, height: sectionHeaderHeight)
  334. }
  335. }
  336. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  337. let sections = sectionDatasource.sectionArrayRow.allKeys.count
  338. if (section == sections - 1) {
  339. return CGSize(width: collectionView.frame.width, height: footerHeight)
  340. } else {
  341. return CGSize(width: collectionView.frame.width, height: 0)
  342. }
  343. }
  344. func numberOfSections(in collectionView: UICollectionView) -> Int {
  345. let sections = sectionDatasource.sectionArrayRow.allKeys.count
  346. return sections
  347. }
  348. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  349. var numberOfItemsInSection: Int = 0
  350. if section < sectionDatasource.sections.count {
  351. let key = sectionDatasource.sections.object(at: section)
  352. let datasource = sectionDatasource.sectionArrayRow.object(forKey: key) as! [tableMetadata]
  353. numberOfItemsInSection = datasource.count
  354. }
  355. return numberOfItemsInSection
  356. }
  357. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  358. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
  359. return collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridMediaCell
  360. }
  361. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridMediaCell
  362. NCMainCommon.sharedInstance.collectionViewCellForItemAt(indexPath, collectionView: collectionView, cell: cell, metadata: metadata, metadataFolder: nil, serverUrl: metadata.serverUrl, isEditMode: isEditMode, selectFileID: selectFileID, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory, hideButtonMore: true, source: self)
  363. return cell
  364. }
  365. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  366. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
  367. return
  368. }
  369. metadataPush = metadata
  370. if isEditMode {
  371. if let index = selectFileID.index(of: metadata.fileID) {
  372. selectFileID.remove(at: index)
  373. } else {
  374. selectFileID.append(metadata.fileID)
  375. }
  376. collectionView.reloadItems(at: [indexPath])
  377. return
  378. }
  379. performSegue(withIdentifier: "segueDetail", sender: self)
  380. }
  381. // MARK: Utility
  382. func selectSearchSections() {
  383. let sections = NSMutableSet()
  384. let lastDate = NCManageDatabase.sharedInstance.getTablePhotoDate(account: self.appDelegate.activeAccount, order: .orderedDescending)
  385. var gteDate: Date?
  386. for item in collectionView.indexPathsForVisibleItems {
  387. if let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(item, sectionDataSource: sectionDatasource) {
  388. if let date = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: metadata.date as Date) {
  389. sections.add(date)
  390. }
  391. }
  392. }
  393. let sortedSections = sections.sorted { (date1, date2) -> Bool in
  394. (date1 as! Date).compare(date2 as! Date) == .orderedDescending
  395. }
  396. if sortedSections.count >= 1 {
  397. let lteDate = Calendar.current.date(byAdding: .day, value: 1, to: sortedSections.first as! Date)!
  398. if lastDate == sortedSections.last as! Date {
  399. gteDate = Calendar.current.date(byAdding: .day, value: -30, to: sortedSections.last as! Date)!
  400. search(lteDate: lteDate, gteDate: gteDate!, addPast: true, setDistantPast: false)
  401. } else {
  402. gteDate = Calendar.current.date(byAdding: .day, value: -1, to: sortedSections.last as! Date)!
  403. search(lteDate: lteDate, gteDate: gteDate!, addPast: false, setDistantPast: false)
  404. }
  405. }
  406. }
  407. // MARK: SEGUE
  408. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  409. let photoDataSource: NSMutableArray = []
  410. for fileID: String in sectionDatasource.allFileID as! [String] {
  411. let metadata = sectionDatasource.allRecordsDataSource.object(forKey: fileID) as! tableMetadata
  412. if metadata.typeFile == k_metadataTypeFile_image {
  413. photoDataSource.add(metadata)
  414. }
  415. }
  416. if let segueNavigationController = segue.destination as? UINavigationController {
  417. if let segueViewController = segueNavigationController.topViewController as? CCDetail {
  418. segueViewController.metadataDetail = metadataPush
  419. segueViewController.dateFilterQuery = nil
  420. segueViewController.photoDataSource = photoDataSource
  421. segueViewController.title = metadataPush!.fileNameView
  422. }
  423. }
  424. }
  425. }