Android: Convert SettingsItem to Kotlin

This commit is contained in:
Charles Lombardo 2023-03-15 03:22:05 -04:00
parent 6dc6720250
commit 43fa5cf5e4
4 changed files with 103 additions and 129 deletions

View File

@ -3,23 +3,16 @@
package org.dolphinemu.dolphinemu.features.settings.model.view
import android.content.Context
import org.dolphinemu.dolphinemu.features.settings.model.AbstractSetting
import org.dolphinemu.dolphinemu.features.settings.model.AbstractStringSetting
import org.dolphinemu.dolphinemu.features.settings.model.Settings
class DateTimeChoiceSetting(
context: Context,
private val setting: AbstractStringSetting,
override val setting: AbstractStringSetting,
nameId: Int,
descriptionId: Int
) : SettingsItem(context, nameId, descriptionId) {
override fun getType(): Int {
return TYPE_DATETIME_CHOICE
}
override fun getSetting(): AbstractSetting {
return setting
}
override val type: Int = TYPE_DATETIME_CHOICE
fun setSelectedValue(settings: Settings, selection: String) {
setting.setString(settings, selection)

View File

@ -1,107 +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.NativeLibrary;
import org.dolphinemu.dolphinemu.features.settings.model.AbstractSetting;
import org.dolphinemu.dolphinemu.features.settings.model.Settings;
import org.dolphinemu.dolphinemu.features.settings.ui.SettingsAdapter;
/**
* ViewModel abstraction for an Item in the RecyclerView powering SettingsFragments.
* Most of them correspond to a single line in an INI file, but there are a few with multiple
* analogues and a few with none (Headers, for example, do not correspond to anything on disk.)
*/
public abstract class SettingsItem
{
public static final int TYPE_HEADER = 0;
public static final int TYPE_SWITCH = 1;
public static final int TYPE_SINGLE_CHOICE = 2;
public static final int TYPE_SLIDER = 3;
public static final int TYPE_SUBMENU = 4;
public static final int TYPE_INPUT_MAPPING_CONTROL = 5;
public static final int TYPE_STRING_SINGLE_CHOICE = 6;
public static final int TYPE_SINGLE_CHOICE_DYNAMIC_DESCRIPTIONS = 8;
public static final int TYPE_FILE_PICKER = 9;
public static final int TYPE_RUN_RUNNABLE = 10;
public static final int TYPE_STRING = 11;
public static final int TYPE_HYPERLINK_HEADER = 12;
public static final int TYPE_DATETIME_CHOICE = 13;
private final CharSequence mName;
private final CharSequence mDescription;
/**
* Base constructor.
*
* @param name A text string to be displayed as this setting's name.
* @param description A text string to be displayed as this setting's description.
*/
public SettingsItem(CharSequence name, CharSequence description)
{
mName = name;
mDescription = description;
}
/**
* @param nameId Resource ID for a text string to be displayed as this setting's name.
* @param descriptionId Resource ID for a text string to be displayed as this setting's description.
*/
public SettingsItem(Context context, int nameId, int descriptionId)
{
mName = nameId == 0 ? "" : context.getText(nameId);
mDescription = descriptionId == 0 ? "" : context.getText(descriptionId);
}
public CharSequence getName()
{
return mName;
}
public CharSequence getDescription()
{
return mDescription;
}
/**
* Used by {@link SettingsAdapter}'s onCreateViewHolder()
* method to determine which type of ViewHolder should be created.
*
* @return An integer (ideally, one of the constants defined in this file)
*/
public abstract int getType();
protected abstract AbstractSetting getSetting();
public boolean isOverridden()
{
AbstractSetting setting = getSetting();
return setting != null && setting.isOverridden();
}
public boolean isEditable()
{
if (!NativeLibrary.IsRunning())
return true;
AbstractSetting setting = getSetting();
return setting != null && setting.isRuntimeEditable();
}
public boolean hasSetting()
{
return getSetting() != null;
}
public boolean canClear()
{
return hasSetting();
}
public void clear(Settings settings)
{
getSetting().delete(settings);
}
}

View File

@ -0,0 +1,89 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.features.settings.model.view
import android.content.Context
import org.dolphinemu.dolphinemu.NativeLibrary
import org.dolphinemu.dolphinemu.features.settings.model.AbstractSetting
import org.dolphinemu.dolphinemu.features.settings.model.Settings
/**
* ViewModel abstraction for an Item in the RecyclerView powering SettingsFragments.
* Most of them correspond to a single line in an INI file, but there are a few with multiple
* analogues and a few with none (Headers, for example, do not correspond to anything on disk.)
*/
abstract class SettingsItem {
val name: CharSequence
val description: CharSequence?
/**
* Base constructor.
*
* @param name A text string to be displayed as this Setting's name.
* @param description A text string to be displayed as this Setting's description.
*/
constructor(name: CharSequence, description: CharSequence?) {
this.name = name
this.description = description
}
/**
* @param nameId Resource ID for a text string to be displayed as this Setting's name.
* @param descriptionId Resource ID for a text string to be displayed as this Setting's description.
*/
constructor(context: Context, nameId: Int, descriptionId: Int) {
name = if (nameId == 0) "" else context.getText(nameId)
description = if (descriptionId == 0) "" else context.getText(descriptionId)
}
/**
* Used by [SettingsAdapter]'s onCreateViewHolder()
* method to determine which type of ViewHolder should be created.
*
* @return An integer (ideally, one of the constants defined in this file)
*/
abstract val type: Int
abstract val setting: AbstractSetting?
val isOverridden: Boolean
get() {
val setting = setting
return setting != null && setting.isOverridden
}
open val isEditable: Boolean
get() {
if (!NativeLibrary.IsRunning()) return true
val setting = setting
return setting != null && setting.isRuntimeEditable
}
private fun hasSetting(): Boolean {
return setting != null
}
open fun canClear(): Boolean {
return hasSetting()
}
open fun clear(settings: Settings) {
setting!!.delete(settings)
}
companion object {
const val TYPE_HEADER = 0
const val TYPE_SWITCH = 1
const val TYPE_SINGLE_CHOICE = 2
const val TYPE_SLIDER = 3
const val TYPE_SUBMENU = 4
const val TYPE_INPUT_MAPPING_CONTROL = 5
const val TYPE_STRING_SINGLE_CHOICE = 6
const val TYPE_SINGLE_CHOICE_DYNAMIC_DESCRIPTIONS = 8
const val TYPE_FILE_PICKER = 9
const val TYPE_RUN_RUNNABLE = 10
const val TYPE_STRING = 11
const val TYPE_HYPERLINK_HEADER = 12
const val TYPE_DATETIME_CHOICE = 13
}
}

View File

@ -18,12 +18,15 @@ class DateTimeSettingViewHolder(
private val binding: ListItemSettingBinding,
adapter: SettingsAdapter
) : SettingViewHolder(binding.root, adapter) {
private var mItem: DateTimeChoiceSetting? = null
lateinit var setting: DateTimeChoiceSetting
override val item: SettingsItem
get() = setting
override fun bind(item: SettingsItem) {
mItem = item as DateTimeChoiceSetting
val inputTime = mItem!!.getSelectedValue()
binding.textSettingName.text = item.getName()
setting = item as DateTimeChoiceSetting
val inputTime = setting.getSelectedValue()
binding.textSettingName.text = item.name
if (!TextUtils.isEmpty(inputTime)) {
val epochTime = inputTime.substring(2).toLong(16)
@ -32,21 +35,17 @@ class DateTimeSettingViewHolder(
val dateFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
binding.textSettingDescription.text = dateFormatter.format(zonedTime)
} else {
binding.textSettingDescription.text = item.getDescription()
binding.textSettingDescription.text = item.description
}
setStyle(binding.textSettingName, mItem)
setStyle(binding.textSettingName, setting)
}
override fun onClick(clicked: View) {
if (!mItem!!.isEditable) {
if (!setting.isEditable) {
showNotRuntimeEditableError()
return
}
adapter.onDateTimeClick(mItem, bindingAdapterPosition)
setStyle(binding.textSettingName, mItem)
}
override fun getItem(): SettingsItem? {
return mItem
adapter.onDateTimeClick(setting, bindingAdapterPosition)
setStyle(binding.textSettingName, setting)
}
}