From 1c47c510cdd97dafe1ec9099b1724e84549ce8b4 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sat, 2 Sep 2023 10:05:42 +0200 Subject: [PATCH] Android: Remove all uses of Any from SliderSetting This makes casting unnecessary, preventing the kind of type error we just had from occurring in the future. --- .../settings/model/view/FloatSliderSetting.kt | 18 ++++++++++--- .../settings/model/view/IntSliderSetting.kt | 11 ++++---- .../settings/model/view/SliderSetting.kt | 26 ++----------------- .../features/settings/ui/SettingsAdapter.kt | 12 ++++----- .../ui/viewholder/SliderViewHolder.kt | 8 +++--- 5 files changed, 32 insertions(+), 43 deletions(-) diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/FloatSliderSetting.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/FloatSliderSetting.kt index 7925903f5d..ee07c64056 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/FloatSliderSetting.kt +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/FloatSliderSetting.kt @@ -10,7 +10,11 @@ import java.math.BigDecimal import java.math.MathContext open class FloatSliderSetting : SliderSetting { - var floatSetting: AbstractFloatSetting + protected val floatSetting: AbstractFloatSetting + + val min: Float + val max: Float + val stepSize: Float override val setting: AbstractSetting get() = floatSetting @@ -25,8 +29,11 @@ open class FloatSliderSetting : SliderSetting { units: String?, stepSize: Float, showDecimal: Boolean - ) : super(context, titleId, descriptionId, min, max, units, stepSize, showDecimal) { + ) : super(context, titleId, descriptionId, units, showDecimal) { floatSetting = setting + this.min = min + this.max = max + this.stepSize = stepSize } constructor( @@ -38,11 +45,14 @@ open class FloatSliderSetting : SliderSetting { units: String?, stepSize: Float, showDecimal: Boolean - ) : super(name, description, min, max, units, stepSize, showDecimal) { + ) : super(name, description, units, showDecimal) { floatSetting = setting + this.min = min + this.max = max + this.stepSize = stepSize } - override val selectedValue: Float + open val selectedValue: Float get() = floatSetting.float open fun setSelectedValue(settings: Settings?, selection: Float) { diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/IntSliderSetting.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/IntSliderSetting.kt index 09a415fc97..45f2a71e69 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/IntSliderSetting.kt +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/IntSliderSetting.kt @@ -12,15 +12,16 @@ class IntSliderSetting( private val intSetting: AbstractIntSetting, titleId: Int, descriptionId: Int, - min: Int, - max: Int, + val min: Int, + val max: Int, units: String?, - stepSize: Int -) : SliderSetting(context, titleId, descriptionId, min, max, units, stepSize, false) { + val stepSize: Int +) : SliderSetting(context, titleId, descriptionId, units, false) { + override val setting: AbstractSetting get() = intSetting - override val selectedValue: Int + val selectedValue: Int get() = intSetting.int fun setSelectedValue(settings: Settings?, selection: Int) { diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/SliderSetting.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/SliderSetting.kt index 6e93d74239..83b047f7a5 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/SliderSetting.kt +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/SliderSetting.kt @@ -7,49 +7,27 @@ import android.content.Context sealed class SliderSetting : SettingsItem { override val type: Int = TYPE_SLIDER - var min: Any - private set - var max: Any - private set - var units: String? - private set - var stepSize: Any = 0 - private set - var showDecimal: Boolean = false - private set + val units: String? + val showDecimal: Boolean constructor( context: Context, nameId: Int, descriptionId: Int, - min: Any, - max: Any, units: String?, - stepSize: Any, showDecimal: Boolean ) : super(context, nameId, descriptionId) { - this.min = min - this.max = max this.units = units - this.stepSize = stepSize this.showDecimal = showDecimal } constructor( name: CharSequence, description: CharSequence?, - min: Any, - max: Any, units: String?, - stepSize: Any, showDecimal: Boolean ) : super(name, description) { - this.min = min - this.max = max this.units = units - this.stepSize = stepSize this.showDecimal = showDecimal } - - abstract val selectedValue: Any } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsAdapter.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsAdapter.kt index eff1b3ba13..b76d152d60 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsAdapter.kt +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsAdapter.kt @@ -249,14 +249,14 @@ class SettingsAdapter( val slider = binding.slider when (item) { is FloatSliderSetting -> { - slider.valueFrom = item.min as Float - slider.valueTo = item.max as Float - slider.stepSize = item.stepSize as Float + slider.valueFrom = item.min + slider.valueTo = item.max + slider.stepSize = item.stepSize } is IntSliderSetting -> { - slider.valueFrom = (item.min as Int).toFloat() - slider.valueTo = (item.max as Int).toFloat() - slider.stepSize = (item.stepSize as Int).toFloat() + slider.valueFrom = item.min.toFloat() + slider.valueTo = item.max.toFloat() + slider.stepSize = item.stepSize.toFloat() } } slider.value = seekbarProgress diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/viewholder/SliderViewHolder.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/viewholder/SliderViewHolder.kt index 6dde8749c2..0ec51430c7 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/viewholder/SliderViewHolder.kt +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/viewholder/SliderViewHolder.kt @@ -7,6 +7,7 @@ import android.text.TextUtils import android.view.View import org.dolphinemu.dolphinemu.R import org.dolphinemu.dolphinemu.databinding.ListItemSettingBinding +import org.dolphinemu.dolphinemu.features.settings.model.view.FloatSliderSetting import org.dolphinemu.dolphinemu.features.settings.model.view.IntSliderSetting import org.dolphinemu.dolphinemu.features.settings.model.view.SettingsItem import org.dolphinemu.dolphinemu.features.settings.model.view.SliderSetting @@ -29,10 +30,9 @@ class SliderViewHolder( if (!TextUtils.isEmpty(item.description)) { binding.textSettingDescription.text = item.description } else { - val selectedValue: Float = if (item is IntSliderSetting) { - (setting.selectedValue as Int).toFloat() - } else { - setting.selectedValue as Float + val selectedValue: Float = when (item) { + is FloatSliderSetting -> item.selectedValue + is IntSliderSetting -> item.selectedValue.toFloat() } if (setting.showDecimal) {