mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-07-16 08:54:47 +02:00
Fix persisting active config
This commit is contained in:
parent
f6c43e479d
commit
a7424cc428
3 changed files with 12 additions and 13 deletions
|
@ -40,7 +40,7 @@ public class AbsDatabase: CAPPlugin {
|
|||
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
|
||||
call.resolve(convertServerConnectionConfigToJSON(config: config))
|
||||
|
|
|
@ -11,7 +11,7 @@ import Unrealm
|
|||
|
||||
struct ServerConnectionConfig: Realmable {
|
||||
var id: String = UUID().uuidString
|
||||
var index: Int = 0
|
||||
var index: Int = 1
|
||||
var name: String = ""
|
||||
var address: String = ""
|
||||
var userId: String = ""
|
||||
|
@ -30,6 +30,10 @@ struct ServerConnectionConfig: Realmable {
|
|||
struct ServerConnectionConfigActiveIndex: Realmable {
|
||||
// This could overflow, but you really would have to try
|
||||
var index: Int?
|
||||
|
||||
static func primaryKey() -> String? {
|
||||
return "index"
|
||||
}
|
||||
}
|
||||
|
||||
func convertServerConnectionConfigToJSON(config: ServerConnectionConfig) -> Dictionary<String, Any> {
|
||||
|
|
|
@ -76,21 +76,16 @@ class Database {
|
|||
}
|
||||
}
|
||||
|
||||
public func setLastActiveConfigIndexToNil() {
|
||||
Database.realmQueue.sync {
|
||||
setLastActiveConfigIndex(index: nil)
|
||||
}
|
||||
private func setLastActiveConfigIndexToNil() {
|
||||
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 {
|
||||
try instance.write {
|
||||
instance.delete(existing)
|
||||
instance.add(obj)
|
||||
var existing = instance.objects(ServerConnectionConfigActiveIndex.self).last ?? ServerConnectionConfigActiveIndex(index: index)
|
||||
existing.index = index
|
||||
instance.add(existing, update: .modified)
|
||||
}
|
||||
} catch(let exception) {
|
||||
NSLog("failed to save server config active index")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue