NCTrash+CollectionView.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 UIKit
  24. import RealmSwift
  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, account: appDelegate.account, 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.selected(selectOcId.contains(tableTrash.fileId), isEditMode: isEditMode)
  85. return cell
  86. }
  87. func setTextFooter(datasource: [tableTrash]) -> String {
  88. var folders: Int = 0, foldersText = ""
  89. var files: Int = 0, filesText = ""
  90. var size: Int64 = 0
  91. var text = ""
  92. for record: tableTrash in datasource {
  93. if record.directory {
  94. folders += 1
  95. } else {
  96. files += 1
  97. size += record.size
  98. }
  99. }
  100. if folders > 1 {
  101. foldersText = "\(folders) " + NSLocalizedString("_folders_", comment: "")
  102. } else if folders == 1 {
  103. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  104. }
  105. if files > 1 {
  106. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + utilityFileSystem.transformedSize(size)
  107. } else if files == 1 {
  108. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + utilityFileSystem.transformedSize(size)
  109. }
  110. if foldersText.isEmpty {
  111. text = filesText
  112. } else if filesText.isEmpty {
  113. text = foldersText
  114. } else {
  115. text = foldersText + ", " + filesText
  116. }
  117. return text
  118. }
  119. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  120. if kind == UICollectionView.elementKindSectionHeader {
  121. guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFirstHeaderEmptyData", for: indexPath) as? NCSectionFirstHeaderEmptyData
  122. else { return NCSectionFirstHeaderEmptyData() }
  123. header.emptyImage.image = utility.loadImage(named: "trash", colors: [NCBrandColor.shared.brandElement])
  124. header.emptyTitle.text = NSLocalizedString("_trash_no_trash_", comment: "")
  125. header.emptyDescription.text = NSLocalizedString("_trash_no_trash_description_", comment: "")
  126. return header
  127. } else {
  128. guard let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as? NCSectionFooter
  129. else { return NCSectionFooter() }
  130. footer.setTitleLabel(setTextFooter(datasource: datasource))
  131. footer.separatorIsHidden(true)
  132. return footer
  133. }
  134. }
  135. }
  136. // MARK: UICollectionViewDelegateFlowLayout
  137. extension NCTrash: UICollectionViewDelegateFlowLayout {
  138. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  139. var height: Double = 0
  140. if datasource.isEmpty {
  141. height = NCGlobal.shared.getHeightHeaderEmptyData(view: view, portraitOffset: 0, landscapeOffset: 0)
  142. }
  143. return CGSize(width: collectionView.frame.width, height: height)
  144. }
  145. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  146. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.endHeightFooter)
  147. }
  148. }