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 config.token = token
Store.serverConfig = config Store.serverConfig = config
call.resolve(serverConnectionConfigToJSON(config: config)) call.resolve(convertServerConnectionConfigToJSON(config: config))
} }
} }
@objc func getDeviceData(_ call: CAPPluginCall) { @objc func getDeviceData(_ call: CAPPluginCall) {
Database.realmQueue.sync { 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 import RealmSwift
class ServerConnectionConfig: Object { class ServerConnectionConfig: Object {
@Persisted var id: String @Persisted(primaryKey: true) var id: String
@Persisted var index: Int = 0 @Persisted(indexed: true) var index: Int
@Persisted var name: String @Persisted var name: String
@Persisted var address: String @Persisted var address: String
@Persisted var userId: String @Persisted var userId: String
@ -18,7 +18,7 @@ class ServerConnectionConfig: Object {
@Persisted var token: String @Persisted var token: String
} }
func serverConnectionConfigToJSON(config: ServerConnectionConfig) -> Dictionary<String, Any> { func convertServerConnectionConfigToJSON(config: ServerConnectionConfig) -> Dictionary<String, Any> {
return [ return [
"id": config.id, "id": config.id,
"name": config.name, "name": config.name,

View file

@ -10,23 +10,36 @@ import RealmSwift
class Database { class Database {
public static let realmQueue = DispatchQueue(label: "realm-queue") 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) { public static func setServerConnectionConfig(config: ServerConnectionConfig) {
// TODO: handle thread errors
let realm = try! Realm(queue: realmQueue) 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 { try! realm.write {
realm.delete(existing) if existing != nil {
realm.delete(existing!)
}
realm.add(config) realm.add(config)
} }
} }
public static func getServerConnectionConfig() -> ServerConnectionConfig { public static func getServerConnectionConfigs() -> [ServerConnectionConfig] {
let realm = try! Realm(queue: realmQueue) let realm = try! Realm(queue: realmQueue)
guard let config = realm.objects(ServerConnectionConfig.self).first else { let configs = realm.objects(ServerConnectionConfig.self)
let fallback = ServerConnectionConfig()
return fallback
}
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 // ONLY USE REALM IN Database.realmQueue OR ELSE THE APP WILL CRASH
public static var serverConfig: ServerConnectionConfig { public static var serverConfig: ServerConnectionConfig {
get { get {
return Database.getServerConnectionConfig() // TODO: change this when multiple configs are possible
Database.getServerConnectionConfigs()[Database.getActiveServerConfigIndex()]
} }
set(updated) { set(updated) {
Database.setServerConnectionConfig(config: updated) Database.setServerConnectionConfig(config: updated)

View file

@ -6,6 +6,7 @@ const isWeb = Capacitor.getPlatform() == 'web'
class DbService { class DbService {
constructor() { } constructor() { }
// Please dont use this, it is not implemented in ios (maybe key: primary value: any ?)
save(db, key, value) { save(db, key, value) {
if (isWeb) return if (isWeb) return
return AbsDatabase.saveFromWebview({ db, key, value }).then(() => { return AbsDatabase.saveFromWebview({ db, key, value }).then(() => {
@ -15,6 +16,7 @@ class DbService {
}) })
} }
// Please dont use this, it is not implemented in ios
load(db, key) { load(db, key) {
if (isWeb) return null if (isWeb) return null
return AbsDatabase.loadFromWebview({ db, key }).then((data) => { return AbsDatabase.loadFromWebview({ db, key }).then((data) => {