Android: Add headers to cheat list

This commit is contained in:
JosJuice 2021-08-10 12:58:19 +02:00
parent 109aef4b81
commit 6934b9a21d
8 changed files with 144 additions and 29 deletions

View File

@ -0,0 +1,48 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.cheats.ui;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.dolphinemu.dolphinemu.features.cheats.model.Cheat;
public class CheatItem
{
public static final int TYPE_CHEAT = 0;
public static final int TYPE_HEADER = 1;
private final @Nullable Cheat mCheat;
private final int mString;
private final int mType;
public CheatItem(@NonNull Cheat cheat)
{
mCheat = cheat;
mString = 0;
mType = TYPE_CHEAT;
}
public CheatItem(int type, int string)
{
mCheat = null;
mString = string;
mType = type;
}
@Nullable
public Cheat getCheat()
{
return mCheat;
}
public int getString()
{
return mString;
}
public int getType()
{
return mType;
}
}

View File

@ -0,0 +1,20 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.cheats.ui;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel;
public abstract class CheatItemViewHolder extends RecyclerView.ViewHolder
{
public CheatItemViewHolder(@NonNull View itemView)
{
super(itemView);
}
public abstract void bind(CheatsViewModel viewModel, CheatItem item, int position);
}

View File

@ -14,7 +14,7 @@ import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.features.cheats.model.Cheat; import org.dolphinemu.dolphinemu.features.cheats.model.Cheat;
import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel; import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel;
public class CheatViewHolder extends ViewHolder public class CheatViewHolder extends CheatItemViewHolder
implements View.OnClickListener, CompoundButton.OnCheckedChangeListener implements View.OnClickListener, CompoundButton.OnCheckedChangeListener
{ {
private final View mRoot; private final View mRoot;
@ -34,17 +34,17 @@ public class CheatViewHolder extends ViewHolder
mCheckbox = itemView.findViewById(R.id.checkbox); mCheckbox = itemView.findViewById(R.id.checkbox);
} }
public void bind(CheatsViewModel viewModel, Cheat item, int position) public void bind(CheatsViewModel viewModel, CheatItem item, int position)
{ {
mCheckbox.setOnCheckedChangeListener(null); mCheckbox.setOnCheckedChangeListener(null);
mName.setText(item.getName());
mCheckbox.setChecked(item.getEnabled());
mViewModel = viewModel; mViewModel = viewModel;
mCheat = item; mCheat = item.getCheat();
mPosition = position; mPosition = position;
mName.setText(mCheat.getName());
mCheckbox.setChecked(mCheat.getEnabled());
mRoot.setOnClickListener(this); mRoot.setOnClickListener(this);
mCheckbox.setOnCheckedChangeListener(this); mCheckbox.setOnCheckedChangeListener(this);
} }

View File

