Android: Convert SystemUpdateViewModel to Kotlin

This commit is contained in:
Charles Lombardo 2023-01-24 10:55:33 -05:00
parent 6dfa555099
commit 2b17e0334a
2 changed files with 79 additions and 152 deletions

View File

@ -1,152 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.sysupdate.ui;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import org.dolphinemu.dolphinemu.utils.WiiUpdateCallback;
import org.dolphinemu.dolphinemu.utils.WiiUtils;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SystemUpdateViewModel extends ViewModel
{
private static final ExecutorService executor = Executors.newFixedThreadPool(1);
private final MutableLiveData<Integer> mProgressData = new MutableLiveData<>();
private final MutableLiveData<Integer> mTotalData = new MutableLiveData<>();
private final MutableLiveData<Long> mTitleIdData = new MutableLiveData<>();
private final MutableLiveData<Integer> mResultData = new MutableLiveData<>();
private boolean mCanceled = false;
private int mRegion;
private String mDiscPath;
public SystemUpdateViewModel()
{
clear();
}
public void setRegion(int region)
{
mRegion = region;
}
public int getRegion()
{
return mRegion;
}
public void setDiscPath(String discPath)
{
mDiscPath = discPath;
}
public String getDiscPath()
{
return mDiscPath;
}
public MutableLiveData<Integer> getProgressData()
{
return mProgressData;
}
public MutableLiveData<Integer> getTotalData()
{
return mTotalData;
}
public MutableLiveData<Long> getTitleIdData()
{
return mTitleIdData;
}
public MutableLiveData<Integer> getResultData()
{
return mResultData;
}
public void setCanceled()
{
mCanceled = true;
}
public void startUpdate()
{
if (!mDiscPath.isEmpty())
{
startDiscUpdate(mDiscPath);
}
else
{
final String region;
switch (mRegion)
{
case 0:
region = "EUR";
break;
case 1:
region = "JPN";
break;
case 2:
region = "KOR";
break;
case 3:
region = "USA";
break;
default:
region = "";
break;
}
startOnlineUpdate(region);
}
}
public void startOnlineUpdate(String region)
{
mCanceled = false;
executor.execute(() ->
{
int result = WiiUtils.doOnlineUpdate(region, constructCallback());
mResultData.postValue(result);
});
}
public void startDiscUpdate(String path)
{
mCanceled = false;
executor.execute(() ->
{
int result = WiiUtils.doDiscUpdate(path, constructCallback());
mResultData.postValue(result);
});
}
public void clear()
{
mProgressData.setValue(0);
mTotalData.setValue(0);
mTitleIdData.setValue(0l);
mResultData.setValue(-1);
mCanceled = false;
mRegion = -1;
mDiscPath = "";
}
private WiiUpdateCallback constructCallback()
{
return (processed, total, titleId) ->
{
mProgressData.postValue(processed);
mTotalData.postValue(total);
mTitleIdData.postValue(titleId);
return !mCanceled;
};
}
}

View File

@ -0,0 +1,79 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.sysupdate.ui
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
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 canceled = false
var region = -1
var discPath: String = ""
init {
clear()
}
fun setCanceled() {
canceled = true
}
fun startUpdate() {
if (discPath.isNotEmpty()) {
startDiscUpdate(discPath)
} else {
val region: String = when (this.region) {
0 -> "EUR"
1 -> "JPN"
2 -> "KOR"
3 -> "USA"
else -> ""
}
startOnlineUpdate(region)
}
}
private fun startOnlineUpdate(region: String?) {
canceled = false
executor.execute {
val result = WiiUtils.doOnlineUpdate(region, constructCallback())
resultData.postValue(result)
}
}
private fun startDiscUpdate(path: String?) {
canceled = false
executor.execute {
val result = WiiUtils.doDiscUpdate(path, constructCallback())
resultData.postValue(result)
}
}
fun clear() {
progressData.value = 0
totalData.value = 0
titleIdData.value = 0L
resultData.value = -1
}
private fun constructCallback(): WiiUpdateCallback {
return WiiUpdateCallback { processed: Int, total: Int, titleId: Long ->
progressData.postValue(processed)
totalData.postValue(total)
titleIdData.postValue(titleId)
!canceled
}
}
companion object {
private val executor = Executors.newFixedThreadPool(1)
}
}