From 6dc6720250483c04b78761773e933ab618c712c9 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Wed, 15 Mar 2023 03:19:24 -0400 Subject: [PATCH] Android: Convert StringSingleChoiceSetting to Kotlin --- .../input/model/view/InputDeviceSetting.java | 4 +- .../model/view/StringSingleChoiceSetting.java | 153 ------------------ .../model/view/StringSingleChoiceSetting.kt | 110 +++++++++++++ 3 files changed, 112 insertions(+), 155 deletions(-) delete mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/StringSingleChoiceSetting.java create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/StringSingleChoiceSetting.kt diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/input/model/view/InputDeviceSetting.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/input/model/view/InputDeviceSetting.java index 3ee7112007..f02de2e3f3 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/input/model/view/InputDeviceSetting.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/input/model/view/InputDeviceSetting.java @@ -46,8 +46,8 @@ public class InputDeviceSetting extends StringSingleChoiceSetting { String[] devices = ControllerInterface.getAllDeviceStrings(); - mChoices = devices; - mValues = devices; + setChoices(devices); + setValues(devices); } @Override diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/StringSingleChoiceSetting.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/StringSingleChoiceSetting.java deleted file mode 100644 index e83fe913cd..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/StringSingleChoiceSetting.java +++ /dev/null @@ -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; - } -} - - diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/StringSingleChoiceSetting.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/StringSingleChoiceSetting.kt new file mode 100644 index 0000000000..62301e23ef --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/StringSingleChoiceSetting.kt @@ -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? + protected set + var values: Array? + 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?, + values: Array?, + 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, + values: Array, + 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() {} +}