mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-26 13:24:39 +02:00
Login is now working
This commit is contained in:
@@ -9,8 +9,8 @@ import Foundation
|
||||
import RealmSwift
|
||||
|
||||
class ServerConnectionConfig: Object {
|
||||
@Persisted var id: String
|
||||
@Persisted var index: Int = 0
|
||||
@Persisted(primaryKey: true) var id: String
|
||||
@Persisted(indexed: true) var index: Int
|
||||
@Persisted var name: String
|
||||
@Persisted var address: String
|
||||
@Persisted var userId: String
|
||||
@@ -18,7 +18,7 @@ class ServerConnectionConfig: Object {
|
||||
@Persisted var token: String
|
||||
}
|
||||
|
||||
func serverConnectionConfigToJSON(config: ServerConnectionConfig) -> Dictionary<String, Any> {
|
||||
func convertServerConnectionConfigToJSON(config: ServerConnectionConfig) -> Dictionary<String, Any> {
|
||||
return [
|
||||
"id": config.id,
|
||||
"name": config.name,
|
||||
|
||||
@@ -10,23 +10,36 @@ import RealmSwift
|
||||
|
||||
class Database {
|
||||
public static let realmQueue = DispatchQueue(label: "realm-queue")
|
||||
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
|
||||
}
|
||||
|
||||
public static func setServerConnectionConfig(config: ServerConnectionConfig) {
|
||||
// TODO: handle thread errors
|
||||
let realm = try! Realm(queue: realmQueue)
|
||||
let existing = realm.objects(ServerConnectionConfig.self)
|
||||
var existing: ServerConnectionConfig?
|
||||
|
||||
if config.id != nil {
|
||||
existing = realm.object(ofType: ServerConnectionConfig.self, forPrimaryKey: config.id)
|
||||
}
|
||||
try! realm.write {
|
||||
realm.delete(existing)
|
||||
if existing != nil {
|
||||
realm.delete(existing!)
|
||||
}
|
||||
realm.add(config)
|
||||
}
|
||||
}
|
||||
public static func getServerConnectionConfig() -> ServerConnectionConfig {
|
||||
public static func getServerConnectionConfigs() -> [ServerConnectionConfig] {
|
||||
let realm = try! Realm(queue: realmQueue)
|
||||
guard let config = realm.objects(ServerConnectionConfig.self).first else {
|
||||
let fallback = ServerConnectionConfig()
|
||||
return fallback
|
||||
}
|
||||
let configs = realm.objects(ServerConnectionConfig.self)
|
||||
|
||||
return config
|
||||
if configs.count <= 0 {
|
||||
return []
|
||||
}
|
||||
return Array(configs)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ class Store {
|
||||
// ONLY USE REALM IN Database.realmQueue OR ELSE THE APP WILL CRASH
|
||||
public static var serverConfig: ServerConnectionConfig {
|
||||
get {
|
||||
return Database.getServerConnectionConfig()
|
||||
// TODO: change this when multiple configs are possible
|
||||
Database.getServerConnectionConfigs()[Database.getActiveServerConfigIndex()]
|
||||
}
|
||||
set(updated) {
|
||||
Database.setServerConnectionConfig(config: updated)
|
||||
|
||||
Reference in New Issue
Block a user