NCManageDatabase+Trash.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // NCManageDatabase+Trash.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/11/23.
  6. // Copyright © 2023 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@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. import UIKit
  25. import RealmSwift
  26. import NextcloudKit
  27. class tableTrash: Object {
  28. @objc dynamic var account = ""
  29. @objc dynamic var classFile = ""
  30. @objc dynamic var contentType = ""
  31. @objc dynamic var date = NSDate()
  32. @objc dynamic var directory: Bool = false
  33. @objc dynamic var fileId = ""
  34. @objc dynamic var fileName = ""
  35. @objc dynamic var filePath = ""
  36. @objc dynamic var hasPreview: Bool = false
  37. @objc dynamic var iconName = ""
  38. @objc dynamic var size: Int64 = 0
  39. @objc dynamic var trashbinFileName = ""
  40. @objc dynamic var trashbinOriginalLocation = ""
  41. @objc dynamic var trashbinDeletionTime = NSDate()
  42. override static func primaryKey() -> String {
  43. return "fileId"
  44. }
  45. }
  46. extension NCManageDatabase {
  47. func addTrash(account: String, items: [NKTrash]) {
  48. do {
  49. let realm = try Realm()
  50. try realm.write {
  51. for trash in items {
  52. let object = tableTrash()
  53. object.account = account
  54. object.contentType = trash.contentType
  55. object.date = trash.date as NSDate
  56. object.directory = trash.directory
  57. object.fileId = trash.fileId
  58. object.fileName = trash.fileName
  59. object.filePath = trash.filePath
  60. object.hasPreview = trash.hasPreview
  61. object.iconName = trash.iconName
  62. object.size = trash.size
  63. object.trashbinDeletionTime = trash.trashbinDeletionTime as NSDate
  64. object.trashbinFileName = trash.trashbinFileName
  65. object.trashbinOriginalLocation = trash.trashbinOriginalLocation
  66. object.classFile = trash.classFile
  67. realm.add(object, update: .all)
  68. }
  69. }
  70. } catch let error {
  71. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  72. }
  73. }
  74. func deleteTrash(filePath: String?, account: String) {
  75. var predicate = NSPredicate()
  76. do {
  77. let realm = try Realm()
  78. try realm.write {
  79. if filePath == nil {
  80. predicate = NSPredicate(format: "account == %@", account)
  81. } else {
  82. predicate = NSPredicate(format: "account == %@ AND filePath == %@", account, filePath!)
  83. }
  84. let result = realm.objects(tableTrash.self).filter(predicate)
  85. realm.delete(result)
  86. }
  87. } catch let error {
  88. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  89. }
  90. }
  91. func deleteTrash(fileId: String?, account: String) {
  92. var predicate = NSPredicate()
  93. do {
  94. let realm = try Realm()
  95. try realm.write {
  96. if fileId == nil {
  97. predicate = NSPredicate(format: "account == %@", account)
  98. } else {
  99. predicate = NSPredicate(format: "account == %@ AND fileId == %@", account, fileId!)
  100. }
  101. let result = realm.objects(tableTrash.self).filter(predicate)
  102. realm.delete(result)
  103. }
  104. } catch let error {
  105. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  106. }
  107. }
  108. func getResultsTrash(filePath: String, account: String) -> Results<tableTrash>? {
  109. do {
  110. let realm = try Realm()
  111. realm.refresh()
  112. return realm.objects(tableTrash.self).filter("account == %@ AND filePath == %@", account, filePath).sorted(byKeyPath: "trashbinDeletionTime", ascending: false)
  113. } catch let error as NSError {
  114. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access to database: \(error)")
  115. }
  116. return nil
  117. }
  118. func getResultTrashItem(fileId: String, account: String) -> tableTrash? {
  119. do {
  120. let realm = try Realm()
  121. realm.refresh()
  122. return realm.objects(tableTrash.self).filter("account == %@ AND fileId == %@", account, fileId).first
  123. } catch let error as NSError {
  124. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access to database: \(error)")
  125. }
  126. return nil
  127. }
  128. }