Android: Convert SingleChoiceViewHolder to Kotlin

This commit is contained in:
Charles Lombardo 2023-03-15 03:32:32 -04:00
parent 917a0c2287
commit 9b79f5fa5d
2 changed files with 98 additions and 148 deletions

View File

@ -1,148 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.settings.ui.viewholder;
import android.content.res.Resources;
import android.text.TextUtils;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.dolphinemu.dolphinemu.databinding.ListItemSettingBinding;
import org.dolphinemu.dolphinemu.features.settings.model.Settings;
import org.dolphinemu.dolphinemu.features.settings.model.view.SettingsItem;
import org.dolphinemu.dolphinemu.features.settings.model.view.SingleChoiceSetting;
import org.dolphinemu.dolphinemu.features.settings.model.view.SingleChoiceSettingDynamicDescriptions;
import org.dolphinemu.dolphinemu.features.settings.model.view.StringSingleChoiceSetting;
import org.dolphinemu.dolphinemu.features.settings.ui.MenuTag;
import org.dolphinemu.dolphinemu.features.settings.ui.SettingsAdapter;
import java.util.function.Supplier;
public final class SingleChoiceViewHolder extends SettingViewHolder
{
private SettingsItem mItem;
private final ListItemSettingBinding mBinding;
public SingleChoiceViewHolder(@NonNull ListItemSettingBinding binding, SettingsAdapter adapter)
{
super(binding.getRoot(), adapter);
mBinding = binding;
}
@Override
public void bind(SettingsItem item)
{
mItem = item;
mBinding.textSettingName.setText(item.getName());
if (!TextUtils.isEmpty(item.getDescription()))
{
mBinding.textSettingDescription.setText(item.getDescription());
}
else if (item instanceof SingleChoiceSetting)
{
SingleChoiceSetting setting = (SingleChoiceSetting) item;
int selected = setting.getSelectedValue();
Resources resMgr = mBinding.textSettingDescription.getContext().getResources();
String[] choices = resMgr.getStringArray(setting.getChoicesId());
int[] values = resMgr.getIntArray(setting.getValuesId());
for (int i = 0; i < values.length; ++i)
{
if (values[i] == selected)
{
mBinding.textSettingDescription.setText(choices[i]);
}
}
}
else if (item instanceof StringSingleChoiceSetting)
{
StringSingleChoiceSetting setting = (StringSingleChoiceSetting) item;
String choice = setting.getSelectedChoice();
mBinding.textSettingDescription.setText(choice);
}
else if (item instanceof SingleChoiceSettingDynamicDescriptions)
{
SingleChoiceSettingDynamicDescriptions setting =
(SingleChoiceSettingDynamicDescriptions) item;
int selected = setting.getSelectedValue();
Resources resMgr = mBinding.textSettingDescription.getContext().getResources();
String[] choices = resMgr.getStringArray(setting.getDescriptionChoicesId());
int[] values = resMgr.getIntArray(setting.getDescriptionValuesId());
for (int i = 0; i < values.length; ++i)
{
if (values[i] == selected)
{
mBinding.textSettingDescription.setText(choices[i]);
}
}
}
MenuTag menuTag = null;
Supplier<Integer> getSelectedValue = null;
if (item instanceof SingleChoiceSetting)
{
SingleChoiceSetting setting = (SingleChoiceSetting) item;
menuTag = setting.getMenuTag();
getSelectedValue = setting::getSelectedValue;
}
else if (item instanceof StringSingleChoiceSetting)
{
StringSingleChoiceSetting setting = (StringSingleChoiceSetting) item;
menuTag = setting.getMenuTag();
getSelectedValue = setting::getSelectedValueIndex;
}
if (menuTag != null && getAdapter().hasMenuTagActionForValue(menuTag, getSelectedValue.get()))
{
mBinding.buttonMoreSettings.setVisibility(View.VISIBLE);
final MenuTag finalMenuTag = menuTag;
final Supplier<Integer> finalGetSelectedValue = getSelectedValue;
mBinding.buttonMoreSettings.setOnClickListener((view) ->
getAdapter().onMenuTagAction(finalMenuTag, finalGetSelectedValue.get()));
}
else
{
mBinding.buttonMoreSettings.setVisibility(View.GONE);
}
setStyle(mBinding.textSettingName, mItem);
}
@Override
public void onClick(View clicked)
{
if (!mItem.isEditable())
{
showNotRuntimeEditableError();
return;
}
int position = getBindingAdapterPosition();
if (mItem instanceof SingleChoiceSetting)
{
getAdapter().onSingleChoiceClick((SingleChoiceSetting) mItem, position);
}
else if (mItem instanceof StringSingleChoiceSetting)
{
getAdapter().onStringSingleChoiceClick((StringSingleChoiceSetting) mItem, position);
}
else if (mItem instanceof SingleChoiceSettingDynamicDescriptions)
{
getAdapter().onSingleChoiceDynamicDescriptionsClick(
(SingleChoiceSettingDynamicDescriptions) mItem, position);
}
setStyle(mBinding.textSettingName, mItem);
}
@Nullable @Override
protected SettingsItem getItem()
{
return mItem;
}
}

View File

@ -0,0 +1,98 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.settings.ui.viewholder
import android.text.TextUtils
import android.view.View
import org.dolphinemu.dolphinemu.databinding.ListItemSettingBinding
import org.dolphinemu.dolphinemu.features.settings.model.view.SettingsItem
import org.dolphinemu.dolphinemu.features.settings.model.view.SingleChoiceSetting
import org.dolphinemu.dolphinemu.features.settings.model.view.SingleChoiceSettingDynamicDescriptions
import org.dolphinemu.dolphinemu.features.settings.model.view.StringSingleChoiceSetting
import org.dolphinemu.dolphinemu.features.settings.ui.MenuTag
import org.dolphinemu.dolphinemu.features.settings.ui.SettingsAdapter
class SingleChoiceViewHolder(
private val binding: ListItemSettingBinding,
adapter: SettingsAdapter
) : SettingViewHolder(binding.getRoot(), adapter) {
override var item: SettingsItem? = null
override fun bind(item: SettingsItem) {
this.item = item
binding.textSettingName.text = item.name
if (!TextUtils.isEmpty(item.description)) {
binding.textSettingDescription.text = item.description
} else if (item is SingleChoiceSetting) {
val selected = item.selectedValue
val resources = binding.textSettingDescription.context.resources
val choices = resources.getStringArray(item.choicesId)
val values = resources.getIntArray(item.valuesId)
for (i in values.indices) {
if (values[i] == selected) {
binding.textSettingDescription.text = choices[i]
}
}
} else if (item is StringSingleChoiceSetting) {
val choice = item.selectedChoice
binding.textSettingDescription.text = choice
} else if (item is SingleChoiceSettingDynamicDescriptions) {
val selected = item.selectedValue
val resMgr = binding.textSettingDescription.context.resources
val choices = resMgr.getStringArray(item.descriptionChoicesId)
val values = resMgr.getIntArray(item.descriptionValuesId)
for (i in values.indices) {
if (values[i] == selected) {
binding.textSettingDescription.text = choices[i]
}
}
}
var menuTag: MenuTag? = null
var selectedValue = 0
if (item is SingleChoiceSetting) {
menuTag = item.menuTag
selectedValue = item.selectedValue
} else if (item is StringSingleChoiceSetting) {
menuTag = item.menuTag
selectedValue = item.selectedValueIndex
}
if (menuTag != null && adapter.hasMenuTagActionForValue(menuTag, selectedValue)) {
binding.buttonMoreSettings.visibility = View.VISIBLE
binding.buttonMoreSettings.setOnClickListener {
adapter.onMenuTagAction(menuTag, selectedValue)
}
} else {
binding.buttonMoreSettings.visibility = View.GONE
}
setStyle(binding.textSettingName, item)
}
override fun onClick(clicked: View) {
if (!item?.isEditable!!) {
showNotRuntimeEditableError()
return
}
val position = bindingAdapterPosition
when (item) {
is SingleChoiceSetting -> {
adapter.onSingleChoiceClick(item as SingleChoiceSetting, position)
}
is StringSingleChoiceSetting -> {
adapter.onStringSingleChoiceClick(item as StringSingleChoiceSetting, position)
}
is SingleChoiceSettingDynamicDescriptions -> {
adapter.onSingleChoiceDynamicDescriptionsClick(
item as SingleChoiceSettingDynamicDescriptions,
position
)
}
}
setStyle(binding.textSettingName, item!!)
}
}