android: Make SwitchSetting only allow Boolean setting

This commit is contained in:
Kleidis 2025-03-08 17:09:32 +01:00 committed by OpenSauce
parent 43dbe42b29
commit 0f1084be9c

View File

@ -9,53 +9,30 @@ import org.citra.citra_emu.features.settings.model.AbstractIntSetting
import org.citra.citra_emu.features.settings.model.AbstractSetting import org.citra.citra_emu.features.settings.model.AbstractSetting
class SwitchSetting( class SwitchSetting(
setting: AbstractSetting, setting: AbstractBooleanSetting,
titleId: Int, titleId: Int,
descriptionId: Int, descriptionId: Int,
val key: String? = null, val key: String? = null,
val defaultValue: Any? = null val defaultValue: Boolean = false
) : SettingsItem(setting, titleId, descriptionId) { ) : SettingsItem(setting, titleId, descriptionId) {
override val type = TYPE_SWITCH override val type = TYPE_SWITCH
val isChecked: Boolean val isChecked: Boolean
get() { get() {
if (setting == null) { if (setting == null) {
return defaultValue as Boolean return defaultValue
} }
// Try integer setting
try {
val setting = setting as AbstractIntSetting
return setting.int == 1
} catch (_: ClassCastException) {
}
// Try boolean setting
try {
val setting = setting as AbstractBooleanSetting val setting = setting as AbstractBooleanSetting
return setting.boolean return setting.boolean
} catch (_: ClassCastException) {
}
return defaultValue as Boolean
} }
/** /**
* Write a value to the backing boolean. If that boolean was previously null, * Write a value to the backing boolean.
* initializes a new one and returns it, so it can be added to the Hashmap.
* *
* @param checked Pretty self explanatory. * @param checked Pretty self explanatory.
* @return the existing setting with the new value applied. * @return the existing setting with the new value applied.
*/ */
fun setChecked(checked: Boolean): AbstractSetting { fun setChecked(checked: Boolean): AbstractBooleanSetting {
// Try integer setting
try {
val setting = setting as AbstractIntSetting
setting.int = if (checked) 1 else 0
return setting
} catch (_: ClassCastException) {
}
// Try boolean setting
val setting = setting as AbstractBooleanSetting val setting = setting as AbstractBooleanSetting
setting.boolean = checked setting.boolean = checked
return setting return setting