NCTrash+Networking.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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: appDelegate.account) { task in
  28. self.dataSourceTask = task
  29. self.collectionView.reloadData()
  30. } completion: { account, items, _, error in
  31. self.refreshControl.endRefreshing()
  32. if account == self.appDelegate.account {
  33. NCManageDatabase.shared.deleteTrash(filePath: self.getFilePath(), account: account)
  34. NCManageDatabase.shared.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 tableTrash = NCManageDatabase.shared.getTrashItem(fileId: fileId, account: appDelegate.account) else { return }
  44. let fileNameFrom = tableTrash.filePath + tableTrash.fileName
  45. let fileNameTo = appDelegate.urlBase + "/" + NextcloudKit.shared.nkCommonInstance.dav + "/trashbin/" + appDelegate.userId + "/restore/" + tableTrash.fileName
  46. NextcloudKit.shared.moveFileOrFolder(serverUrlFileNameSource: fileNameFrom, serverUrlFileNameDestination: fileNameTo, overwrite: true, account: appDelegate.account) { account, error in
  47. guard error == .success, account == self.appDelegate.account else {
  48. NCContentPresenter().showError(error: error)
  49. return
  50. }
  51. NCManageDatabase.shared.deleteTrash(fileId: fileId, account: account)
  52. self.reloadDataSource()
  53. }
  54. }
  55. func emptyTrash() {
  56. let serverUrlFileName = appDelegate.urlBase + "/" + NextcloudKit.shared.nkCommonInstance.dav + "/trashbin/" + appDelegate.userId + "/trash"
  57. NextcloudKit.shared.deleteFileOrFolder(serverUrlFileName: serverUrlFileName, account: appDelegate.account) { account, error in
  58. guard error == .success, account == self.appDelegate.account else {
  59. NCContentPresenter().showError(error: error)
  60. return
  61. }
  62. NCManageDatabase.shared.deleteTrash(fileId: nil, account: self.appDelegate.account)
  63. self.reloadDataSource()
  64. }
  65. }
  66. func deleteItem(with fileId: String) {
  67. guard let tableTrash = NCManageDatabase.shared.getTrashItem(fileId: fileId, account: appDelegate.account) else { return }
  68. let serverUrlFileName = tableTrash.filePath + tableTrash.fileName
  69. NextcloudKit.shared.deleteFileOrFolder(serverUrlFileName: serverUrlFileName, account: appDelegate.account) { account, error in
  70. guard error == .success, account == self.appDelegate.account else {
  71. NCContentPresenter().showError(error: error)
  72. return
  73. }
  74. NCManageDatabase.shared.deleteTrash(fileId: fileId, account: account)
  75. self.reloadDataSource()
  76. }
  77. }
  78. }
  79. class NCOperationDownloadThumbnailTrash: ConcurrentOperation {
  80. var tableTrash: tableTrash
  81. var fileId: String
  82. var collectionView: UICollectionView?
  83. var cell: NCTrashCellProtocol?
  84. var account: String
  85. init(tableTrash: tableTrash, fileId: String, account: String, cell: NCTrashCellProtocol?, collectionView: UICollectionView?) {
  86. self.tableTrash = tableTrash
  87. self.fileId = fileId
  88. self.account = account
  89. self.cell = cell
  90. self.collectionView = collectionView
  91. }
  92. override func start() {
  93. guard !isCancelled else { return self.finish() }
  94. let fileNamePreviewLocalPath = NCUtilityFileSystem().getDirectoryProviderStoragePreviewOcId(tableTrash.fileId, etag: tableTrash.fileName)
  95. let fileNameIconLocalPath = NCUtilityFileSystem().getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName)
  96. NextcloudKit.shared.downloadTrashPreview(fileId: tableTrash.fileId,
  97. fileNamePreviewLocalPath: fileNamePreviewLocalPath,
  98. fileNameIconLocalPath: fileNameIconLocalPath,
  99. widthPreview: NCGlobal.shared.sizePreview,
  100. heightPreview: NCGlobal.shared.sizePreview,
  101. sizeIcon: NCGlobal.shared.sizeIcon,
  102. account: account,
  103. options: NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)) { _, imagePreview, _, _, _, error in
  104. if error == .success, let imagePreview = imagePreview {
  105. DispatchQueue.main.async {
  106. if self.fileId == self.cell?.objectId, let imageView = self.cell?.imageItem {
  107. UIView.transition(with: imageView,
  108. duration: 0.75,
  109. options: .transitionCrossDissolve,
  110. animations: { imageView.image = imagePreview },
  111. completion: nil)
  112. } else {
  113. self.collectionView?.reloadData()
  114. }
  115. }
  116. }
  117. self.finish()
  118. }
  119. }
  120. }