NCManageDatabase+ExternalSites.swift 3.1 KB

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