NCTrash+CollectionView.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. return datasource.count
  52. }
  53. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  54. let tableTrash = datasource[indexPath.item]
  55. var image: UIImage?
  56. var cell: NCTrashCellProtocol & UICollectionViewCell
  57. if layoutForView?.layout == NCGlobal.shared.layoutList {
  58. guard let listCell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCTrashListCell else { return NCTrashListCell() }
  59. listCell.delegate = self
  60. cell = listCell
  61. } else {
  62. guard let gridCell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as? NCTrashGridCell else { return NCTrashGridCell() }
  63. gridCell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCImageCache.images.buttonMore)
  64. gridCell.delegate = self
  65. cell = gridCell
  66. }
  67. if tableTrash.iconName.isEmpty {
  68. image = NCImageCache.images.file
  69. } else {
  70. image = NCUtility().loadImage(named: tableTrash.iconName, useTypeIconFile: true)
  71. }
  72. if FileManager().fileExists(atPath: utilityFileSystem.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName)) {
  73. image = UIImage(contentsOfFile: utilityFileSystem.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName))
  74. } else {
  75. if tableTrash.hasPreview && !utilityFileSystem.fileProviderStoragePreviewIconExists(tableTrash.fileId, etag: tableTrash.fileName) {
  76. if NCNetworking.shared.downloadThumbnailTrashQueue.operations.filter({ ($0 as? NCOperationDownloadThumbnailTrash)?.fileId == tableTrash.fileId }).isEmpty {
  77. NCNetworking.shared.downloadThumbnailTrashQueue.addOperation(NCOperationDownloadThumbnailTrash(tableTrash: tableTrash, fileId: tableTrash.fileId, cell: cell, collectionView: collectionView))
  78. }
  79. }
  80. }
  81. cell.indexPath = indexPath
  82. cell.objectId = tableTrash.fileId
  83. cell.setupCellUI(tableTrash: tableTrash, image: image)
  84. cell.selectMode(isEditMode)
  85. if isEditMode {
  86. cell.selected(selectOcId.contains(tableTrash.fileId))
  87. }
  88. return cell
  89. }
  90. func setTextFooter(datasource: [tableTrash]) -> String {
  91. var folders: Int = 0, foldersText = ""
  92. var files: Int = 0, filesText = ""
  93. var size: Int64 = 0
  94. var text = ""
  95. for record: tableTrash in datasource {
  96. if record.directory {
  97. folders += 1
  98. } else {
  99. files += 1
  100. size += record.size
  101. }
  102. }
  103. if folders > 1 {
  104. foldersText = "\(folders) " + NSLocalizedString("_folders_", comment: "")
  105. } else if folders == 1 {
  106. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  107. }
  108. if files > 1 {
  109. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + utilityFileSystem.transformedSize(size)
  110. } else if files == 1 {
  111. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + utilityFileSystem.transformedSize(size)
  112. }
  113. if foldersText.isEmpty {
  114. text = filesText
  115. } else if filesText.isEmpty {
  116. text = foldersText
  117. } else {
  118. text = foldersText + ", " + filesText
  119. }
  120. return text
  121. }
  122. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  123. if kind == UICollectionView.elementKindSectionHeader {
  124. guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderEmptyData", for: indexPath) as? NCSectionHeaderEmptyData
  125. else { return NCSectionHeaderEmptyData() }
  126. header.emptyImage.image = utility.loadImage(named: "trash", colors: [NCBrandColor.shared.brandElement])
  127. header.emptyTitle.text = NSLocalizedString("_trash_no_trash_", comment: "")
  128. header.emptyDescription.text = NSLocalizedString("_trash_no_trash_description_", comment: "")
  129. return header
  130. } else {
  131. guard let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as? NCSectionFooter
  132. else { return NCSectionFooter() }
  133. footer.setTitleLabel(setTextFooter(datasource: datasource))
  134. footer.separatorIsHidden(true)
  135. return footer
  136. }
  137. }
  138. }
  139. // MARK: UICollectionViewDelegateFlowLayout
  140. extension NCTrash: UICollectionViewDelegateFlowLayout {
  141. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  142. var height: Double = 0
  143. if datasource.isEmpty {
  144. height = NCGlobal.shared.getHeightHeaderEmptyData(view: view, portraitOffset: 0, landscapeOffset: 0)
  145. }
  146. return CGSize(width: collectionView.frame.width, height: height)
  147. }
  148. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  149. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.endHeightFooter)
  150. }
  151. }