@ -14,7 +14,7 @@ import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.features.cheats.model.Cheat; import org.dolphinemu.dolphinemu.features.cheats.model.Cheat;
import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel; import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel;
public class CheatsAdapter extends RecyclerView.Adapter<CheatViewHolder> public class CheatsAdapter extends RecyclerView.Adapter<CheatItemViewHolder>
{ {
private final CheatsViewModel mViewModel; private final CheatsViewModel mViewModel;
@ -31,15 +31,25 @@ public class CheatsAdapter extends RecyclerView.Adapter<CheatViewHolder>
@NonNull @NonNull
@Override @Override
public CheatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) public CheatItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{ {
LayoutInflater inflater = LayoutInflater.from(parent.getContext()); LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.list_item_cheat, parent, false);
return new CheatViewHolder(view); switch (viewType)
{
case CheatItem.TYPE_CHEAT:
View cheatView = inflater.inflate(R.layout.list_item_cheat, parent, false);
return new CheatViewHolder(cheatView);
case CheatItem.TYPE_HEADER:
View headerView = inflater.inflate(R.layout.list_item_header, parent, false);
return new HeaderViewHolder(headerView);
default:
throw new UnsupportedOperationException();
}
} }
@Override @Override
public void onBindViewHolder(@NonNull CheatViewHolder holder, int position) public void onBindViewHolder(@NonNull CheatItemViewHolder holder, int position)
{ {
holder.bind(mViewModel, getItemAt(position), position); holder.bind(mViewModel, getItemAt(position), position);
} }
@ -48,30 +58,42 @@ public class CheatsAdapter extends RecyclerView.Adapter<CheatViewHolder>
public int getItemCount() public int getItemCount()
{ {
return mViewModel.getARCheats().length + mViewModel.getGeckoCheats().length + return mViewModel.getARCheats().length + mViewModel.getGeckoCheats().length +
mViewModel.getPatchCheats().length; mViewModel.getPatchCheats().length + 3;
} }
private Cheat getItemAt(int position) @Override
public int getItemViewType(int position)
{ {
return getItemAt(position).getType();
}
private CheatItem getItemAt(int position)
{
if (position == 0)
return new CheatItem(CheatItem.TYPE_HEADER, R.string.cheats_header_patch);
position -= 1;
Cheat[] patchCheats = mViewModel.getPatchCheats(); Cheat[] patchCheats = mViewModel.getPatchCheats();
if (position < patchCheats.length) if (position < patchCheats.length)
{ return new CheatItem(patchCheats[position]);
return patchCheats[position];
}
position -= patchCheats.length; position -= patchCheats.length;
if (position == 0)
return new CheatItem(CheatItem.TYPE_HEADER, R.string.cheats_header_ar);
position -= 1;
Cheat[] arCheats = mViewModel.getARCheats(); Cheat[] arCheats = mViewModel.getARCheats();
if (position < arCheats.length) if (position < arCheats.length)
{ return new CheatItem(arCheats[position]);
return arCheats[position];
}
position -= arCheats.length; position -= arCheats.length;
if (position == 0)
return new CheatItem(CheatItem.TYPE_HEADER, R.string.cheats_header_gecko);
position -= 1;
Cheat[] geckoCheats = mViewModel.getGeckoCheats(); Cheat[] geckoCheats = mViewModel.getGeckoCheats();
if (position < geckoCheats.length) if (position < geckoCheats.length)
{ return new CheatItem(geckoCheats[position]);
return geckoCheats[position];
}
position -= geckoCheats.length; position -= geckoCheats.length;
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();

View File

@ -0,0 +1,28 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.cheats.ui;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel;
public class HeaderViewHolder extends CheatItemViewHolder
{
private TextView mHeaderName;
public HeaderViewHolder(@NonNull View itemView)
{
super(itemView);
mHeaderName = itemView.findViewById(R.id.text_header_name);
}
public void bind(CheatsViewModel viewModel, CheatItem item, int position)
{
mHeaderName.setText(item.getString());
}
}

View File

@ -18,9 +18,7 @@ import androidx.recyclerview.widget.RecyclerView;
import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.dialogs.MotionAlertDialog; import org.dolphinemu.dolphinemu.dialogs.MotionAlertDialog;
import org.dolphinemu.dolphinemu.features.settings.model.AdHocBooleanSetting;
import org.dolphinemu.dolphinemu.features.settings.model.Settings; import org.dolphinemu.dolphinemu.features.settings.model.Settings;
import org.dolphinemu.dolphinemu.features.settings.model.StringSetting;
import org.dolphinemu.dolphinemu.features.settings.model.view.CheckBoxSetting; import org.dolphinemu.dolphinemu.features.settings.model.view.CheckBoxSetting;
import org.dolphinemu.dolphinemu.features.settings.model.view.FilePicker; import org.dolphinemu.dolphinemu.features.settings.model.view.FilePicker;
import org.dolphinemu.dolphinemu.features.settings.model.view.FloatSliderSetting; import org.dolphinemu.dolphinemu.features.settings.model.view.FloatSliderSetting;
@ -43,7 +41,6 @@ import org.dolphinemu.dolphinemu.features.settings.ui.viewholder.SettingViewHold
import org.dolphinemu.dolphinemu.features.settings.ui.viewholder.SingleChoiceViewHolder; import org.dolphinemu.dolphinemu.features.settings.ui.viewholder.SingleChoiceViewHolder;
import org.dolphinemu.dolphinemu.features.settings.ui.viewholder.SliderViewHolder; import org.dolphinemu.dolphinemu.features.settings.ui.viewholder.SliderViewHolder;
import org.dolphinemu.dolphinemu.features.settings.ui.viewholder.SubmenuViewHolder; import org.dolphinemu.dolphinemu.features.settings.ui.viewholder.SubmenuViewHolder;
import org.dolphinemu.dolphinemu.ui.main.MainPresenter;
import org.dolphinemu.dolphinemu.utils.DirectoryInitialization; import org.dolphinemu.dolphinemu.utils.DirectoryInitialization;
import org.dolphinemu.dolphinemu.utils.FileBrowserHelper; import org.dolphinemu.dolphinemu.utils.FileBrowserHelper;
import org.dolphinemu.dolphinemu.utils.Log; import org.dolphinemu.dolphinemu.utils.Log;
@ -51,10 +48,7 @@ import org.dolphinemu.dolphinemu.utils.Log;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.security.InvalidParameterException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
public final class SettingsAdapter extends RecyclerView.Adapter<SettingViewHolder> public final class SettingsAdapter extends RecyclerView.Adapter<SettingViewHolder>
implements DialogInterface.OnClickListener, SeekBar.OnSeekBarChangeListener implements DialogInterface.OnClickListener, SeekBar.OnSeekBarChangeListener
@ -87,7 +81,7 @@ public final class SettingsAdapter extends RecyclerView.Adapter<SettingViewHolde
switch (viewType) switch (viewType)
{ {
case SettingsItem.TYPE_HEADER: case SettingsItem.TYPE_HEADER:
view = inflater.inflate(R.layout.list_item_settings_header, parent, false); view = inflater.inflate(R.layout.list_item_header, parent, false);
return new HeaderViewHolder(view, this); return new HeaderViewHolder(view, this);
case SettingsItem.TYPE_CHECKBOX: case SettingsItem.TYPE_CHECKBOX:

View File

@ -389,6 +389,9 @@
<!-- Cheats Screen --> <!-- Cheats Screen -->
<string name="cheats">Cheats</string> <string name="cheats">Cheats</string>
<string name="cheats_with_game_id">Cheats: %1$s</string> <string name="cheats_with_game_id">Cheats: %1$s</string>
<string name="cheats_header_ar">AR Codes</string>
<string name="cheats_header_gecko">Gecko Codes</string>
<string name="cheats_header_patch">Patches</string>
<string name="cheats_name">Name</string> <string name="cheats_name">Name</string>
<string name="cheats_creator">Creator</string> <string name="cheats_creator">Creator</string>
<string name="cheats_notes">Notes</string> <string name="cheats_notes">Notes</string>