NCTrash+CollectionView.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. self.navigationItem.title = NSLocalizedString("_selected_", comment: "") + " : \(selectOcId.count)" + " / \(datasource.count)"
  36. return
  37. }
  38. if tableTrash.directory,
  39. let ncTrash: NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as? NCTrash {
  40. ncTrash.trashPath = tableTrash.filePath + tableTrash.fileName
  41. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  42. self.navigationController?.pushViewController(ncTrash, animated: true)
  43. }
  44. }
  45. }
  46. // MARK: UICollectionViewDataSource
  47. extension NCTrash: UICollectionViewDataSource {
  48. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  49. if kind == UICollectionView.elementKindSectionHeader {
  50. guard let trashHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as? NCTrashSectionHeaderMenu
  51. else { return UICollectionReusableView() }
  52. if collectionView.collectionViewLayout == gridLayout {
  53. trashHeader.buttonSwitch.setImage(UIImage(named: "switchList")?.image(color: NCBrandColor.shared.gray, size: 25), for: .normal)
  54. } else {
  55. trashHeader.buttonSwitch.setImage(UIImage(named: "switchGrid")?.image(color: NCBrandColor.shared.gray, size: 25), for: .normal)
  56. }
  57. trashHeader.delegate = self
  58. trashHeader.backgroundColor = NCBrandColor.shared.systemBackground
  59. trashHeader.separator.backgroundColor = NCBrandColor.shared.separator
  60. trashHeader.setStatusButton(datasource: datasource)
  61. trashHeader.setTitleSorted(datasourceTitleButton: layoutForView?.titleButtonHeader ?? "")
  62. return trashHeader
  63. } else {
  64. guard let trashFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as? NCTrashSectionFooter
  65. else { return UICollectionReusableView() }
  66. trashFooter.setTitleLabelFooter(datasource: datasource)
  67. return trashFooter
  68. }
  69. }
  70. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  71. emptyDataSet?.numberOfItemsInSection(datasource.count, section: section)
  72. return datasource.count
  73. }
  74. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  75. let tableTrash = datasource[indexPath.item]
  76. var image: UIImage?
  77. if tableTrash.iconName.isEmpty {
  78. image = UIImage(named: "file")
  79. } else {
  80. image = UIImage(named: tableTrash.iconName)
  81. }
  82. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName)) {
  83. image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName))
  84. } else {
  85. if tableTrash.hasPreview && !CCUtility.fileProviderStoragePreviewIconExists(tableTrash.fileId, etag: tableTrash.fileName) {
  86. downloadThumbnail(with: tableTrash, indexPath: indexPath)
  87. }
  88. }
  89. var cell: NCTrashCell & UICollectionViewCell
  90. if collectionView.collectionViewLayout == listLayout {
  91. guard let listCell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCTrashListCell else { return UICollectionViewCell() }
  92. cell = listCell
  93. } else {
  94. // GRID
  95. guard let gridCell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as? NCGridCell else { return UICollectionViewCell() }
  96. gridCell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCBrandColor.cacheImages.buttonMore)
  97. cell = gridCell
  98. }
  99. cell.setupCellUI(tableTrash: tableTrash, image: image)
  100. cell.selectMode(isEditMode)
  101. if isEditMode {
  102. cell.selected(selectOcId.contains(tableTrash.fileId))
  103. }
  104. return cell
  105. }
  106. }
  107. // MARK: UICollectionViewDelegateFlowLayout
  108. extension NCTrash: UICollectionViewDelegateFlowLayout {
  109. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  110. return CGSize(width: collectionView.frame.width, height: highHeader)
  111. }
  112. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  113. return CGSize(width: collectionView.frame.width, height: highHeader)
  114. }
  115. }