NCManageDatabase+Problems.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // NCManageDatabase+Problems.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 15/03/22.
  6. // Copyright © 2022 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 RealmSwift
  25. import NextcloudKit
  26. class tableProblems: Object {
  27. @Persisted var account = ""
  28. @Persisted(primaryKey: true) var primaryKey = ""
  29. @Persisted var selector: String = ""
  30. @Persisted var count: Int = 0
  31. @Persisted var oldest: Double = 0
  32. }
  33. extension NCManageDatabase {
  34. func addProblem(account: String, selector: String, error: NKError) {
  35. do {
  36. let realm = try Realm()
  37. let primaryKey = account + selector
  38. try realm.write {
  39. if let result = realm.objects(tableProblems.self).filter("primaryKey == %@", primaryKey).first {
  40. result.count += 1
  41. result.oldest = Date().timeIntervalSince1970
  42. realm.add(result, update: .all)
  43. } else {
  44. let result = tableProblems()
  45. result.primaryKey = primaryKey
  46. result.count = 1
  47. result.oldest = Date().timeIntervalSince1970
  48. realm.add(result, update: .all)
  49. }
  50. }
  51. } catch let error {
  52. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  53. }
  54. }
  55. func deleteProblem(account: String, selector: String) {
  56. do {
  57. let realm = try Realm()
  58. let primaryKey = account + selector
  59. try realm.write {
  60. let result = realm.objects(tableProblems.self).filter("primaryKey == %@", primaryKey)
  61. realm.delete(result)
  62. }
  63. } catch let error {
  64. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  65. }
  66. }
  67. func getProblems(account: String) -> Results<tableProblems>? {
  68. do {
  69. let realm = try Realm()
  70. realm.refresh()
  71. return realm.objects(tableProblems.self).filter("account == %@", account)
  72. } catch let error as NSError {
  73. NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
  74. }
  75. return nil
  76. }
  77. }