From 59ecda79398a5da9c5c4ae5ac34f32d8b7497919 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Wed, 1 Mar 2023 13:36:40 -0500 Subject: [PATCH] Android: Convert CheatsViewModel to Kotlin --- .../cheats/model/CheatsViewModel.java | 321 ------------------ .../features/cheats/model/CheatsViewModel.kt | 207 +++++++++++ 2 files changed, 207 insertions(+), 321 deletions(-) delete mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.java create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.kt diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.java deleted file mode 100644 index 4d9900c168..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.java +++ /dev/null @@ -1,321 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later - -package org.dolphinemu.dolphinemu.features.cheats.model; - -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MutableLiveData; -import androidx.lifecycle.ViewModel; - -import java.util.ArrayList; -import java.util.Collections; - -public class CheatsViewModel extends ViewModel -{ - private boolean mLoaded = false; - - private int mSelectedCheatPosition = -1; - private final MutableLiveData mSelectedCheat = new MutableLiveData<>(null); - private final MutableLiveData mIsAdding = new MutableLiveData<>(false); - private final MutableLiveData mIsEditing = new MutableLiveData<>(false); - - private final MutableLiveData mCheatAddedEvent = new MutableLiveData<>(null); - private final MutableLiveData mCheatChangedEvent = new MutableLiveData<>(null); - private final MutableLiveData mCheatDeletedEvent = new MutableLiveData<>(null); - private final MutableLiveData mGeckoCheatsDownloadedEvent = new MutableLiveData<>(null); - private final MutableLiveData mOpenDetailsViewEvent = new MutableLiveData<>(false); - - private GraphicsModGroup mGraphicsModGroup; - private ArrayList mGraphicsMods; - private ArrayList mPatchCheats; - private ArrayList mARCheats; - private ArrayList mGeckoCheats; - - private boolean mGraphicsModsNeedSaving = false; - private boolean mPatchCheatsNeedSaving = false; - private boolean mARCheatsNeedSaving = false; - private boolean mGeckoCheatsNeedSaving = false; - - public void load(String gameID, int revision) - { - if (mLoaded) - return; - - mGraphicsModGroup = GraphicsModGroup.load(gameID); - mGraphicsMods = new ArrayList<>(); - Collections.addAll(mGraphicsMods, mGraphicsModGroup.getMods()); - - mPatchCheats = new ArrayList<>(); - Collections.addAll(mPatchCheats, PatchCheat.loadCodes(gameID, revision)); - - mARCheats = new ArrayList<>(); - Collections.addAll(mARCheats, ARCheat.loadCodes(gameID, revision)); - - mGeckoCheats = new ArrayList<>(); - Collections.addAll(mGeckoCheats, GeckoCheat.loadCodes(gameID, revision)); - - for (GraphicsMod mod : mGraphicsMods) - { - mod.setChangedCallback(() -> mGraphicsModsNeedSaving = true); - } - for (PatchCheat cheat : mPatchCheats) - { - cheat.setChangedCallback(() -> mPatchCheatsNeedSaving = true); - } - for (ARCheat cheat : mARCheats) - { - cheat.setChangedCallback(() -> mARCheatsNeedSaving = true); - } - for (GeckoCheat cheat : mGeckoCheats) - { - cheat.setChangedCallback(() -> mGeckoCheatsNeedSaving = true); - } - - mLoaded = true; - } - - public void saveIfNeeded(String gameID, int revision) - { - if (mGraphicsModsNeedSaving) - { - mGraphicsModGroup.save(); - mGraphicsModsNeedSaving = false; - } - - if (mPatchCheatsNeedSaving) - { - PatchCheat.saveCodes(gameID, revision, mPatchCheats.toArray(new PatchCheat[0])); - mPatchCheatsNeedSaving = false; - } - - if (mARCheatsNeedSaving) - { - ARCheat.saveCodes(gameID, revision, mARCheats.toArray(new ARCheat[0])); - mARCheatsNeedSaving = false; - } - - if (mGeckoCheatsNeedSaving) - { - GeckoCheat.saveCodes(gameID, revision, mGeckoCheats.toArray(new GeckoCheat[0])); - mGeckoCheatsNeedSaving = false; - } - } - - public LiveData getSelectedCheat() - { - return mSelectedCheat; - } - - public void setSelectedCheat(Cheat cheat, int position) - { - if (mIsEditing.getValue()) - setIsEditing(false); - - mSelectedCheat.setValue(cheat); - mSelectedCheatPosition = position; - } - - public LiveData getIsAdding() - { - return mIsAdding; - } - - public void startAddingCheat(Cheat cheat, int position) - { - mSelectedCheat.setValue(cheat); - mSelectedCheatPosition = position; - - mIsAdding.setValue(true); - mIsEditing.setValue(true); - } - - public void finishAddingCheat() - { - if (!mIsAdding.getValue()) - throw new IllegalStateException(); - - mIsAdding.setValue(false); - mIsEditing.setValue(false); - - Cheat cheat = mSelectedCheat.getValue(); - - if (cheat instanceof PatchCheat) - { - mPatchCheats.add((PatchCheat) mSelectedCheat.getValue()); - cheat.setChangedCallback(() -> mPatchCheatsNeedSaving = true); - mPatchCheatsNeedSaving = true; - } - else if (cheat instanceof ARCheat) - { - mARCheats.add((ARCheat) mSelectedCheat.getValue()); - cheat.setChangedCallback(() -> mPatchCheatsNeedSaving = true); - mARCheatsNeedSaving = true; - } - else if (cheat instanceof GeckoCheat) - { - mGeckoCheats.add((GeckoCheat) mSelectedCheat.getValue()); - cheat.setChangedCallback(() -> mGeckoCheatsNeedSaving = true); - mGeckoCheatsNeedSaving = true; - } - else - { - throw new UnsupportedOperationException(); - } - - notifyCheatAdded(); - } - - public LiveData getIsEditing() - { - return mIsEditing; - } - - public void setIsEditing(boolean isEditing) - { - mIsEditing.setValue(isEditing); - - if (mIsAdding.getValue() && !isEditing) - { - mIsAdding.setValue(false); - setSelectedCheat(null, -1); - } - } - - /** - * When a cheat is added, the integer stored in the returned LiveData - * changes to the position of that cheat, then changes back to null. - */ - public LiveData getCheatAddedEvent() - { - return mCheatAddedEvent; - } - - private void notifyCheatAdded() - { - mCheatAddedEvent.setValue(mSelectedCheatPosition); - mCheatAddedEvent.setValue(null); - } - - /** - * When a cheat is edited, the integer stored in the returned LiveData - * changes to the position of that cheat, then changes back to null. - */ - public LiveData getCheatChangedEvent() - { - return mCheatChangedEvent; - } - - /** - * Notifies that an edit has been made to the contents of the currently selected cheat. - */ - public void notifySelectedCheatChanged() - { - notifyCheatChanged(mSelectedCheatPosition); - } - - /** - * Notifies that an edit has been made to the contents of the cheat at the given position. - */ - public void notifyCheatChanged(int position) - { - mCheatChangedEvent.setValue(position); - mCheatChangedEvent.setValue(null); - } - - /** - * When a cheat is deleted, the integer stored in the returned LiveData - * changes to the position of that cheat, then changes back to null. - */ - public LiveData getCheatDeletedEvent() - { - return mCheatDeletedEvent; - } - - public void deleteSelectedCheat() - { - Cheat cheat = mSelectedCheat.getValue(); - int position = mSelectedCheatPosition; - - setSelectedCheat(null, -1); - - if (mPatchCheats.remove(cheat)) - mPatchCheatsNeedSaving = true; - if (mARCheats.remove(cheat)) - mARCheatsNeedSaving = true; - if (mGeckoCheats.remove(cheat)) - mGeckoCheatsNeedSaving = true; - - notifyCheatDeleted(position); - } - - /** - * Notifies that the cheat at the given position has been deleted. - */ - private void notifyCheatDeleted(int position) - { - mCheatDeletedEvent.setValue(position); - mCheatDeletedEvent.setValue(null); - } - - /** - * When Gecko cheats are downloaded, the integer stored in the returned LiveData - * changes to the number of cheats added, then changes back to null. - */ - public LiveData getGeckoCheatsDownloadedEvent() - { - return mGeckoCheatsDownloadedEvent; - } - - public int addDownloadedGeckoCodes(GeckoCheat[] cheats) - { - int cheatsAdded = 0; - - for (GeckoCheat cheat : cheats) - { - if (!mGeckoCheats.contains(cheat)) - { - mGeckoCheats.add(cheat); - cheatsAdded++; - } - } - - if (cheatsAdded != 0) - { - mGeckoCheatsNeedSaving = true; - mGeckoCheatsDownloadedEvent.setValue(cheatsAdded); - mGeckoCheatsDownloadedEvent.setValue(null); - } - - return cheatsAdded; - } - - public LiveData getOpenDetailsViewEvent() - { - return mOpenDetailsViewEvent; - } - - public void openDetailsView() - { - mOpenDetailsViewEvent.setValue(true); - mOpenDetailsViewEvent.setValue(false); - } - - public ArrayList getGraphicsMods() - { - return mGraphicsMods; - } - - public ArrayList getPatchCheats() - { - return mPatchCheats; - } - - public ArrayList getARCheats() - { - return mARCheats; - } - - public ArrayList getGeckoCheats() - { - return mGeckoCheats; - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.kt new file mode 100644 index 0000000000..e2d52bbc78 --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.kt @@ -0,0 +1,207 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.dolphinemu.dolphinemu.features.cheats.model + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import org.dolphinemu.dolphinemu.features.cheats.model.ARCheat.Companion.loadCodes +import org.dolphinemu.dolphinemu.features.cheats.model.ARCheat.Companion.saveCodes +import kotlin.collections.ArrayList + +class CheatsViewModel : ViewModel() { + private var loaded = false + + private var selectedCheatPosition = -1 + private val _selectedCheat = MutableLiveData(null) + val selectedCheat: LiveData get() = _selectedCheat + private val _isAdding = MutableLiveData(false) + val isAdding: LiveData get() = _isAdding + private val _isEditing = MutableLiveData(false) + val isEditing: LiveData get() = _isEditing + + private val _cheatAddedEvent = MutableLiveData(null) + val cheatAddedEvent: LiveData get() = _cheatAddedEvent + private val _cheatChangedEvent = MutableLiveData(null) + val cheatChangedEvent: LiveData get() = _cheatChangedEvent + private val _cheatDeletedEvent = MutableLiveData(null) + val cheatDeletedEvent: LiveData get() = _cheatDeletedEvent + private val _geckoCheatsDownloadedEvent = MutableLiveData(null) + val geckoCheatsDownloadedEvent: LiveData get() = _geckoCheatsDownloadedEvent + private val _openDetailsViewEvent = MutableLiveData(false) + val openDetailsViewEvent: LiveData get() = _openDetailsViewEvent + + private var graphicsModGroup: GraphicsModGroup? = null + var graphicsMods: ArrayList = ArrayList() + var patchCheats: ArrayList = ArrayList() + var aRCheats: ArrayList = ArrayList() + var geckoCheats: ArrayList = ArrayList() + + private var graphicsModsNeedSaving = false + private var patchCheatsNeedSaving = false + private var aRCheatsNeedSaving = false + private var geckoCheatsNeedSaving = false + + fun load(gameID: String, revision: Int) { + if (loaded) return + + graphicsModGroup = GraphicsModGroup.load(gameID) + graphicsMods.addAll(graphicsModGroup!!.mods) + patchCheats.addAll(PatchCheat.loadCodes(gameID, revision)) + aRCheats.addAll(loadCodes(gameID, revision)) + geckoCheats.addAll(GeckoCheat.loadCodes(gameID, revision)) + + for (mod in graphicsMods) { + mod.setChangedCallback { graphicsModsNeedSaving = true } + } + for (cheat in patchCheats) { + cheat.setChangedCallback { patchCheatsNeedSaving = true } + } + for (cheat in aRCheats) { + cheat.setChangedCallback { aRCheatsNeedSaving = true } + } + for (cheat in geckoCheats) { + cheat.setChangedCallback { geckoCheatsNeedSaving = true } + } + + loaded = true + } + + fun saveIfNeeded(gameID: String, revision: Int) { + if (graphicsModsNeedSaving) { + graphicsModGroup!!.save() + graphicsModsNeedSaving = false + } + + if (patchCheatsNeedSaving) { + PatchCheat.saveCodes(gameID, revision, patchCheats.toTypedArray()) + patchCheatsNeedSaving = false + } + + if (aRCheatsNeedSaving) { + saveCodes(gameID, revision, aRCheats.toTypedArray()) + aRCheatsNeedSaving = false + } + + if (geckoCheatsNeedSaving) { + GeckoCheat.saveCodes(gameID, revision, geckoCheats.toTypedArray()) + geckoCheatsNeedSaving = false + } + } + + fun setSelectedCheat(cheat: Cheat?, position: Int) { + if (isEditing.value!!) setIsEditing(false) + + _selectedCheat.value = cheat + selectedCheatPosition = position + } + + fun startAddingCheat(cheat: Cheat?, position: Int) { + _selectedCheat.value = cheat + selectedCheatPosition = position + + _isAdding.value = true + _isEditing.value = true + } + + fun finishAddingCheat() { + check(isAdding.value!!) + + _isAdding.value = false + _isEditing.value = false + + when (val cheat = selectedCheat.value) { + is PatchCheat -> { + patchCheats.add(cheat) + cheat.setChangedCallback(Runnable { patchCheatsNeedSaving = true }) + patchCheatsNeedSaving = true + } + is ARCheat -> { + aRCheats.add(cheat) + cheat.setChangedCallback(Runnable { patchCheatsNeedSaving = true }) + aRCheatsNeedSaving = true + } + is GeckoCheat -> { + geckoCheats.add(cheat) + cheat.setChangedCallback(Runnable { geckoCheatsNeedSaving = true }) + geckoCheatsNeedSaving = true + } + else -> throw UnsupportedOperationException() + } + + notifyCheatAdded() + } + + fun setIsEditing(isEditing: Boolean) { + _isEditing.value = isEditing + if (isAdding.value!! && !isEditing) { + _isAdding.value = false + setSelectedCheat(null, -1) + } + } + + private fun notifyCheatAdded() { + _cheatAddedEvent.value = selectedCheatPosition + _cheatAddedEvent.value = null + } + + /** + * Notifies that an edit has been made to the contents of the currently selected cheat. + */ + fun notifySelectedCheatChanged() { + notifyCheatChanged(selectedCheatPosition) + } + + /** + * Notifies that an edit has been made to the contents of the cheat at the given position. + */ + private fun notifyCheatChanged(position: Int) { + _cheatChangedEvent.value = position + _cheatChangedEvent.value = null + } + + fun deleteSelectedCheat() { + val cheat = selectedCheat.value + val position = selectedCheatPosition + + setSelectedCheat(null, -1) + + if (patchCheats.remove(cheat)) patchCheatsNeedSaving = true + if (aRCheats.remove(cheat)) aRCheatsNeedSaving = true + if (geckoCheats.remove(cheat)) geckoCheatsNeedSaving = true + + notifyCheatDeleted(position) + } + + /** + * Notifies that the cheat at the given position has been deleted. + */ + private fun notifyCheatDeleted(position: Int) { + _cheatDeletedEvent.value = position + _cheatDeletedEvent.value = null + } + + fun addDownloadedGeckoCodes(cheats: Array): Int { + var cheatsAdded = 0 + + for (cheat in cheats) { + if (!geckoCheats.contains(cheat)) { + geckoCheats.add(cheat) + cheatsAdded++ + } + } + + if (cheatsAdded != 0) { + geckoCheatsNeedSaving = true + _geckoCheatsDownloadedEvent.value = cheatsAdded + _geckoCheatsDownloadedEvent.value = null + } + + return cheatsAdded + } + + fun openDetailsView() { + _openDetailsViewEvent.value = true + _openDetailsViewEvent.value = false + } +}