Android: Use coroutine for system updates

This commit is contained in:
Charles Lombardo 2023-01-26 13:13:25 -05:00
parent d66d8210bf
commit 75ce7a04ca
1 changed files with 29 additions and 25 deletions

View File

@ -4,9 +4,12 @@ package org.dolphinemu.dolphinemu.features.sysupdate.ui
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.dolphinemu.dolphinemu.utils.WiiUpdateCallback import org.dolphinemu.dolphinemu.utils.WiiUpdateCallback
import org.dolphinemu.dolphinemu.utils.WiiUtils import org.dolphinemu.dolphinemu.utils.WiiUtils
import java.util.concurrent.Executors
class SystemUpdateViewModel : ViewModel() { class SystemUpdateViewModel : ViewModel() {
val progressData = MutableLiveData<Int>() val progressData = MutableLiveData<Int>()
@ -14,6 +17,7 @@ class SystemUpdateViewModel : ViewModel() {
val titleIdData = MutableLiveData<Long>() val titleIdData = MutableLiveData<Long>()
val resultData = MutableLiveData<Int>() val resultData = MutableLiveData<Int>()
private var isRunning = false
private var canceled = false private var canceled = false
var region = -1 var region = -1
var discPath: String = "" var discPath: String = ""
@ -27,34 +31,38 @@ class SystemUpdateViewModel : ViewModel() {
} }
fun startUpdate() { fun startUpdate() {
if (discPath.isNotEmpty()) { if (isRunning) return
startDiscUpdate(discPath) isRunning = true
} else {
val region: String = when (this.region) { viewModelScope.launch {
0 -> "EUR" withContext(Dispatchers.IO) {
1 -> "JPN" if (discPath.isNotEmpty()) {
2 -> "KOR" startDiscUpdate(discPath)
3 -> "USA" } else {
else -> "" val region: String = when (region) {
0 -> "EUR"
1 -> "JPN"
2 -> "KOR"
3 -> "USA"
else -> ""
}
startOnlineUpdate(region)
}
isRunning = false
} }
startOnlineUpdate(region)
} }
} }
private fun startOnlineUpdate(region: String?) { private fun startOnlineUpdate(region: String) {
canceled = false canceled = false
executor.execute { val result = WiiUtils.doOnlineUpdate(region, constructCallback())
val result = WiiUtils.doOnlineUpdate(region, constructCallback()) resultData.postValue(result)
resultData.postValue(result)
}
} }
private fun startDiscUpdate(path: String?) { private fun startDiscUpdate(path: String) {
canceled = false canceled = false
executor.execute { val result = WiiUtils.doDiscUpdate(path, constructCallback())
val result = WiiUtils.doDiscUpdate(path, constructCallback()) resultData.postValue(result)
resultData.postValue(result)
}
} }
fun clear() { fun clear() {
@ -72,8 +80,4 @@ class SystemUpdateViewModel : ViewModel() {
!canceled !canceled
} }
} }
companion object {
private val executor = Executors.newFixedThreadPool(1)
}
} }