NCTrash+CollectionView.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // NCTrash+CollectionView.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 18.01.22.
  6. // Copyright © 2022 Henrik Storch. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@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. // MARK: UICollectionViewDelegate
  25. extension NCTrash: UICollectionViewDelegate {
  26. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  27. let tableTrash = datasource[indexPath.item]
  28. guard !isEditMode else {
  29. if let index = selectOcId.firstIndex(of: tableTrash.fileId) {
  30. selectOcId.remove(at: index)
  31. } else {
  32. selectOcId.append(tableTrash.fileId)
  33. }
  34. collectionView.reloadItems(at: [indexPath])
  35. self.navigationItem.title = NSLocalizedString("_selected_", comment: "") + " : \(selectOcId.count)" + " / \(datasource.count)"
  36. return
  37. }
  38. if tableTrash.directory,
  39. let ncTrash: NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as? NCTrash {
  40. ncTrash.trashPath = tableTrash.filePath + tableTrash.fileName
  41. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  42. self.navigationController?.pushViewController(ncTrash, animated: true)
  43. }
  44. }
  45. }
  46. // MARK: UICollectionViewDataSource
  47. extension NCTrash: UICollectionViewDataSource {
  48. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  49. emptyDataSet?.numberOfItemsInSection(datasource.count, section: section)
  50. return datasource.count
  51. }
  52. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  53. let tableTrash = datasource[indexPath.item]
  54. var image: UIImage?
  55. if tableTrash.iconName.isEmpty {
  56. image = UIImage(named: "file")
  57. } else {
  58. image = UIImage(named: tableTrash.iconName)
  59. }
  60. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName)) {
  61. image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName))
  62. } else {
  63. if tableTrash.hasPreview && !CCUtility.fileProviderStoragePreviewIconExists(tableTrash.fileId, etag: tableTrash.fileName) {
  64. downloadThumbnail(with: tableTrash, indexPath: indexPath)
  65. }
  66. }
  67. var cell: NCTrashCell & UICollectionViewCell
  68. if collectionView.collectionViewLayout == listLayout {
  69. guard let listCell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCTrashListCell else { return UICollectionViewCell() }
  70. listCell.delegate = self
  71. cell = listCell
  72. } else {
  73. // GRID
  74. guard let gridCell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as? NCGridCell else { return UICollectionViewCell() }
  75. gridCell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCBrandColor.cacheImages.buttonMore)
  76. gridCell.delegate = self
  77. cell = gridCell
  78. }
  79. cell.setupCellUI(tableTrash: tableTrash, image: image)
  80. cell.selectMode(isEditMode)
  81. if isEditMode {
  82. cell.selected(selectOcId.contains(tableTrash.fileId))
  83. }
  84. return cell
  85. }
  86. func setTextFooter(datasource: [tableTrash]) -> String {
  87. var folders: Int = 0, foldersText = ""
  88. var files: Int = 0, filesText = ""
  89. var size: Int64 = 0
  90. var text = ""
  91. for record: tableTrash in datasource {
  92. if record.directory {
  93. folders += 1
  94. } else {
  95. files += 1
  96. size += record.size
  97. }
  98. }
  99. if folders > 1 {
  100. foldersText = "\(folders) " + NSLocalizedString("_folders_", comment: "")
  101. } else if folders == 1 {
  102. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  103. }
  104. if files > 1 {
  105. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + CCUtility.transformedSize(size)
  106. } else if files == 1 {
  107. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + CCUtility.transformedSize(size)
  108. }
  109. if foldersText.isEmpty {
  110. text = filesText
  111. } else if filesText.isEmpty {
  112. text = foldersText
  113. } else {
  114. text = foldersText + ", " + filesText
  115. }
  116. return text
  117. }
  118. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  119. if kind == UICollectionView.elementKindSectionHeader {
  120. guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as? NCSectionHeaderMenu
  121. else { return UICollectionReusableView() }
  122. if collectionView.collectionViewLayout == gridLayout {
  123. header.setImageSwitchList()
  124. header.buttonSwitch.accessibilityLabel = NSLocalizedString("_list_view_", comment: "")
  125. } else {
  126. header.setImageSwitchGrid()
  127. header.buttonSwitch.accessibilityLabel = NSLocalizedString("_grid_view_", comment: "")
  128. }
  129. header.delegate = self
  130. header.setStatusButtonOne(count: datasource.count)
  131. header.setSortedTitle(layoutForView?.titleButtonHeader ?? "")
  132. header.buttonMoreIsHidden(false)
  133. header.setButtonsCommand(heigt: 0)
  134. header.setButtonsView(heigt: heightButtonsView)
  135. header.setRichWorkspaceHeight(0)
  136. header.setSectionHeight(0)
  137. return header
  138. } else {
  139. guard let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as? NCSectionFooter
  140. else { return UICollectionReusableView() }
  141. footer.setTitleLabel(text: setTextFooter(datasource: datasource))
  142. return footer
  143. }
  144. }
  145. }
  146. // MARK: UICollectionViewDelegateFlowLayout
  147. extension NCTrash: UICollectionViewDelegateFlowLayout {
  148. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  149. return CGSize(width: collectionView.frame.width, height: heightButtonsView)
  150. }
  151. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  152. return CGSize(width: collectionView.frame.width, height: footerEndHeight)
  153. }
  154. }