NCManageDatabase+Problems.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 = 1
  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.oldest = Date().timeIntervalSince1970
  47. realm.add(result, update: .all)
  48. }
  49. }
  50. } catch let error {
  51. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  52. }
  53. }
  54. func deleteProblem(account: String, selector: String) {
  55. do {
  56. let realm = try Realm()
  57. let primaryKey = account + selector
  58. try realm.write {
  59. let result = realm.objects(tableProblems.self).filter("primaryKey == %@", primaryKey)
  60. realm.delete(result)
  61. }
  62. } catch let error {
  63. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  64. }
  65. }
  66. func getProblems(account: String) -> Results<tableProblems>? {
  67. do {
  68. let realm = try Realm()
  69. realm.refresh()
  70. return realm.objects(tableProblems.self).filter("account == %@", account)
  71. } catch let error as NSError {
  72. NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
  73. }
  74. return nil
  75. }
  76. func deleteProblems(account: String) {
  77. do {
  78. let realm = try Realm()
  79. try realm.write {
  80. let result = realm.objects(tableProblems.self).filter("account == %@", account)
  81. realm.delete(result)
  82. }
  83. } catch let error {
  84. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  85. }
  86. }
  87. }