mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-05 02:25:45 +02:00
Fix configs sharing an index
This commit is contained in:
parent
ba1efedd79
commit
c1f803bdd0
2 changed files with 31 additions and 19 deletions
|
@ -43,7 +43,7 @@ public class AbsDatabase: CAPPlugin {
|
|||
|
||||
let config = ServerConnectionConfig()
|
||||
config.id = id ?? ""
|
||||
config.index = 1
|
||||
config.index = 0
|
||||
config.name = name
|
||||
config.address = address
|
||||
config.userId = userId
|
||||
|
|
|
@ -20,26 +20,38 @@ class Database {
|
|||
let realm = try! Realm()
|
||||
let existing: ServerConnectionConfig? = realm.object(ofType: ServerConnectionConfig.self, forPrimaryKey: config.id)
|
||||
|
||||
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 {
|
||||
if existing != nil {
|
||||
realm.delete(existing!)
|
||||
if let existing = existing {
|
||||
do {
|
||||
try existing.update {
|
||||
existing.name = config.name
|
||||
existing.address = config.address
|
||||
existing.userId = config.userId
|
||||
existing.username = config.username
|
||||
existing.token = config.token
|
||||
}
|
||||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue