Fix some issues with local db

This commit is contained in:
Damontecres 2025-10-15 11:58:06 -04:00
parent 9d67b9a3ca
commit ba45c3f509
No known key found for this signature in database
2 changed files with 14 additions and 4 deletions

View file

@ -55,9 +55,17 @@ interface JellyfinServerDao {
@Insert(onConflict = OnConflictStrategy.IGNORE) @Insert(onConflict = OnConflictStrategy.IGNORE)
fun addServer(server: JellyfinServer): Long fun addServer(server: JellyfinServer): Long
@Update(onConflict = OnConflictStrategy.ABORT) @Update
fun updateServer(server: JellyfinServer): Int fun updateServer(server: JellyfinServer): Int
@Transaction
fun addOrUpdateServer(server: JellyfinServer) {
val result = addServer(server)
if (result == -1L) {
updateServer(server)
}
}
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun addUser(user: JellyfinUser) fun addUser(user: JellyfinUser)

View file

@ -9,6 +9,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.jellyfin.sdk.Jellyfin import org.jellyfin.sdk.Jellyfin
import org.jellyfin.sdk.api.client.ApiClient import org.jellyfin.sdk.api.client.ApiClient
import org.jellyfin.sdk.api.client.extensions.systemApi
import org.jellyfin.sdk.api.client.extensions.userApi import org.jellyfin.sdk.api.client.extensions.userApi
import org.jellyfin.sdk.model.api.AuthenticationResult import org.jellyfin.sdk.model.api.AuthenticationResult
import org.jellyfin.sdk.model.api.UserDto import org.jellyfin.sdk.model.api.UserDto
@ -42,7 +43,7 @@ class ServerRepository
*/ */
suspend fun addAndChangeServer(server: JellyfinServer) { suspend fun addAndChangeServer(server: JellyfinServer) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
serverDao.addServer(server) serverDao.addOrUpdateServer(server)
} }
changeServer(server) changeServer(server)
} }
@ -74,14 +75,15 @@ class ServerRepository
apiClient.userApi apiClient.userApi
.getCurrentUser() .getCurrentUser()
.content .content
val sysInfo by apiClient.systemApi.getSystemInfo()
val updatedServer = server.copy(name = userDto.serverName) val updatedServer = server.copy(name = sysInfo.serverName)
val updatedUser = val updatedUser =
user.copy( user.copy(
id = userDto.id.toString(), id = userDto.id.toString(),
name = userDto.name, name = userDto.name,
) )
serverDao.addServer(updatedServer) serverDao.addOrUpdateServer(updatedServer)
serverDao.addUser(updatedUser) serverDao.addUser(updatedUser)
userPreferencesDataStore.updateData { userPreferencesDataStore.updateData {
it it