advplyr.audiobookshelf-app/ios/App/Shared/util/Database.swift

46 lines
1.3 KiB
Swift
Raw Normal View History

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:31:14 +02:00
public static func getActiveServerConfigIndex() -> Int {
let realm = try! Realm(queue: realmQueue)
guard let config = realm.objects(ServerConnectionConfig.self).first else {
return -1
}
return config.index
}
2022-04-11 16:29:19 +02:00
public static func setServerConnectionConfig(config: ServerConnectionConfig) {
2022-04-11 18:31:14 +02:00
// TODO: handle thread errors
2022-04-11 16:29:19 +02:00
let realm = try! Realm(queue: realmQueue)
2022-04-11 18:31:14 +02:00
var existing: ServerConnectionConfig?
2022-04-11 16:29:19 +02:00
2022-04-11 18:31:14 +02:00
if config.id != nil {
existing = realm.object(ofType: ServerConnectionConfig.self, forPrimaryKey: config.id)
}
2022-04-11 16:29:19 +02:00
try! realm.write {
2022-04-11 18:31:14 +02:00
if existing != nil {
realm.delete(existing!)
}
2022-04-11 16:29:19 +02:00
realm.add(config)
}
}
2022-04-11 18:31:14 +02:00
public static func getServerConnectionConfigs() -> [ServerConnectionConfig] {
2022-04-11 16:29:19 +02:00
let realm = try! Realm(queue: realmQueue)
2022-04-11 18:31:14 +02:00
let configs = realm.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
}
}