Skip to content

Commit

Permalink
Android: Use coroutine for system updates
Browse files Browse the repository at this point in the history
  • Loading branch information
t895 committed Jan 26, 2023
1 parent 5279a21 commit 55ec62a
Showing 1 changed file with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ 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>()
val totalData = MutableLiveData<Int>()
val titleIdData = MutableLiveData<Long>()
val resultData = MutableLiveData<Int>()

private var isRunning = false
private var canceled = false
var region = -1
var discPath: String = ""
Expand All @@ -27,34 +31,38 @@ class SystemUpdateViewModel : ViewModel() {
}

fun startUpdate() {
if (discPath.isNotEmpty()) {
startDiscUpdate(discPath)
} else {
val region: String = when (this.region) {
0 -> "EUR"
1 -> "JPN"
2 -> "KOR"
3 -> "USA"
else -> ""
if (isRunning) return
isRunning = true

viewModelScope.launch {
withContext(Dispatchers.IO) {
if (discPath.isNotEmpty()) {
startDiscUpdate(discPath)
} 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
executor.execute {
val result = WiiUtils.doOnlineUpdate(region, constructCallback())
resultData.postValue(result)
}
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)
}
val result = WiiUtils.doDiscUpdate(path, constructCallback())
resultData.postValue(result)
}

fun clear() {
Expand All @@ -72,8 +80,4 @@ class SystemUpdateViewModel : ViewModel() {
!canceled
}
}

companion object {
private val executor = Executors.newFixedThreadPool(1)
}
}

0 comments on commit 55ec62a

Please sign in to comment.