Fixed coroutine scope for server creation/update

This commit is contained in:
Nite 2021-02-17 18:36:36 +01:00
parent 6fcdfea1db
commit 8a047c5b78
No known key found for this signature in database
GPG Key ID: 1D1AD59B1C6386C1
1 changed files with 7 additions and 2 deletions

View File

@ -6,6 +6,9 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.preference.PreferenceManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.moire.ultrasonic.data.ActiveServerProvider
@ -36,6 +39,8 @@ class ServerSettingsModel(
private const val PREFERENCES_KEY_MUSIC_FOLDER_ID = "musicFolderId"
}
private val appScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
/**
* This function will try and convert settings from the Preferences to the Database
* @return True, if the migration was executed, False otherwise
@ -160,7 +165,7 @@ class ServerSettingsModel(
fun updateItem(serverSetting: ServerSetting?) {
if (serverSetting == null) return
viewModelScope.launch {
appScope.launch {
repository.update(serverSetting)
activeServerProvider.invalidateCache()
Timber.d("updateItem updated server setting: $serverSetting")
@ -173,7 +178,7 @@ class ServerSettingsModel(
fun saveNewItem(serverSetting: ServerSetting?) {
if (serverSetting == null) return
viewModelScope.launch {
appScope.launch {
serverSetting.index = (repository.count() ?: 0) + 1
serverSetting.id = (repository.getMaxId() ?: 0) + 1
repository.insert(serverSetting)