diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/ServerSettingsModel.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/ServerSettingsModel.kt index dc5ad667..0e1e9f79 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/ServerSettingsModel.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/ServerSettingsModel.kt @@ -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)