NCMedia.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, DropdownMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  26. @IBOutlet fileprivate weak var collectionView: UICollectionView!
  27. var titleCurrentFolder = NSLocalizedString("_manage_file_offline_", comment: "")
  28. var serverUrl = ""
  29. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. private var metadataPush: tableMetadata?
  31. private var isEditMode = false
  32. private var selectFileID = [String]()
  33. private var sectionDatasource = CCSectionDataSourceMetadata()
  34. private var typeLayout = ""
  35. private var datasourceSorted = ""
  36. private var datasourceAscending = true
  37. private var datasourceGroupBy = ""
  38. private var datasourceDirectoryOnTop = false
  39. private var autoUploadFileName = ""
  40. private var autoUploadDirectory = ""
  41. private var listLayout: NCListLayout!
  42. private var gridLayout: NCGridLayout!
  43. private var actionSheet: ActionSheet?
  44. private let headerMenuHeight: CGFloat = 50
  45. private let sectionHeaderHeight: CGFloat = 20
  46. private let footerHeight: CGFloat = 50
  47. private let refreshControl = UIRefreshControl()
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. // Cell
  51. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  52. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  53. // Header
  54. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  55. collectionView.register(UINib.init(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
  56. // Footer
  57. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  58. collectionView.alwaysBounceVertical = true
  59. listLayout = NCListLayout()
  60. gridLayout = NCGridLayout()
  61. // Add Refresh Control
  62. if #available(iOS 10.0, *) {
  63. collectionView.refreshControl = refreshControl
  64. } else {
  65. collectionView.addSubview(refreshControl)
  66. }
  67. // Configure Refresh Control
  68. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  69. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brand
  70. refreshControl.addTarget(self, action: #selector(loadDatasource), for: .valueChanged)
  71. // empty Data Source
  72. self.collectionView.emptyDataSetDelegate = self;
  73. self.collectionView.emptyDataSetSource = self;
  74. }
  75. override func viewWillAppear(_ animated: Bool) {
  76. super.viewWillAppear(animated)
  77. // Color
  78. appDelegate.aspectNavigationControllerBar(self.navigationController?.navigationBar, online: appDelegate.reachability.isReachable(), hidden: false)
  79. appDelegate.aspectTabBar(self.tabBarController?.tabBar, hidden: false)
  80. self.navigationItem.title = titleCurrentFolder
  81. (typeLayout, datasourceSorted, datasourceAscending, datasourceGroupBy, datasourceDirectoryOnTop) = NCUtility.sharedInstance.getLayoutForView(key: k_layout_view_offline)
  82. // get auto upload folder
  83. autoUploadFileName = NCManageDatabase.sharedInstance.getAccountAutoUploadFileName()
  84. autoUploadDirectory = NCManageDatabase.sharedInstance.getAccountAutoUploadDirectory(appDelegate.activeUrl)
  85. if typeLayout == k_layout_list {
  86. collectionView.collectionViewLayout = listLayout
  87. } else {
  88. collectionView.collectionViewLayout = gridLayout
  89. }
  90. loadDatasource()
  91. }
  92. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  93. super.viewWillTransition(to: size, with: coordinator)
  94. coordinator.animate(alongsideTransition: nil) { _ in
  95. self.collectionView.collectionViewLayout.invalidateLayout()
  96. self.actionSheet?.viewDidLayoutSubviews()
  97. }
  98. }
  99. // MARK: DZNEmpty
  100. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  101. return NCBrandColor.sharedInstance.backgroundView
  102. }
  103. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  104. return CCGraphics.changeThemingColorImage(UIImage.init(named: "filesNoFiles"), multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
  105. }
  106. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  107. let text = "\n"+NSLocalizedString("_files_no_files_", comment: "")
  108. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  109. return NSAttributedString.init(string: text, attributes: attributes)
  110. }
  111. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  112. return true
  113. }
  114. // MARK: TAP EVENT
  115. func tapSwitchHeader(sender: Any) {
  116. if collectionView.collectionViewLayout == gridLayout {
  117. // list layout
  118. UIView.animate(withDuration: 0.0, animations: {
  119. self.collectionView.collectionViewLayout.invalidateLayout()
  120. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  121. self.collectionView.reloadData()
  122. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  123. })
  124. })
  125. typeLayout = k_layout_list
  126. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_offline, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  127. } else {
  128. // grid layout
  129. UIView.animate(withDuration: 0.0, animations: {
  130. self.collectionView.collectionViewLayout.invalidateLayout()
  131. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  132. self.collectionView.reloadData()
  133. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  134. })
  135. })
  136. typeLayout = k_layout_grid
  137. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_offline, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  138. }
  139. }
  140. func tapOrderHeader(sender: Any) {
  141. var menuView: DropdownMenu?
  142. var selectedIndexPath = [IndexPath()]
  143. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameAZ"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_a_z_", comment: ""))
  144. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameZA"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_z_a_", comment: ""))
  145. let item3 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateMoreRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_more_recent_", comment: ""))
  146. let item4 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateLessRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_less_recent_", comment: ""))
  147. let item5 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortSmallest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_smallest_", comment: ""))
  148. let item6 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortLargest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_largest_", comment: ""))
  149. switch datasourceSorted {
  150. case "fileName":
  151. if datasourceAscending == true { item1.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 0)) }
  152. if datasourceAscending == false { item2.style = .highlight; selectedIndexPath.append(IndexPath(row: 1, section: 0)) }
  153. case "date":
  154. if datasourceAscending == false { item3.style = .highlight; selectedIndexPath.append(IndexPath(row: 2, section: 0)) }
  155. if datasourceAscending == true { item4.style = .highlight; selectedIndexPath.append(IndexPath(row: 3, section: 0)) }
  156. case "size":
  157. if datasourceAscending == true { item5.style = .highlight; selectedIndexPath.append(IndexPath(row: 4, section: 0)) }
  158. if datasourceAscending == false { item6.style = .highlight; selectedIndexPath.append(IndexPath(row: 5, section: 0)) }
  159. default:
  160. ()
  161. }
  162. let item7 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByAlphabetic"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_alphabetic_no_", comment: ""))
  163. let item8 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByFile"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_typefile_no_", comment: ""))
  164. let item9 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByDate"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_date_no_", comment: ""))
  165. switch datasourceGroupBy {
  166. case "alphabetic":
  167. item7.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 1))
  168. case "typefile":
  169. item8.style = .highlight; selectedIndexPath.append(IndexPath(row: 1, section: 1))
  170. case "date":
  171. item9.style = .highlight; selectedIndexPath.append(IndexPath(row: 2, section: 1))
  172. default:
  173. ()
  174. }
  175. let item10 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "foldersOnTop"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_directory_on_top_no_", comment: ""))
  176. if datasourceDirectoryOnTop {
  177. item10.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 2))
  178. }
  179. let sectionOrder = DropdownSection(sectionIdentifier: "", items: [item1, item2, item3, item4, item5, item6])
  180. let sectionGroupBy = DropdownSection(sectionIdentifier: "", items: [item7, item8, item9])
  181. let sectionFolderOnTop = DropdownSection(sectionIdentifier: "", items: [item10])
  182. menuView = DropdownMenu(navigationController: self.navigationController!, sections: [sectionOrder, sectionGroupBy, sectionFolderOnTop], selectedIndexPath: selectedIndexPath)
  183. menuView?.token = "tapOrderHeaderMenu"
  184. menuView?.delegate = self
  185. menuView?.rowHeight = 45
  186. menuView?.sectionHeaderHeight = 8
  187. menuView?.highlightColor = NCBrandColor.sharedInstance.brand
  188. menuView?.tableView.alwaysBounceVertical = false
  189. menuView?.tableViewBackgroundColor = UIColor.white
  190. let header = (sender as? UIButton)?.superview
  191. let headerRect = self.collectionView.convert(header!.bounds, from: self.view)
  192. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  193. menuView?.topOffsetY = CGFloat(menuOffsetY)
  194. menuView?.showMenu()
  195. }
  196. func tapMoreHeader(sender: Any) {
  197. }
  198. func tapMoreListItem(with fileID: String, sender: Any) {
  199. tapMoreGridItem(with: fileID, sender: sender)
  200. }
  201. func tapMoreGridItem(with fileID: String, sender: Any) {
  202. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID == %@", fileID)) else {
  203. return
  204. }
  205. if !isEditMode {
  206. var items = [ActionSheetItem]()
  207. let appearanceDelete = ActionSheetItemAppearance.init()
  208. appearanceDelete.textColor = UIColor.red
  209. // 0 == CCMore, 1 = first NCOffline ....
  210. if (self == self.navigationController?.viewControllers[1]) {
  211. items.append(ActionSheetItem(title: NSLocalizedString("_remove_available_offline_", comment: ""), value: 0, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "offline"), multiplier: 2, color: NCBrandColor.sharedInstance.icon)))
  212. }
  213. items.append(ActionSheetItem(title: NSLocalizedString("_share_", comment: ""), value: 1, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), multiplier: 2, color: NCBrandColor.sharedInstance.icon)))
  214. let itemDelete = ActionSheetItem(title: NSLocalizedString("_delete_", comment: ""), value: 2, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: UIColor.red))
  215. itemDelete.customAppearance = appearanceDelete
  216. items.append(itemDelete)
  217. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  218. actionSheet = ActionSheet(items: items) { sheet, item in
  219. if item.value as? Int == 0 {
  220. if metadata.directory {
  221. NCManageDatabase.sharedInstance.setDirectory(serverUrl: CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!, offline: false, account: self.appDelegate.activeAccount)
  222. } else {
  223. NCManageDatabase.sharedInstance.setLocalFile(fileID: metadata.fileID, offline: false)
  224. }
  225. self.loadDatasource()
  226. }
  227. if item.value as? Int == 1 { self.appDelegate.activeMain.readShare(withAccount: self.appDelegate.activeAccount, openWindow: true, metadata: metadata) }
  228. if item.value as? Int == 2 { self.deleteItem(with: metadata, sender: sender) }
  229. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  230. }
  231. let headerView = NCActionSheetHeader.sharedInstance.actionSheetHeader(isDirectory: metadata.directory, iconName: metadata.iconName, fileID: metadata.fileID, fileNameView: metadata.fileNameView, text: metadata.fileNameView)
  232. actionSheet?.headerView = headerView
  233. actionSheet?.headerView?.frame.size.height = 50
  234. actionSheet?.present(in: self, from: sender as! UIButton)
  235. } else {
  236. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  237. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  238. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  239. }
  240. }
  241. // MARK: DROP-DOWN-MENU
  242. func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
  243. if dropdownMenu.token == "tapOrderHeaderMenu" {
  244. switch indexPath.section {
  245. case 0: switch indexPath.row {
  246. case 0: datasourceSorted = "fileName"; datasourceAscending = true
  247. case 1: datasourceSorted = "fileName"; datasourceAscending = false
  248. case 2: datasourceSorted = "date"; datasourceAscending = false
  249. case 3: datasourceSorted = "date"; datasourceAscending = true
  250. case 4: datasourceSorted = "size"; datasourceAscending = true
  251. case 5: datasourceSorted = "size"; datasourceAscending = false
  252. default: ()
  253. }
  254. case 1: switch indexPath.row {
  255. case 0:
  256. if datasourceGroupBy == "alphabetic" {
  257. datasourceGroupBy = "none"
  258. } else {
  259. datasourceGroupBy = "alphabetic"
  260. }
  261. case 1:
  262. if datasourceGroupBy == "typefile" {
  263. datasourceGroupBy = "none"
  264. } else {
  265. datasourceGroupBy = "typefile"
  266. }
  267. case 2:
  268. if datasourceGroupBy == "date" {
  269. datasourceGroupBy = "none"
  270. } else {
  271. datasourceGroupBy = "date"
  272. }
  273. default: ()
  274. }
  275. case 2:
  276. if datasourceDirectoryOnTop {
  277. datasourceDirectoryOnTop = false
  278. } else {
  279. datasourceDirectoryOnTop = true
  280. }
  281. default: ()
  282. }
  283. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_offline, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  284. loadDatasource()
  285. }
  286. if dropdownMenu.token == "tapMoreHeaderMenu" {
  287. }
  288. if dropdownMenu.token == "tapMoreHeaderMenuSelect" {
  289. }
  290. }
  291. // MARK: NC API
  292. func deleteItem(with metadata: tableMetadata, sender: Any) {
  293. var items = [ActionSheetItem]()
  294. guard let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == serverUrl", appDelegate.activeAccount, metadata.serverUrl)) else {
  295. return
  296. }
  297. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  298. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  299. actionSheet = ActionSheet(items: items) { sheet, item in
  300. if item is ActionSheetDangerButton {
  301. NCMainCommon.sharedInstance.deleteFile(metadatas: [metadata], e2ee: tableDirectory.e2eEncrypted, serverUrl: tableDirectory.serverUrl, folderFileID: tableDirectory.fileID) { (errorCode, message) in
  302. self.loadDatasource()
  303. }
  304. }
  305. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  306. }
  307. let headerView = NCActionSheetHeader.sharedInstance.actionSheetHeader(isDirectory: metadata.directory, iconName: metadata.iconName, fileID: metadata.fileID, fileNameView: metadata.fileNameView, text: metadata.fileNameView)
  308. actionSheet?.headerView = headerView
  309. actionSheet?.headerView?.frame.size.height = 50
  310. actionSheet?.present(in: self, from: sender as! UIButton)
  311. }
  312. // MARK: DATASOURCE
  313. @objc func loadDatasource() {
  314. var fileIDs = [String]()
  315. sectionDatasource = CCSectionDataSourceMetadata()
  316. if serverUrl == "" {
  317. if let directories = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "serverUrl", ascending: true) {
  318. for directory: tableDirectory in directories {
  319. fileIDs.append(directory.fileID)
  320. }
  321. }
  322. if let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "fileName", ascending: true) {
  323. for file: tableLocalFile in files {
  324. fileIDs.append(file.fileID)
  325. }
  326. }
  327. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND fileID IN %@", appDelegate.activeAccount, fileIDs), sorted: datasourceSorted, ascending: datasourceAscending) {
  328. sectionDatasource = CCSectionMetadata.creataDataSourseSectionMetadata(metadatas, listProgressMetadata: nil, groupByField: datasourceGroupBy, filterFileID: nil, filterTypeFileImage: false, filterTypeFileVideo: false, activeAccount: appDelegate.activeAccount)
  329. }
  330. } else {
  331. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.activeAccount, serverUrl), sorted: datasourceSorted, ascending: datasourceAscending) {
  332. sectionDatasource = CCSectionMetadata.creataDataSourseSectionMetadata(metadatas, listProgressMetadata: nil, groupByField: datasourceGroupBy, filterFileID: nil, filterTypeFileImage: false, filterTypeFileVideo: false, activeAccount: appDelegate.activeAccount)
  333. }
  334. }
  335. self.refreshControl.endRefreshing()
  336. collectionView.reloadData()
  337. }
  338. // MARK: COLLECTIONVIEW METHODS
  339. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  340. if (indexPath.section == 0) {
  341. if kind == UICollectionView.elementKindSectionHeader {
  342. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  343. if collectionView.collectionViewLayout == gridLayout {
  344. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  345. } else {
  346. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  347. }
  348. header.delegate = self
  349. header.setStatusButton(count: sectionDatasource.allFileID.count)
  350. header.setTitleOrder(datasourceSorted: datasourceSorted, datasourceAscending: datasourceAscending)
  351. if datasourceGroupBy == "none" {
  352. header.labelSection.isHidden = true
  353. header.labelSectionHeightConstraint.constant = 0
  354. } else {
  355. header.labelSection.isHidden = false
  356. header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
  357. header.labelSectionHeightConstraint.constant = sectionHeaderHeight
  358. }
  359. return header
  360. } else {
  361. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  362. footer.setTitleLabel(sectionDatasource: sectionDatasource)
  363. return footer
  364. }
  365. } else {
  366. if kind == UICollectionView.elementKindSectionHeader {
  367. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
  368. header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
  369. return header
  370. } else {
  371. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  372. footer.setTitleLabel(sectionDatasource: sectionDatasource)
  373. return footer
  374. }
  375. }
  376. }
  377. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  378. if section == 0 {
  379. if datasourceGroupBy == "none" {
  380. return CGSize(width: collectionView.frame.width, height: headerMenuHeight)
  381. } else {
  382. return CGSize(width: collectionView.frame.width, height: headerMenuHeight + sectionHeaderHeight)
  383. }
  384. } else {
  385. return CGSize(width: collectionView.frame.width, height: sectionHeaderHeight)
  386. }
  387. }
  388. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  389. let sections = sectionDatasource.sectionArrayRow.allKeys.count
  390. if (section == sections - 1) {
  391. return CGSize(width: collectionView.frame.width, height: footerHeight)
  392. } else {
  393. return CGSize(width: collectionView.frame.width, height: 0)
  394. }
  395. }
  396. func numberOfSections(in collectionView: UICollectionView) -> Int {
  397. let sections = sectionDatasource.sectionArrayRow.allKeys.count
  398. return sections
  399. }
  400. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  401. let key = sectionDatasource.sections.object(at: section)
  402. let datasource = sectionDatasource.sectionArrayRow.object(forKey: key) as! [tableMetadata]
  403. return datasource.count
  404. }
  405. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  406. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
  407. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  408. }
  409. let cell = NCMainCommon.sharedInstance.collectionViewCellForItemAt(indexPath, collectionView: collectionView, typeLayout: typeLayout, metadata: metadata, metadataFolder: nil, serverUrl: metadata.serverUrl, isEditMode: isEditMode, selectFileID: selectFileID, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory, hideButtonMore: false, source: self)
  410. return cell
  411. }
  412. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  413. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
  414. return
  415. }
  416. metadataPush = metadata
  417. if isEditMode {
  418. if let index = selectFileID.index(of: metadata.fileID) {
  419. selectFileID.remove(at: index)
  420. } else {
  421. selectFileID.append(metadata.fileID)
  422. }
  423. collectionView.reloadItems(at: [indexPath])
  424. return
  425. }
  426. performSegue(withIdentifier: "segueDetail", sender: self)
  427. }
  428. // MARK: SEGUE
  429. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  430. let photoDataSource: NSMutableArray = []
  431. for fileID: String in sectionDatasource.allFileID as! [String] {
  432. let metadata = sectionDatasource.allRecordsDataSource.object(forKey: fileID) as! tableMetadata
  433. if metadata.typeFile == k_metadataTypeFile_image {
  434. photoDataSource.add(metadata)
  435. }
  436. }
  437. if let segueNavigationController = segue.destination as? UINavigationController {
  438. if let segueViewController = segueNavigationController.topViewController as? CCDetail {
  439. segueViewController.metadataDetail = metadataPush
  440. segueViewController.dateFilterQuery = nil
  441. segueViewController.photoDataSource = photoDataSource
  442. segueViewController.title = metadataPush!.fileNameView
  443. }
  444. }
  445. }
  446. }