NCTrash+CollectionView.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. return
  36. }
  37. if tableTrash.directory,
  38. let ncTrash: NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as? NCTrash {
  39. ncTrash.trashPath = tableTrash.filePath + tableTrash.fileName
  40. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  41. self.navigationController?.pushViewController(ncTrash, animated: true)
  42. }
  43. }
  44. }
  45. // MARK: UICollectionViewDataSource
  46. extension NCTrash: UICollectionViewDataSource {
  47. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  48. emptyDataSet?.numberOfItemsInSection(datasource.count, section: section)
  49. return datasource.count
  50. }
  51. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  52. let tableTrash = datasource[indexPath.item]
  53. var image: UIImage?
  54. if tableTrash.iconName.isEmpty {
  55. image = UIImage(named: "file")
  56. } else {
  57. image = UIImage(named: tableTrash.iconName)
  58. }
  59. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName)) {
  60. image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName))
  61. } else {
  62. if tableTrash.hasPreview && !CCUtility.fileProviderStoragePreviewIconExists(tableTrash.fileId, etag: tableTrash.fileName) {
  63. downloadThumbnail(with: tableTrash, indexPath: indexPath)
  64. }
  65. }
  66. var cell: NCTrashCellProtocol & UICollectionViewCell
  67. if layoutForView?.layout == NCGlobal.shared.layoutList {
  68. guard let listCell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCTrashListCell else { return UICollectionViewCell() }
  69. listCell.delegate = self
  70. cell = listCell
  71. } else {
  72. // GRID
  73. guard let gridCell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as? NCGridCell else { return UICollectionViewCell() }
  74. gridCell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCBrandColor.cacheImages.buttonMore)
  75. gridCell.delegate = self
  76. cell = gridCell
  77. }
  78. cell.indexPath = indexPath
  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 layoutForView?.layout == NCGlobal.shared.layoutGrid {
  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.setStatusButtonsView(enable: !datasource.isEmpty)
  131. header.setSortedTitle(layoutForView?.titleButtonHeader ?? "")
  132. header.setButtonsView(height: NCGlobal.shared.heightButtonsView)
  133. header.setRichWorkspaceHeight(0)
  134. header.setSectionHeight(0)
  135. header.setViewTransfer(isHidden: true)
  136. return header
  137. } else {
  138. guard let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as? NCSectionFooter
  139. else { return UICollectionReusableView() }
  140. footer.setTitleLabel(setTextFooter(datasource: datasource))
  141. footer.separatorIsHidden(true)
  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: NCGlobal.shared.heightButtonsView)
  150. }
  151. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  152. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.endHeightFooter)
  153. }
  154. }