From 95879c2e76a68526079445b427d1f3740416c978 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Wed, 4 Aug 2021 22:38:14 +0200 Subject: [PATCH] Android: Add details view for cheats The details view only contains the name of the cheat for now. --- Source/Android/app/build.gradle | 1 + .../cheats/model/CheatsViewModel.java | 14 +++++ .../cheats/ui/CheatDetailsFragment.java | 62 +++++++++++++++++++ .../features/cheats/ui/CheatViewHolder.java | 16 ++++- .../features/cheats/ui/CheatsAdapter.java | 2 +- .../src/main/res/layout/activity_cheats.xml | 23 +++++-- .../res/layout/fragment_cheat_details.xml | 39 ++++++++++++ .../src/main/res/layout/list_item_cheat.xml | 1 + .../app/src/main/res/values/strings.xml | 1 + 9 files changed, 152 insertions(+), 7 deletions(-) create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatDetailsFragment.java create mode 100644 Source/Android/app/src/main/res/layout/fragment_cheat_details.xml diff --git a/Source/Android/app/build.gradle b/Source/Android/app/build.gradle index ebcee3d5cd..57715c02dc 100644 --- a/Source/Android/app/build.gradle +++ b/Source/Android/app/build.gradle @@ -91,6 +91,7 @@ dependencies { implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'androidx.lifecycle:lifecycle-viewmodel:2.3.1' implementation 'androidx.fragment:fragment:1.3.6' + implementation "androidx.slidingpanelayout:slidingpanelayout:1.2.0-alpha03" implementation 'com.google.android.material:material:1.4.0' // Android TV UI libraries. 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 c2cadaf8c0..d05da7d978 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 @@ -2,12 +2,16 @@ package org.dolphinemu.dolphinemu.features.cheats.model; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; public class CheatsViewModel extends ViewModel { private boolean mLoaded = false; + private final MutableLiveData mSelectedCheat = new MutableLiveData<>(null); + private PatchCheat[] mPatchCheats; private ARCheat[] mARCheats; private GeckoCheat[] mGeckoCheats; @@ -62,6 +66,16 @@ public class CheatsViewModel extends ViewModel } } + public LiveData getSelectedCheat() + { + return mSelectedCheat; + } + + public void setSelectedCheat(Cheat cheat) + { + mSelectedCheat.setValue(cheat); + } + public Cheat[] getPatchCheats() { return mPatchCheats; 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 new file mode 100644 index 0000000000..90356b63eb --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatDetailsFragment.java @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.dolphinemu.dolphinemu.features.cheats.ui; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.EditText; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.ViewModelProvider; + +import org.dolphinemu.dolphinemu.R; +import org.dolphinemu.dolphinemu.features.cheats.model.Cheat; +import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel; + +public class CheatDetailsFragment extends Fragment +{ + private View mRoot; + private EditText mEditName; + + private CheatsViewModel mViewModel; + private Cheat mCheat; + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState) + { + return inflater.inflate(R.layout.fragment_cheat_details, container, false); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) + { + mRoot = view.findViewById(R.id.root); + mEditName = view.findViewById(R.id.edit_name); + + CheatsActivity activity = (CheatsActivity) requireActivity(); + mViewModel = new ViewModelProvider(activity).get(CheatsViewModel.class); + + LiveData selectedCheat = mViewModel.getSelectedCheat(); + selectedCheat.observe(getViewLifecycleOwner(), this::populateFields); + populateFields(selectedCheat.getValue()); + } + + private void populateFields(@Nullable Cheat cheat) + { + mRoot.setVisibility(cheat == null ? View.GONE : View.VISIBLE); + + if (cheat != null && cheat != mCheat) + { + mEditName.setText(cheat.getName()); + } + + mCheat = cheat; + } +} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatViewHolder.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatViewHolder.java index d3fa67905b..adaa5949a6 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatViewHolder.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/cheats/ui/CheatViewHolder.java @@ -12,34 +12,46 @@ import androidx.recyclerview.widget.RecyclerView.ViewHolder; import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.features.cheats.model.Cheat; +import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel; -public class CheatViewHolder extends ViewHolder implements CompoundButton.OnCheckedChangeListener +public class CheatViewHolder extends ViewHolder + implements View.OnClickListener, CompoundButton.OnCheckedChangeListener { + private final View mRoot; private final TextView mName; private final CheckBox mCheckbox; + private CheatsViewModel mViewModel; private Cheat mCheat; public CheatViewHolder(@NonNull View itemView) { super(itemView); + mRoot = itemView.findViewById(R.id.root); mName = itemView.findViewById(R.id.text_name); mCheckbox = itemView.findViewById(R.id.checkbox); } - public void bind(Cheat item) + public void bind(CheatsViewModel viewModel, Cheat item) { mCheckbox.setOnCheckedChangeListener(null); mName.setText(item.getName()); mCheckbox.setChecked(item.getEnabled()); + mViewModel = viewModel; mCheat = item; + mRoot.setOnClickListener(this); mCheckbox.setOnCheckedChangeListener(this); } + public void onClick(View root) + { + mViewModel.setSelectedCheat(mCheat); + } + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mCheat.setEnabled(isChecked); 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 6193b6c52f..3638ab383f 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 @@ -34,7 +34,7 @@ public class CheatsAdapter extends RecyclerView.Adapter @Override public void onBindViewHolder(@NonNull CheatViewHolder holder, int position) { - holder.bind(getItemAt(position)); + holder.bind(mViewModel, getItemAt(position)); } @Override diff --git a/Source/Android/app/src/main/res/layout/activity_cheats.xml b/Source/Android/app/src/main/res/layout/activity_cheats.xml index 21279342a7..0f89b488e1 100644 --- a/Source/Android/app/src/main/res/layout/activity_cheats.xml +++ b/Source/Android/app/src/main/res/layout/activity_cheats.xml @@ -1,7 +1,22 @@ - + android:layout_height="match_parent"> + + + + + + diff --git a/Source/Android/app/src/main/res/layout/fragment_cheat_details.xml b/Source/Android/app/src/main/res/layout/fragment_cheat_details.xml new file mode 100644 index 0000000000..14c7dfeace --- /dev/null +++ b/Source/Android/app/src/main/res/layout/fragment_cheat_details.xml @@ -0,0 +1,39 @@ + + + + + + + + diff --git a/Source/Android/app/src/main/res/layout/list_item_cheat.xml b/Source/Android/app/src/main/res/layout/list_item_cheat.xml index 46bd84196a..5d79260b68 100644 --- a/Source/Android/app/src/main/res/layout/list_item_cheat.xml +++ b/Source/Android/app/src/main/res/layout/list_item_cheat.xml @@ -3,6 +3,7 @@ xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" + android:id="@+id/root" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="true"> diff --git a/Source/Android/app/src/main/res/values/strings.xml b/Source/Android/app/src/main/res/values/strings.xml index 43fb50fd47..8582608000 100644 --- a/Source/Android/app/src/main/res/values/strings.xml +++ b/Source/Android/app/src/main/res/values/strings.xml @@ -389,6 +389,7 @@ Cheats Cheats: %1$s + Name Format