// // NCManageDatabase+ExternalSites.swift // Nextcloud // // Created by Marino Faggiana on 13/11/23. // Copyright © 2023 Marino Faggiana. All rights reserved. // // Author Marino Faggiana // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // import Foundation import UIKit import RealmSwift import NextcloudKit class tableExternalSites: Object { @objc dynamic var account = "" @objc dynamic var icon = "" @objc dynamic var idExternalSite: Int = 0 @objc dynamic var lang = "" @objc dynamic var name = "" @objc dynamic var type = "" @objc dynamic var url = "" } extension NCManageDatabase { func addExternalSites(_ externalSite: NKExternalSite, account: String) { do { let realm = try Realm() try realm.write { let addObject = tableExternalSites() addObject.account = account addObject.idExternalSite = externalSite.idExternalSite addObject.icon = externalSite.icon addObject.lang = externalSite.lang addObject.name = externalSite.name addObject.url = externalSite.url addObject.type = externalSite.type realm.add(addObject) } } catch let error { NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)") } } func deleteExternalSites(account: String) { do { let realm = try Realm() try realm.write { let results = realm.objects(tableExternalSites.self).filter("account == %@", account) realm.delete(results) } } catch let error { NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)") } } func getAllExternalSites(account: String) -> [tableExternalSites]? { do { let realm = try Realm() realm.refresh() let results = realm.objects(tableExternalSites.self).filter("account == %@", account).sorted(byKeyPath: "idExternalSite", ascending: true) if results.isEmpty { return nil } else { return Array(results.map { tableExternalSites.init(value: $0) }) } } catch let error as NSError { NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access to database: \(error)") } return nil } }