Login is now working

This commit is contained in:
Rasmus Krämer 2022-04-11 18:31:14 +02:00
parent a0ee82bb31
commit 6de4626239
No known key found for this signature in database
GPG key ID: EC9E510611BFDAA2
5 changed files with 41 additions and 14 deletions

View file

@ -50,12 +50,23 @@ public class AbsDatabase: CAPPlugin {
config.token = token
Store.serverConfig = config
call.resolve(serverConnectionConfigToJSON(config: config))
call.resolve(convertServerConnectionConfigToJSON(config: config))
}
}
@objc func getDeviceData(_ call: CAPPluginCall) {
Database.realmQueue.sync {
call.resolve(serverConnectionConfigToJSON(config: Store.serverConfig))
let configs = Database.getServerConnectionConfigs()
let index = Database.getActiveServerConfigIndex()
call.resolve([
"serverConnectionConfigs": configs.map { config in
return convertServerConnectionConfigToJSON(config: config)
},
"lastServerConnectionConfigId": index < 0 ? -1 : configs[index].id,
"currentLocalPlaybackSession": nil, // Luckily this isn't implemented yet
])
}
}
}

View file

@ -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,

View file

@ -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)
}
}

View file

@ -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)