Android: Add details view for cheats

The details view only contains the name of the cheat for now.
This commit is contained in:
JosJuice 2021-08-04 22:38:14 +02:00
parent 93a1271386
commit 95879c2e76
9 changed files with 152 additions and 7 deletions

View File

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

View File

@ -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<Cheat> mSelectedCheat = new MutableLiveData<>(null);
private PatchCheat[] mPatchCheats;
private ARCheat[] mARCheats;
private GeckoCheat[] mGeckoCheats;
@ -62,6 +66,16 @@ public class CheatsViewModel extends ViewModel
}
}
public LiveData<Cheat> getSelectedCheat()
{
return mSelectedCheat;
}
public void setSelectedCheat(Cheat cheat)
{
mSelectedCheat.setValue(cheat);
}
public Cheat[] getPatchCheats()
{
return mPatchCheats;

View File

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

View File

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

View File

@ -34,7 +34,7 @@ public class CheatsAdapter extends RecyclerView.Adapter<CheatViewHolder>
@Override
public void onBindViewHolder(@NonNull CheatViewHolder holder, int position)
{
holder.bind(getItemAt(position));
holder.bind(mViewModel, getItemAt(position));
}
@Override

View File

@ -1,7 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
<androidx.slidingpanelayout.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sliding_pane_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cheat_list"
android:name="org.dolphinemu.dolphinemu.features.cheats.ui.CheatListFragment" />
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/cheat_list"
android:name="org.dolphinemu.dolphinemu.features.cheats.ui.CheatListFragment" />
<androidx.fragment.app.FragmentContainerView
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/cheat_details"
android:name="org.dolphinemu.dolphinemu.features.cheats.ui.CheatDetailsFragment" />
</androidx.slidingpanelayout.widget.SlidingPaneLayout>

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:id="@+id/label_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Headline"
android:textSize="18sp"
android:text="@string/cheats_name"
android:layout_margin="@dimen/spacing_large"
android:labelFor="@id/edit_name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/edit_name" />
<EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:layout_marginHorizontal="@dimen/spacing_large"
android:importantForAutofill="no"
android:inputType="text"
android:enabled="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/label_name"
tools:text="Hyrule Field Speed Hack" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

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

View File

@ -389,6 +389,7 @@
<!-- Cheats Screen -->
<string name="cheats">Cheats</string>
<string name="cheats_with_game_id">Cheats: %1$s</string>
<string name="cheats_name">Name</string>
<!-- Convert Screen -->
<string name="convert_format">Format</string>