more dynamic select options

This commit is contained in:
Chloe committed 2025-01-17 14:54:42 -07:00
1 parent c090754129
commit 1583f259b9
2 files changed
+51 -31

No files matched your search

@@ -135,13 +135,15 @@ fun SelectLogLevelDialog(
onDismissRequest = onDismissRequest, onDismissRequest = onDismissRequest,
onSelect = { onSelect(it) }, onSelect = { onSelect(it) },
initialValue = initialValue, initialValue = initialValue,
toLabel = { ) {
// i'm lazy (LogPriority.VERBOSE..LogPriority.FATAL).reversed().forEach {
it.name.lowercase().replaceFirstChar { c -> c.uppercaseChar() } SelectOption(
}, name = it.name.lowercase().replaceFirstChar { c -> c.uppercaseChar() },
optionsCount = LogPriority.FATAL downTo LogPriority.VERBOSE value = it
) )
} }
}
}
@Composable @Composable
fun LogCard(logLine: LogLine, modifier: Modifier = Modifier) { fun LogCard(logLine: LogLine, modifier: Modifier = Modifier) {
@@ -24,10 +24,13 @@ import androidx.compose.material3.Switch
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TextButton import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
@@ -109,9 +112,11 @@ fun SettingsSelectCard(
extraSelectBehavior?.invoke(selected) extraSelectBehavior?.invoke(selected)
}, },
initialValue = preferenceValue, initialValue = preferenceValue,
toLabel = toLabel, ) {
optionsCount = 0..maxVal (0..maxVal).forEach {
) SelectOption(name = toLabel(it), value = it)
}
}
} }
} }
@@ -204,16 +209,42 @@ fun StringDialog(
) )
} }
internal val LocalSelectValue = compositionLocalOf<Any> { 0 }
internal val LocalSelectSetValue = staticCompositionLocalOf<(Any) -> Unit> { {} }
@Composable
fun <T> SelectOption(name: String, value: T) {
val currentValue = LocalSelectValue.current
val setValue = LocalSelectSetValue.current
// do not give the row or column padding!! it messes up the selection effect
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.fillMaxWidth()
.clickable(
onClick = { setValue(value as Any) },
role = Role.RadioButton
)
.padding(horizontal = 12.dp)
) {
RadioButton(
selected = currentValue.equals(value),
onClick = { setValue(value as Any) }
)
Text(name, style = Typography.bodyMedium)
}
}
@Composable @Composable
fun <T> SelectDialog( fun <T> SelectDialog(
title: String, title: String,
onDismissRequest: () -> Unit, onDismissRequest: () -> Unit,
onSelect: (T) -> Unit, onSelect: (T) -> Unit,
initialValue: T, initialValue: T,
toLabel: @Composable (T) -> String, options: @Composable () -> Unit,
optionsCount: Iterable<T>,
) { ) {
var selectedValue by remember { mutableStateOf(initialValue) } val (selectedValue, setSelectedValue) = remember { mutableStateOf(initialValue) }
Dialog(onDismissRequest = onDismissRequest) { Dialog(onDismissRequest = onDismissRequest) {
Card( Card(
@@ -232,27 +263,14 @@ fun <T> SelectDialog(
) )
) )
// do not give the row or column padding!! it messes up the selection effect CompositionLocalProvider(
optionsCount.forEach { id -> LocalSelectValue provides (selectedValue as Any),
Row( LocalSelectSetValue provides ({
verticalAlignment = Alignment.CenterVertically, @Suppress("UNCHECKED_CAST")
modifier = Modifier setSelectedValue(it as T)
.fillMaxWidth() })
.clickable(
onClick = { selectedValue = id },
role = Role.RadioButton
)
.padding(horizontal = 12.dp)
) { ) {
RadioButton( options()
selected = selectedValue == id,
onClick = { selectedValue = id }
)
Text(
toLabel(id),
style = Typography.bodyMedium
)
}
} }
Row( Row(