NCTrash+CollectionView.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 Realm
  24. import UIKit
  25. // MARK: UICollectionViewDelegate
  26. extension NCTrash: UICollectionViewDelegate {
  27. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  28. let tableTrash = datasource[indexPath.item]
  29. guard !isEditMode else {
  30. if let index = selectOcId.firstIndex(of: tableTrash.fileId) {
  31. selectOcId.remove(at: index)
  32. } else {
  33. selectOcId.append(tableTrash.fileId)
  34. }
  35. collectionView.reloadItems(at: [indexPath])
  36. tabBarSelect.update(selectOcId: selectOcId)
  37. return
  38. }
  39. if tableTrash.directory,
  40. let ncTrash: NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as? NCTrash {
  41. ncTrash.filePath = tableTrash.filePath + tableTrash.fileName
  42. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  43. ncTrash.filename = tableTrash.fileName
  44. self.navigationController?.pushViewController(ncTrash, animated: true)
  45. }
  46. }
  47. }
  48. // MARK: UICollectionViewDataSource
  49. extension NCTrash: UICollectionViewDataSource {
  50. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  51. emptyDataSet?.numberOfItemsInSection(datasource.count, section: section)
  52. return datasource.count
  53. }
  54. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  55. let tableTrash = datasource[indexPath.item]
  56. var image: UIImage?
  57. if tableTrash.iconName.isEmpty {
  58. image = UIImage(named: "file")
  59. } else {
  60. image = UIImage(named: tableTrash.iconName)
  61. }
  62. if FileManager().fileExists(atPath: utilityFileSystem.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName)) {
  63. image = UIImage(contentsOfFile: utilityFileSystem.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName))
  64. } else {
  65. if tableTrash.hasPreview && !utilityFileSystem.fileProviderStoragePreviewIconExists(tableTrash.fileId, etag: tableTrash.fileName) {
  66. downloadThumbnail(with: tableTrash, indexPath: indexPath)
  67. }
  68. }
  69. var cell: NCTrashCellProtocol & UICollectionViewCell
  70. if layoutForView?.layout == NCGlobal.shared.layoutList {
  71. guard let listCell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCTrashListCell else { return NCTrashListCell() }
  72. listCell.listCellDelegate = self
  73. cell = listCell
  74. } else {
  75. guard let gridCell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as? NCTrashGridCell else { return NCTrashGridCell() }
  76. gridCell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCImageCache.images.buttonMore)
  77. gridCell.gridCellDelegate = self
  78. cell = gridCell
  79. }
  80. cell.indexPath = indexPath
  81. cell.setupCellUI(tableTrash: tableTrash, image: image)
  82. cell.selectMode(isEditMode)
  83. if isEditMode {
  84. cell.selected(selectOcId.contains(tableTrash.fileId))
  85. }
  86. return cell
  87. }
  88. func setTextFooter(datasource: [tableTrash]) -> String {
  89. var folders: Int = 0, foldersText = ""
  90. var files: Int = 0, filesText = ""
  91. var size: Int64 = 0
  92. var text = ""
  93. for record: tableTrash in datasource {
  94. if record.directory {
  95. folders += 1
  96. } else {
  97. files += 1
  98. size += record.size
  99. }
  100. }
  101. if folders > 1 {
  102. foldersText = "\(folders) " + NSLocalizedString("_folders_", comment: "")
  103. } else if folders == 1 {
  104. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  105. }
  106. if files > 1 {
  107. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + utilityFileSystem.transformedSize(size)
  108. } else if files == 1 {
  109. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + utilityFileSystem.transformedSize(size)
  110. }
  111. if foldersText.isEmpty {
  112. text = filesText
  113. } else if filesText.isEmpty {
  114. text = foldersText
  115. } else {
  116. text = foldersText + ", " + filesText
  117. }
  118. return text
  119. }
  120. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  121. guard let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as? NCSectionFooter
  122. else { return UICollectionReusableView() }
  123. footer.setTitleLabel(setTextFooter(datasource: datasource))
  124. footer.separatorIsHidden(true)
  125. return footer
  126. }
  127. }
  128. // MARK: UICollectionViewDelegateFlowLayout
  129. extension NCTrash: UICollectionViewDelegateFlowLayout {
  130. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  131. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.endHeightFooter)
  132. }
  133. }