Fix persisting active config

This commit is contained in:
ronaldheft 2022-08-02 17:10:45 -04:00
parent f6c43e479d
commit a7424cc428
3 changed files with 12 additions and 13 deletions

View file

@ -40,7 +40,7 @@ public class AbsDatabase: CAPPlugin {
id = "\(address)@\(username)".toBase64() id = "\(address)@\(username)".toBase64()
} }
let config = ServerConnectionConfig(id: id!, index: 0, name: name, address: address, userId: userId, username: username, token: token) let config = ServerConnectionConfig(id: id!, index: 1, name: name, address: address, userId: userId, username: username, token: token)
Store.serverConfig = config Store.serverConfig = config
call.resolve(convertServerConnectionConfigToJSON(config: config)) call.resolve(convertServerConnectionConfigToJSON(config: config))

View file

@ -11,7 +11,7 @@ import Unrealm
struct ServerConnectionConfig: Realmable { struct ServerConnectionConfig: Realmable {
var id: String = UUID().uuidString var id: String = UUID().uuidString
var index: Int = 0 var index: Int = 1
var name: String = "" var name: String = ""
var address: String = "" var address: String = ""
var userId: String = "" var userId: String = ""
@ -30,6 +30,10 @@ struct ServerConnectionConfig: Realmable {
struct ServerConnectionConfigActiveIndex: Realmable { struct ServerConnectionConfigActiveIndex: Realmable {
// This could overflow, but you really would have to try // This could overflow, but you really would have to try
var index: Int? var index: Int?
static func primaryKey() -> String? {
return "index"
}
} }
func convertServerConnectionConfigToJSON(config: ServerConnectionConfig) -> Dictionary<String, Any> { func convertServerConnectionConfigToJSON(config: ServerConnectionConfig) -> Dictionary<String, Any> {

View file

@ -76,21 +76,16 @@ class Database {
} }
} }
public func setLastActiveConfigIndexToNil() { private func setLastActiveConfigIndexToNil() {
Database.realmQueue.sync {
setLastActiveConfigIndex(index: nil) setLastActiveConfigIndex(index: nil)
} }
}
public func setLastActiveConfigIndex(index: Int?) {
let existing = instance.objects(ServerConnectionConfigActiveIndex.self)
var obj = ServerConnectionConfigActiveIndex()
obj.index = index
private func setLastActiveConfigIndex(index: Int?) {
do { do {
try instance.write { try instance.write {
instance.delete(existing) var existing = instance.objects(ServerConnectionConfigActiveIndex.self).last ?? ServerConnectionConfigActiveIndex(index: index)
instance.add(obj) existing.index = index
instance.add(existing, update: .modified)
} }
} catch(let exception) { } catch(let exception) {
NSLog("failed to save server config active index") NSLog("failed to save server config active index")