summaryrefslogtreecommitdiff
path: root/Source/Android/app/src/main/java
diff options
context:
space:
mode:
authorJosJuice <josjuice@gmail.com>2021-08-10 15:42:35 +0200
committerJosJuice <josjuice@gmail.com>2021-09-16 18:48:39 +0200
commit1470dfcf81e9e7f71b2548f6f7253423140e29dd (patch)
treef83466b3ecc2454f4588fa3233a2c2ef3a7ffb3d /Source/Android/app/src/main/java
parent404eb13e2f5252739a4a778bc549abc1867f284e (diff)
Android: Add the ability to delete cheats
Diffstat (limited to 'Source/Android/app/src/main/java')
-rw-r--r--Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/model/CheatsViewModel.java36
-rw-r--r--Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatDetailsFragment.java16
-rw-r--r--Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsAdapter.java6
3 files changed, 58 insertions, 0 deletions
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
index f405c427a5..750c921c94 100644
--- 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
@@ -20,6 +20,7 @@ public class CheatsViewModel extends ViewModel
private final MutableLiveData<Integer> mCheatAddedEvent = new MutableLiveData<>(null);
private final MutableLiveData<Integer> mCheatChangedEvent = new MutableLiveData<>(null);
+ private final MutableLiveData<Integer> mCheatDeletedEvent = new MutableLiveData<>(null);
private final MutableLiveData<Boolean> mOpenDetailsViewEvent = new MutableLiveData<>(false);
private ArrayList<PatchCheat> mPatchCheats;
@@ -200,6 +201,41 @@ public class CheatsViewModel extends ViewModel
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<Integer> 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);
+ }
+
public LiveData<Boolean> getOpenDetailsViewEvent()
{
return mOpenDetailsViewEvent;
diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatDetailsFragment.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatDetailsFragment.java
index 3de19df37a..421b0aa094 100644
--- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatDetailsFragment.java
+++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatDetailsFragment.java
@@ -13,6 +13,7 @@ import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
+import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModelProvider;
@@ -32,6 +33,7 @@ public class CheatDetailsFragment extends Fragment
private TextView mLabelNotes;
private EditText mEditNotes;
private EditText mEditCode;
+ private Button mButtonDelete;
private Button mButtonEdit;
private Button mButtonCancel;
private Button mButtonOk;
@@ -59,6 +61,7 @@ public class CheatDetailsFragment extends Fragment
mLabelNotes = view.findViewById(R.id.label_notes);
mEditNotes = view.findViewById(R.id.edit_notes);
mEditCode = view.findViewById(R.id.edit_code);
+ mButtonDelete = view.findViewById(R.id.button_delete);
mButtonEdit = view.findViewById(R.id.button_edit);
mButtonCancel = view.findViewById(R.id.button_cancel);
mButtonOk = view.findViewById(R.id.button_ok);
@@ -69,6 +72,7 @@ public class CheatDetailsFragment extends Fragment
mViewModel.getSelectedCheat().observe(getViewLifecycleOwner(), this::onSelectedCheatUpdated);
mViewModel.getIsEditing().observe(getViewLifecycleOwner(), this::onIsEditingUpdated);
+ mButtonDelete.setOnClickListener(this::onDeleteClicked);
mButtonEdit.setOnClickListener((v) -> mViewModel.setIsEditing(true));
mButtonCancel.setOnClickListener((v) ->
{
@@ -84,6 +88,16 @@ public class CheatDetailsFragment extends Fragment
mEditCode.setError(null);
}
+ private void onDeleteClicked(View view)
+ {
+ AlertDialog.Builder builder =
+ new AlertDialog.Builder(requireContext(), R.style.DolphinDialogBase);
+ builder.setMessage(getString(R.string.cheats_delete_confirmation, mCheat.getName()));
+ builder.setPositiveButton(R.string.yes, (dialog, i) -> mViewModel.deleteSelectedCheat());
+ builder.setNegativeButton(R.string.no, null);
+ builder.show();
+ }
+
private void onOkClicked(View view)
{
clearEditErrors();
@@ -138,6 +152,7 @@ public class CheatDetailsFragment extends Fragment
mEditNotes.setVisibility(notesVisibility);
boolean userDefined = cheat != null && cheat.getUserDefined();
+ mButtonDelete.setEnabled(userDefined);
mButtonEdit.setEnabled(userDefined);
// If the fragment was recreated while editing a cheat, it's vital that we
@@ -162,6 +177,7 @@ public class CheatDetailsFragment extends Fragment
mEditNotes.setEnabled(isEditing);
mEditCode.setEnabled(isEditing);
+ mButtonDelete.setVisibility(isEditing ? View.GONE : View.VISIBLE);
mButtonEdit.setVisibility(isEditing ? View.GONE : View.VISIBLE);
mButtonCancel.setVisibility(isEditing ? View.VISIBLE : View.GONE);
mButtonOk.setVisibility(isEditing ? View.VISIBLE : View.GONE);
diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsAdapter.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsAdapter.java
index 3406e1b692..c12cd58c9f 100644
--- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsAdapter.java
+++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatsAdapter.java
@@ -37,6 +37,12 @@ public class CheatsAdapter extends RecyclerView.Adapter<CheatItemViewHolder>
if (position != null)
notifyItemChanged(position);
});
+
+ mViewModel.getCheatDeletedEvent().observe(owner, (position) ->
+ {
+ if (position != null)
+ notifyItemRemoved(position);
+ });
}
@NonNull