NCTrash+CollectionView.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. guard let resultTableTrash = datasource?[indexPath.item] else { return }
  29. guard !isEditMode else {
  30. if let index = selectOcId.firstIndex(of: resultTableTrash.fileId) {
  31. selectOcId.remove(at: index)
  32. } else {
  33. selectOcId.append(resultTableTrash.fileId)
  34. }
  35. collectionView.reloadItems(at: [indexPath])
  36. tabBarSelect.update(selectOcId: selectOcId)
  37. return
  38. }
  39. if resultTableTrash.directory,
  40. let ncTrash: NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as? NCTrash {
  41. ncTrash.filePath = resultTableTrash.filePath + resultTableTrash.fileName
  42. ncTrash.titleCurrentFolder = resultTableTrash.trashbinFileName
  43. ncTrash.filename = resultTableTrash.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 ?? 0
  52. }
  53. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  54. var image: UIImage?
  55. var cell: NCTrashCellProtocol & UICollectionViewCell
  56. if layoutForView?.layout == NCGlobal.shared.layoutList {
  57. let listCell = (collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCTrashListCell)!
  58. listCell.delegate = self
  59. cell = listCell
  60. } else {
  61. let gridCell = (collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as? NCTrashGridCell)!
  62. gridCell.setButtonMore(image: NCImageCache.shared.getImageButtonMore())
  63. gridCell.delegate = self
  64. cell = gridCell
  65. }
  66. guard let resultTableTrash = datasource?[indexPath.item] else { return cell }
  67. cell.imageItem.contentMode = .scaleAspectFit
  68. if resultTableTrash.iconName.isEmpty {
  69. image = NCImageCache.shared.getImageFile()
  70. } else {
  71. image = NCUtility().loadImage(named: resultTableTrash.iconName, useTypeIconFile: true, account: resultTableTrash.account)
  72. }
  73. if let imageIcon = utility.getImage(ocId: resultTableTrash.fileId, etag: resultTableTrash.fileName, ext: NCGlobal.shared.previewExt512) {
  74. image = imageIcon
  75. cell.imageItem.contentMode = .scaleAspectFill
  76. } else {
  77. if resultTableTrash.hasPreview {
  78. if NCNetworking.shared.downloadThumbnailTrashQueue.operations.filter({ ($0 as? NCOperationDownloadThumbnailTrash)?.fileId == resultTableTrash.fileId }).isEmpty {
  79. NCNetworking.shared.downloadThumbnailTrashQueue.addOperation(NCOperationDownloadThumbnailTrash(fileId: resultTableTrash.fileId, fileName: resultTableTrash.fileName, account: session.account, collectionView: collectionView))
  80. }
  81. }
  82. }
  83. cell.account = resultTableTrash.account
  84. cell.objectId = resultTableTrash.fileId
  85. cell.setupCellUI(tableTrash: resultTableTrash, image: image)
  86. cell.selected(selectOcId.contains(resultTableTrash.fileId), isEditMode: isEditMode, account: resultTableTrash.account)
  87. return cell
  88. }
  89. func setTextFooter(datasource: Results<tableTrash>) -> String {
  90. var folders: Int = 0, foldersText = ""
  91. var files: Int = 0, filesText = ""
  92. var size: Int64 = 0
  93. var text = ""
  94. for record: tableTrash in datasource {
  95. if record.directory {
  96. folders += 1
  97. } else {
  98. files += 1
  99. size += record.size
  100. }
  101. }
  102. if folders > 1 {
  103. foldersText = "\(folders) " + NSLocalizedString("_folders_", comment: "")
  104. } else if folders == 1 {
  105. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  106. }
  107. if files > 1 {
  108. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + utilityFileSystem.transformedSize(size)
  109. } else if files == 1 {
  110. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + utilityFileSystem.transformedSize(size)
  111. }
  112. if foldersText.isEmpty {
  113. text = filesText
  114. } else if filesText.isEmpty {
  115. text = foldersText
  116. } else {
  117. text = foldersText + ", " + filesText
  118. }
  119. return text
  120. }
  121. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  122. if kind == UICollectionView.elementKindSectionHeader {
  123. guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFirstHeaderEmptyData", for: indexPath) as? NCSectionFirstHeaderEmptyData
  124. else { return NCSectionFirstHeaderEmptyData() }
  125. header.emptyImage.image = utility.loadImage(named: "trash", colors: [NCBrandColor.shared.getElement(account: session.account)])
  126. header.emptyTitle.text = NSLocalizedString("_trash_no_trash_", comment: "")
  127. header.emptyDescription.text = NSLocalizedString("_trash_no_trash_description_", comment: "")
  128. return header
  129. } else {
  130. guard let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as? NCSectionFooter
  131. else { return NCSectionFooter() }
  132. if let datasource {
  133. footer.setTitleLabel(setTextFooter(datasource: datasource))
  134. footer.separatorIsHidden(true)
  135. }
  136. return footer
  137. }
  138. }
  139. }
  140. // MARK: UICollectionViewDelegateFlowLayout
  141. extension NCTrash: UICollectionViewDelegateFlowLayout {
  142. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  143. var height: Double = 0
  144. if let datasource, datasource.isEmpty {
  145. height = utility.getHeightHeaderEmptyData(view: view, portraitOffset: 0, landscapeOffset: 0)
  146. }
  147. return CGSize(width: collectionView.frame.width, height: height)
  148. }
  149. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  150. return CGSize(width: collectionView.frame.width, height: NCGlobal.shared.endHeightFooter)
  151. }
  152. }