Android: Add the ability to delete cheats

This commit is contained in:
JosJuice 2021-08-10 15:42:35 +02:00
parent 404eb13e2f
commit 1470dfcf81
5 changed files with 73 additions and 2 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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

View File

@ -141,7 +141,18 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="top"
app:constraint_referenced_ids="button_edit,button_cancel,button_ok" />
app:constraint_referenced_ids="button_delete,button_edit,button_cancel,button_ok" />
<Button
android:id="@+id/button_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_large"
android:text="@string/cheats_delete"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/button_edit"
app:layout_constraintTop_toBottomOf="@id/barrier"
app:layout_constraintBottom_toBottomOf="parent" />
<Button
android:id="@+id/button_edit"
@ -149,7 +160,7 @@
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_large"
android:text="@string/cheats_edit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintStart_toEndOf="@id/button_delete"
app:layout_constraintEnd_toStartOf="@id/button_cancel"
app:layout_constraintTop_toBottomOf="@id/barrier"
app:layout_constraintBottom_toBottomOf="parent" />

View File

@ -400,6 +400,8 @@
<string name="cheats_notes">Notes</string>
<string name="cheats_code">Code</string>
<string name="cheats_edit">Edit</string>
<string name="cheats_delete">Delete</string>
<string name="cheats_delete_confirmation">Are you sure you want to delete "%1$s"?</string>
<string name="cheats_error_no_name">Name can\'t be empty</string>
<string name="cheats_error_no_code_lines">Code can\'t be empty</string>
<string name="cheats_error_on_line">Error on line %1$d</string>