diff options
| author | Charles Lombardo <clombardo169@gmail.com> | 2023-01-24 10:55:33 -0500 |
|---|---|---|
| committer | Charles Lombardo <clombardo169@gmail.com> | 2023-03-07 15:45:40 -0500 |
| commit | 2b17e0334aa47c4266b70c6a7e08a143abdf9462 (patch) | |
| tree | 2ac166453a668400d4a56023d6c0b2483ba9ba34 /Source/Android/app/src/main/java | |
| parent | 6dfa5550998fdf6d33ad60f5f5d6b00f6840f728 (diff) | |
Android: Convert SystemUpdateViewModel to Kotlin
Diffstat (limited to 'Source/Android/app/src/main/java')
2 files changed, 79 insertions, 152 deletions
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<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; - }; - } -} 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<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) + } +} |
