mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-30 23:27:17 +02:00
Fix configs sharing an index
This commit is contained in:
@@ -43,7 +43,7 @@ public class AbsDatabase: CAPPlugin {
|
|||||||
|
|
||||||
let config = ServerConnectionConfig()
|
let config = ServerConnectionConfig()
|
||||||
config.id = id ?? ""
|
config.id = id ?? ""
|
||||||
config.index = 1
|
config.index = 0
|
||||||
config.name = name
|
config.name = name
|
||||||
config.address = address
|
config.address = address
|
||||||
config.userId = userId
|
config.userId = userId
|
||||||
|
|||||||
@@ -20,26 +20,38 @@ class Database {
|
|||||||
let realm = try! Realm()
|
let realm = try! Realm()
|
||||||
let existing: ServerConnectionConfig? = realm.object(ofType: ServerConnectionConfig.self, forPrimaryKey: config.id)
|
let existing: ServerConnectionConfig? = realm.object(ofType: ServerConnectionConfig.self, forPrimaryKey: config.id)
|
||||||
|
|
||||||
if config.index == 0 {
|
if let existing = existing {
|
||||||
let lastConfig: ServerConnectionConfig? = realm.objects(ServerConnectionConfig.self).last
|
do {
|
||||||
|
try existing.update {
|
||||||
if lastConfig != nil {
|
existing.name = config.name
|
||||||
config.index = lastConfig!.index + 1
|
existing.address = config.address
|
||||||
} else {
|
existing.userId = config.userId
|
||||||
config.index = 1
|
existing.username = config.username
|
||||||
}
|
existing.token = config.token
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
try realm.write {
|
|
||||||
if existing != nil {
|
|
||||||
realm.delete(existing!)
|
|
||||||
}
|
}
|
||||||
realm.add(config)
|
} catch {
|
||||||
|
NSLog("failed to update server config")
|
||||||
|
debugPrint(error)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if config.index == 0 {
|
||||||
|
let lastConfig: ServerConnectionConfig? = realm.objects(ServerConnectionConfig.self).last
|
||||||
|
|
||||||
|
if lastConfig != nil {
|
||||||
|
config.index = lastConfig!.index + 1
|
||||||
|
} else {
|
||||||
|
config.index = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
try realm.write {
|
||||||
|
realm.add(config)
|
||||||
|
}
|
||||||
|
} catch(let exception) {
|
||||||
|
NSLog("failed to save server config")
|
||||||
|
debugPrint(exception)
|
||||||
}
|
}
|
||||||
} catch(let exception) {
|
|
||||||
NSLog("failed to save server config")
|
|
||||||
debugPrint(exception)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setLastActiveConfigIndex(index: config.index)
|
setLastActiveConfigIndex(index: config.index)
|
||||||
|
|||||||
Reference in New Issue
Block a user