NCTrash+CollectionView.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.setupCellUI(tableTrash: tableTrash, image: image)
  79. cell.selectMode(isEditMode)
  80. if isEditMode {
  81. cell.selected(selectOcId.contains(tableTrash.fileId))
  82. }
  83. return cell
  84. }
  85. func setTextFooter(datasource: [tableTrash]) -> String {
  86. var folders: Int = 0, foldersText = ""
  87. var files: Int = 0, filesText = ""
  88. var size: Int64 = 0
  89. var text = ""
  90. for record: tableTrash in datasource {
  91. if record.directory {
  92. folders += 1
  93. } else {
  94. files += 1
  95. size += record.size
  96. }
  97. }
  98. if folders > 1 {
  99. foldersText = "\(folders) " + NSLocalizedString("_folders_", comment: "")
  100. } else if folders == 1 {
  101. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  102. }
  103. if files > 1 {
  104. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + CCUtility.transformedSize(size)
  105. } else if files == 1 {
  106. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + CCUtility.transformedSize(size)
  107. }
  108. if foldersText.isEmpty {
  109. text = filesText
  110. } else if filesText.isEmpty {
  111. text = foldersText
  112. } else {
  113. text = foldersText + ", " + filesText
  114. }
  115. return text
  116. }
  117. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  118. if kind == UICollectionView.elementKindSectionHeader {
  119. guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as? NCSectionHeaderMenu
  120. else { return UICollectionReusableView() }
  121. if layoutForView?.layout == NCGlobal.shared.layoutGrid {
  122. header.setImageSwitchList()
  123. header.buttonSwitch.accessibilityLabel = NSLocalizedString("_list_view_", comment: "")
  124. } else {
  125. header.setImageSwitchGrid()
  126. header.buttonSwitch.accessibilityLabel = NSLocalizedString("_grid_view_", comment: "")
  127. }
  128. header.delegate = self
  129. header.setStatusButtonsView(enable: !datasource.isEmpty)
  130. header.setSortedTitle(layoutForView?.titleButtonHeader ?? "")
  131. header.setButtonsView(height: NCGlobal.shared.heightButtonsView)
  132. header.setRichWorkspaceHeight(0)
  133. header.setSectionHeight(0)
  134. header.setViewTransfer(isHidden: true)
  135. return header
  136. } else {
  137. guard let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as? NCSectionFooter
  138. else { return UICollectionReusableView() }
  139. footer.setTitleLabel(setTextFooter(datasource: datasource))
  140. footer.separatorIsHidden(true)
  141. return footer
  142. }
  143. }
  144. }
  145. // MARK: UICollectionViewDelegateFlowLayout
  146. extension NCTrash: UICollectionViewDelegateFlowLayout {
  147. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  148. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.heightButtonsView)
  149. }
  150. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  151. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.endHeightFooter)
  152. }
  153. }