NCManageDatabase+ExternalSites.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // NCManageDatabase+ExternalSites.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 RealmSwift
  25. import NextcloudKit
  26. class tableExternalSites: Object {
  27. @objc dynamic var account = ""
  28. @objc dynamic var icon = ""
  29. @objc dynamic var idExternalSite: Int = 0
  30. @objc dynamic var lang = ""
  31. @objc dynamic var name = ""
  32. @objc dynamic var type = ""
  33. @objc dynamic var url = ""
  34. }
  35. extension NCManageDatabase {
  36. func addExternalSites(_ externalSite: NKExternalSite, account: String) {
  37. do {
  38. let realm = try Realm()
  39. try realm.write {
  40. let addObject = tableExternalSites()
  41. addObject.account = account
  42. addObject.idExternalSite = externalSite.idExternalSite
  43. addObject.icon = externalSite.icon
  44. addObject.lang = externalSite.lang
  45. addObject.name = externalSite.name
  46. addObject.url = externalSite.url
  47. addObject.type = externalSite.type
  48. realm.add(addObject)
  49. }
  50. } catch let error {
  51. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  52. }
  53. }
  54. func deleteExternalSites(account: String) {
  55. do {
  56. let realm = try Realm()
  57. try realm.write {
  58. let results = realm.objects(tableExternalSites.self).filter("account == %@", account)
  59. realm.delete(results)
  60. }
  61. } catch let error {
  62. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  63. }
  64. }
  65. func getAllExternalSites(account: String) -> [tableExternalSites]? {
  66. do {
  67. let realm = try Realm()
  68. realm.refresh()
  69. let results = realm.objects(tableExternalSites.self).filter("account == %@", account).sorted(byKeyPath: "idExternalSite", ascending: true)
  70. if results.isEmpty {
  71. return nil
  72. } else {
  73. return Array(results.map { tableExternalSites.init(value: $0) })
  74. }
  75. } catch let error as NSError {
  76. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access to database: \(error)")
  77. }
  78. return nil
  79. }
  80. }