diff --git a/ios/App/App/plugins/AbsDatabase.swift b/ios/App/App/plugins/AbsDatabase.swift index 185c3535..e227b2fd 100644 --- a/ios/App/App/plugins/AbsDatabase.swift +++ b/ios/App/App/plugins/AbsDatabase.swift @@ -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 + ]) } } + + } diff --git a/ios/App/Shared/models/ServerConnectionConfig.swift b/ios/App/Shared/models/ServerConnectionConfig.swift index 3fe55f2a..85cf2a74 100644 --- a/ios/App/Shared/models/ServerConnectionConfig.swift +++ b/ios/App/Shared/models/ServerConnectionConfig.swift @@ -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 { +func convertServerConnectionConfigToJSON(config: ServerConnectionConfig) -> Dictionary { return [ "id": config.id, "name": config.name, diff --git a/ios/App/Shared/util/Database.swift b/ios/App/Shared/util/Database.swift index 46292ad6..0d35fac2 100644 --- a/ios/App/Shared/util/Database.swift +++ b/ios/App/Shared/util/Database.swift @@ -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) } } diff --git a/ios/App/Shared/util/Store.swift b/ios/App/Shared/util/Store.swift index e3a68ac4..bdedadce 100644 --- a/ios/App/Shared/util/Store.swift +++ b/ios/App/Shared/util/Store.swift @@ -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) diff --git a/plugins/db.js b/plugins/db.js index 3bc2a860..91177e41 100644 --- a/plugins/db.js +++ b/plugins/db.js @@ -6,6 +6,7 @@ const isWeb = Capacitor.getPlatform() == 'web' class DbService { constructor() { } + // Please dont use this, it is not implemented in ios (maybe key: primary value: any ?) save(db, key, value) { if (isWeb) return 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) { if (isWeb) return null return AbsDatabase.loadFromWebview({ db, key }).then((data) => {