2022-04-11 16:29:19 +02:00
|
|
|
//
|
|
|
|
// Database.swift
|
|
|
|
// App
|
|
|
|
//
|
|
|
|
// Created by Rasmus Krämer on 11.04.22.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RealmSwift
|
|
|
|
|
|
|
|
class Database {
|
|
|
|
public static let realmQueue = DispatchQueue(label: "realm-queue")
|
2022-04-11 18:50:25 +02:00
|
|
|
|
|
|
|
// All DB releated actions must be executed on "realm-queue"
|
|
|
|
private static var instance: Realm = {
|
|
|
|
// TODO: handle thread errors
|
|
|
|
try! Realm(queue: realmQueue)
|
|
|
|
}()
|
|
|
|
|
2022-04-11 18:31:14 +02:00
|
|
|
public static func getActiveServerConfigIndex() -> Int {
|
2022-04-11 18:50:25 +02:00
|
|
|
guard let config = instance.objects(ServerConnectionConfig.self).first else {
|
2022-04-11 18:31:14 +02:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return config.index
|
|
|
|
}
|
2022-04-11 16:29:19 +02:00
|
|
|
|
|
|
|
public static func setServerConnectionConfig(config: ServerConnectionConfig) {
|
2022-04-11 18:50:25 +02:00
|
|
|
let existing: ServerConnectionConfig? = instance.object(ofType: ServerConnectionConfig.self, forPrimaryKey: config.id)
|
2022-04-11 16:29:19 +02:00
|
|
|
|
2022-04-11 18:50:25 +02:00
|
|
|
try! instance.write {
|
2022-04-11 18:31:14 +02:00
|
|
|
if existing != nil {
|
2022-04-11 18:50:25 +02:00
|
|
|
instance.delete(existing!)
|
2022-04-11 18:31:14 +02:00
|
|
|
}
|
2022-04-11 18:50:25 +02:00
|
|
|
instance.add(config)
|
2022-04-11 16:29:19 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-11 18:31:14 +02:00
|
|
|
public static func getServerConnectionConfigs() -> [ServerConnectionConfig] {
|
2022-04-11 18:50:25 +02:00
|
|
|
let configs = instance.objects(ServerConnectionConfig.self)
|
2022-04-11 16:29:19 +02:00
|
|
|
|
2022-04-11 18:31:14 +02:00
|
|
|
if configs.count <= 0 {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
return Array(configs)
|
2022-04-11 16:29:19 +02:00
|
|
|
}
|
|
|
|
}
|