NCTrash+Networking.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // NCTrash+Networking.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 18/03/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. import UIKit
  22. import NextcloudKit
  23. import Queuer
  24. import RealmSwift
  25. extension NCTrash {
  26. @objc func loadListingTrash() {
  27. NextcloudKit.shared.listingTrash(filename: filename, showHiddenFiles: false, account: session.account) { task in
  28. self.dataSourceTask = task
  29. self.collectionView.reloadData()
  30. } completion: { account, items, _, error in
  31. self.refreshControl.endRefreshing()
  32. if let items {
  33. self.database.deleteTrash(filePath: self.getFilePath(), account: account)
  34. self.database.addTrash(account: account, items: items)
  35. }
  36. self.reloadDataSource()
  37. if error != .success {
  38. NCContentPresenter().showError(error: error)
  39. }
  40. }
  41. }
  42. func restoreItem(with fileId: String) {
  43. guard let resultTableTrash = self.database.getResultTrashItem(fileId: fileId, account: session.account) else { return }
  44. let fileNameFrom = resultTableTrash.filePath + resultTableTrash.fileName
  45. let fileNameTo = session.urlBase + "/remote.php/dav/trashbin/" + session.userId + "/restore/" + resultTableTrash.fileName
  46. NextcloudKit.shared.moveFileOrFolder(serverUrlFileNameSource: fileNameFrom, serverUrlFileNameDestination: fileNameTo, overwrite: true, account: session.account) { account, _, error in
  47. guard error == .success else {
  48. NCContentPresenter().showError(error: error)
  49. return
  50. }
  51. self.database.deleteTrash(fileId: fileId, account: account)
  52. self.reloadDataSource()
  53. }
  54. }
  55. func emptyTrash() {
  56. let serverUrlFileName = session.urlBase + "/remote.php/dav/trashbin/" + session.userId + "/trash"
  57. NextcloudKit.shared.deleteFileOrFolder(serverUrlFileName: serverUrlFileName, account: session.account) { account, _, error in
  58. guard error == .success else {
  59. NCContentPresenter().showError(error: error)
  60. return
  61. }
  62. self.database.deleteTrash(fileId: nil, account: account)
  63. self.reloadDataSource()
  64. }
  65. }
  66. func deleteItem(with fileId: String) {
  67. guard let resultTableTrash = self.database.getResultTrashItem(fileId: fileId, account: session.account) else { return }
  68. let serverUrlFileName = resultTableTrash.filePath + resultTableTrash.fileName
  69. NextcloudKit.shared.deleteFileOrFolder(serverUrlFileName: serverUrlFileName, account: session.account) { account, _, error in
  70. guard error == .success else {
  71. NCContentPresenter().showError(error: error)
  72. return
  73. }
  74. self.database.deleteTrash(fileId: fileId, account: account)
  75. self.reloadDataSource()
  76. }
  77. }
  78. }
  79. class NCOperationDownloadThumbnailTrash: ConcurrentOperation, @unchecked Sendable {
  80. var fileId: String
  81. var fileName: String
  82. var collectionView: UICollectionView
  83. var account: String
  84. init(fileId: String, fileName: String, account: String, collectionView: UICollectionView) {
  85. self.fileId = fileId
  86. self.fileName = fileName
  87. self.account = account
  88. self.collectionView = collectionView
  89. }
  90. override func start() {
  91. guard !isCancelled else { return self.finish() }
  92. NextcloudKit.shared.downloadTrashPreview(fileId: fileId, account: account) { _, _, _, responseData, error in
  93. if error == .success, let data = responseData?.data {
  94. NCUtility().createImageFileFrom(data: data, ocId: self.fileId, etag: self.fileName)
  95. for case let cell as NCTrashCellProtocol in self.collectionView.visibleCells where cell.objectId == self.fileId {
  96. cell.imageItem?.contentMode = .scaleAspectFill
  97. UIView.transition(with: cell.imageItem,
  98. duration: 0.75,
  99. options: .transitionCrossDissolve,
  100. animations: { cell.imageItem.image = UIImage(data: data) },
  101. completion: nil)
  102. }
  103. }
  104. self.finish()
  105. }
  106. }
  107. }