From 2b17e0334aa47c4266b70c6a7e08a143abdf9462 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Tue, 24 Jan 2023 10:55:33 -0500 Subject: [PATCH] Android: Convert SystemUpdateViewModel to Kotlin --- .../sysupdate/ui/SystemUpdateViewModel.java | 152 ------------------ .../sysupdate/ui/SystemUpdateViewModel.kt | 79 +++++++++ 2 files changed, 79 insertions(+), 152 deletions(-) delete mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/sysupdate/ui/SystemUpdateViewModel.java create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/sysupdate/ui/SystemUpdateViewModel.kt diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/sysupdate/ui/SystemUpdateViewModel.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/sysupdate/ui/SystemUpdateViewModel.java deleted file mode 100644 index a249b01ee7..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/sysupdate/ui/SystemUpdateViewModel.java +++ /dev/null @@ -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 mProgressData = new MutableLiveData<>(); - private final MutableLiveData mTotalData = new MutableLiveData<>(); - private final MutableLiveData mTitleIdData = new MutableLiveData<>(); - private final MutableLiveData 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 getProgressData() - { - return mProgressData; - } - - public MutableLiveData getTotalData() - { - return mTotalData; - } - - public MutableLiveData getTitleIdData() - { - return mTitleIdData; - } - - public MutableLiveData 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; - }; - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/sysupdate/ui/SystemUpdateViewModel.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/sysupdate/ui/SystemUpdateViewModel.kt new file mode 100644 index 0000000000..dacc51221d --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/sysupdate/ui/SystemUpdateViewModel.kt @@ -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() + val totalData = MutableLiveData() + val titleIdData = MutableLiveData() + val resultData = MutableLiveData() + + 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) + } +}