Android: Use coroutine for system updates
This commit is contained in:
parent
d66d8210bf
commit
75ce7a04ca
|
@ -4,9 +4,12 @@ package org.dolphinemu.dolphinemu.features.sysupdate.ui
|
|||
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
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.WiiUtils
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
class SystemUpdateViewModel : ViewModel() {
|
||||
val progressData = MutableLiveData<Int>()
|
||||
|
@ -14,6 +17,7 @@ class SystemUpdateViewModel : ViewModel() {
|
|||
val titleIdData = MutableLiveData<Long>()
|
||||
val resultData = MutableLiveData<Int>()
|
||||
|
||||
private var isRunning = false
|
||||
private var canceled = false
|
||||
var region = -1
|
||||
var discPath: String = ""
|
||||
|
@ -27,10 +31,15 @@ class SystemUpdateViewModel : ViewModel() {
|
|||
}
|
||||
|
||||
fun startUpdate() {
|
||||
if (isRunning) return
|
||||
isRunning = true
|
||||
|
||||
viewModelScope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
if (discPath.isNotEmpty()) {
|
||||
startDiscUpdate(discPath)
|
||||
} else {
|
||||
val region: String = when (this.region) {
|
||||
val region: String = when (region) {
|
||||
0 -> "EUR"
|
||||
1 -> "JPN"
|
||||
2 -> "KOR"
|
||||
|
@ -39,23 +48,22 @@ class SystemUpdateViewModel : ViewModel() {
|
|||
}
|
||||
startOnlineUpdate(region)
|
||||
}
|
||||
isRunning = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun startOnlineUpdate(region: String?) {
|
||||
private fun startOnlineUpdate(region: String) {
|
||||
canceled = false
|
||||
executor.execute {
|
||||
val result = WiiUtils.doOnlineUpdate(region, constructCallback())
|
||||
resultData.postValue(result)
|
||||
}
|
||||
}
|
||||
|
||||
private fun startDiscUpdate(path: String?) {
|
||||
private fun startDiscUpdate(path: String) {
|
||||
canceled = false
|
||||
executor.execute {
|
||||
val result = WiiUtils.doDiscUpdate(path, constructCallback())
|
||||
resultData.postValue(result)
|
||||
}
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
progressData.value = 0
|
||||
|
@ -72,8 +80,4 @@ class SystemUpdateViewModel : ViewModel() {
|
|||
!canceled
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val executor = Executors.newFixedThreadPool(1)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue