Android: Convert StringSingleChoiceSetting to Kotlin

This commit is contained in:
Charles Lombardo 2023-03-15 03:19:24 -04:00
parent 98ab893be7
commit 6dc6720250
3 changed files with 112 additions and 155 deletions

View File

@ -46,8 +46,8 @@ public class InputDeviceSetting extends StringSingleChoiceSetting
{
String[] devices = ControllerInterface.getAllDeviceStrings();
mChoices = devices;
mValues = devices;
setChoices(devices);
setValues(devices);
}
@Override

View File

@ -1,153 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.settings.model.view;
import android.content.Context;
import org.dolphinemu.dolphinemu.DolphinApplication;
import org.dolphinemu.dolphinemu.features.settings.model.AbstractSetting;
import org.dolphinemu.dolphinemu.features.settings.model.AbstractStringSetting;
import org.dolphinemu.dolphinemu.features.settings.model.Settings;
import org.dolphinemu.dolphinemu.features.settings.ui.MenuTag;
public class StringSingleChoiceSetting extends SettingsItem
{
private final AbstractStringSetting mSetting;
protected String[] mChoices;
protected String[] mValues;
private final MenuTag mMenuTag;
private int mNoChoicesAvailableString = 0;
public StringSingleChoiceSetting(Context context, AbstractStringSetting setting, int titleId,
int descriptionId, String[] choices, String[] values, MenuTag menuTag)
{
super(context, titleId, descriptionId);
mSetting = setting;
mChoices = choices;
mValues = values;
mMenuTag = menuTag;
}
public StringSingleChoiceSetting(Context context, AbstractStringSetting setting, int titleId,
int descriptionId, String[] choices, String[] values)
{
this(context, setting, titleId, descriptionId, choices, values, null);
}
public StringSingleChoiceSetting(Context context, AbstractStringSetting setting, int titleId,
int descriptionId, String[] choices, String[] values, int noChoicesAvailableString)
{
this(context, setting, titleId, descriptionId, choices, values, null);
mNoChoicesAvailableString = noChoicesAvailableString;
}
public StringSingleChoiceSetting(Context context, AbstractStringSetting setting, int titleId,
int descriptionId, int choicesId, int valuesId, MenuTag menuTag)
{
super(context, titleId, descriptionId);
mSetting = setting;
mChoices = DolphinApplication.getAppContext().getResources().getStringArray(choicesId);
mValues = DolphinApplication.getAppContext().getResources().getStringArray(valuesId);
mMenuTag = menuTag;
}
public StringSingleChoiceSetting(Context context, AbstractStringSetting setting, int titleId,
int descriptionId, int choicesId, int valuesId)
{
this(context, setting, titleId, descriptionId, choicesId, valuesId, null);
}
public String[] getChoices()
{
return mChoices;
}
public String[] getValues()
{
return mValues;
}
public String getChoiceAt(int index)
{
if (mChoices == null)
return null;
if (index >= 0 && index < mChoices.length)
{
return mChoices[index];
}
return "";
}
public String getValueAt(int index)
{
if (mValues == null)
return null;
if (index >= 0 && index < mValues.length)
{
return mValues[index];
}
return "";
}
public String getSelectedChoice()
{
return getChoiceAt(getSelectedValueIndex());
}
public String getSelectedValue()
{
return mSetting.getString();
}
public int getSelectedValueIndex()
{
String selectedValue = getSelectedValue();
for (int i = 0; i < mValues.length; i++)
{
if (mValues[i].equals(selectedValue))
{
return i;
}
}
return -1;
}
public MenuTag getMenuTag()
{
return mMenuTag;
}
public int getNoChoicesAvailableString()
{
return mNoChoicesAvailableString;
}
public void setSelectedValue(Settings settings, String selection)
{
mSetting.setString(settings, selection);
}
public void refreshChoicesAndValues()
{
}
@Override
public int getType()
{
return TYPE_STRING_SINGLE_CHOICE;
}
@Override
public AbstractSetting getSetting()
{
return mSetting;
}
}

View File

@ -0,0 +1,110 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.settings.model.view
import android.content.Context
import org.dolphinemu.dolphinemu.DolphinApplication
import org.dolphinemu.dolphinemu.features.settings.model.AbstractSetting
import org.dolphinemu.dolphinemu.features.settings.model.AbstractStringSetting
import org.dolphinemu.dolphinemu.features.settings.model.Settings
import org.dolphinemu.dolphinemu.features.settings.ui.MenuTag
open class StringSingleChoiceSetting : SettingsItem {
override val type: Int = TYPE_SINGLE_CHOICE
private val stringSetting: AbstractStringSetting?
override val setting: AbstractSetting?
get() = stringSetting
var choices: Array<String?>?
protected set
var values: Array<String?>?
protected set
val menuTag: MenuTag?
var noChoicesAvailableString = 0
private set
open val selectedChoice: String?
get() = getChoiceAt(selectedValueIndex)
open val selectedValue: String
get() = stringSetting!!.string
@JvmOverloads
constructor(
context: Context,
setting: AbstractStringSetting?,
titleId: Int,
descriptionId: Int,
choices: Array<String?>?,
values: Array<String?>?,
menuTag: MenuTag? = null
) : super(context, titleId, descriptionId) {
stringSetting = setting
this.choices = choices
this.values = values
this.menuTag = menuTag
}
constructor(
context: Context,
setting: AbstractStringSetting,
titleId: Int,
descriptionId: Int,
choices: Array<String?>,
values: Array<String?>,
noChoicesAvailableString: Int
) : this(context, setting, titleId, descriptionId, choices, values) {
this.noChoicesAvailableString = noChoicesAvailableString
}
@JvmOverloads
constructor(
context: Context,
setting: AbstractStringSetting,
titleId: Int,
descriptionId: Int,
choicesId: Int,
valuesId: Int,
menuTag: MenuTag? = null
) : super(context, titleId, descriptionId) {
stringSetting = setting
choices = DolphinApplication.getAppContext().resources.getStringArray(choicesId)
values = DolphinApplication.getAppContext().resources.getStringArray(valuesId)
this.menuTag = menuTag
}
fun getChoiceAt(index: Int): String? {
if (choices == null) return null
return if (index >= 0 && index < choices!!.size) {
choices!![index]
} else ""
}
fun getValueAt(index: Int): String? {
if (values == null) return null
return if (index >= 0 && index < values!!.size) {
values!![index]
} else ""
}
val selectedValueIndex: Int
get() {
val selectedValue = selectedValue
for (i in values!!.indices) {
if (values!![i] == selectedValue) {
return i
}
}
return -1
}
open fun setSelectedValue(settings: Settings?, selection: String?) {
stringSetting!!.setString(settings!!, selection!!)
}
open fun refreshChoicesAndValues() {}
}