config: unique selection value

This commit is contained in:
rhunk
2023-05-26 20:46:04 +02:00
parent c12ba73f28
commit de1a676084
3 changed files with 57 additions and 0 deletions

View File

@@ -44,6 +44,10 @@ open class ConfigAccessor(
return get(key).value() as Map<String, Boolean>
}
fun state(key: ConfigProperty): String {
return get(key).value() as String
}
fun get(key: ConfigProperty): ConfigValue<*> {
return configMap[key]!!
}

View File

@@ -0,0 +1,28 @@
package me.rhunk.snapenhance.config.impl
import me.rhunk.snapenhance.config.ConfigValue
class ConfigStateSelection(
private val keys: List<String>,
var state: String = ""
) : ConfigValue<String>() {
fun keys(): List<String> {
return keys
}
override fun value(): String {
return state
}
fun value(key: String) {
state = key
}
override fun write(): String {
return state
}
override fun read(value: String) {
state = value
}
}

View File

@@ -13,6 +13,7 @@ import me.rhunk.snapenhance.Constants
import me.rhunk.snapenhance.config.ConfigProperty
import me.rhunk.snapenhance.config.impl.ConfigIntegerValue
import me.rhunk.snapenhance.config.impl.ConfigStateListValue
import me.rhunk.snapenhance.config.impl.ConfigStateSelection
import me.rhunk.snapenhance.config.impl.ConfigStateValue
import me.rhunk.snapenhance.config.impl.ConfigStringValue
import me.rhunk.snapenhance.features.impl.ui.menus.AbstractMenu
@@ -89,6 +90,30 @@ class SettingsMenu : AbstractMenu() {
ViewAppearanceHelper.applyTheme(viewModel, switch)
switch
}
is ConfigStateSelection -> {
val button = Button(viewModel.context)
updateButtonText(button, property.valueContainer.value())
button.setOnClickListener {_ ->
val builder = AlertDialog.Builder(viewModel.context)
builder.setTitle(context.translation.get(property.nameKey))
builder.setSingleChoiceItems(
property.valueContainer.keys().toTypedArray(),
property.valueContainer.keys().indexOf(property.valueContainer.value())
) { _, which ->
property.valueContainer.value(property.valueContainer.keys()[which])
}
builder.setPositiveButton("OK") { _, _ ->
updateButtonText(button, property.valueContainer.value())
}
builder.show()
}
ViewAppearanceHelper.applyTheme(viewModel, button)
button
}
is ConfigStateListValue -> {
val button = Button(viewModel.context)
updateButtonText(button, property.valueContainer.toString())