pull/2925/head
Dane Evans 2025-08-30 10:09:36 +10:00
rodzic 39575c2257
commit d54db0ae4e
1 zmienionych plików z 59 dodań i 54 usunięć

Wyświetl plik

@ -61,55 +61,56 @@ inline fun <reified T> EditListPreference(
val listState = remember(list) { mutableStateListOf<T>().apply { addAll(list) } } val listState = remember(list) { mutableStateListOf<T>().apply { addAll(list) } }
Column(modifier = modifier) { Column(modifier = modifier) {
Text( Text(modifier = modifier.padding(16.dp), text = title, style = MaterialTheme.typography.bodyMedium)
modifier = modifier.padding(16.dp),
text = title,
style = MaterialTheme.typography.bodyMedium,
)
listState.forEachIndexed { index, value -> listState.forEachIndexed { index, value ->
val trailingIcon = @Composable { val trailingIcon =
IconButton( @Composable {
onClick = { IconButton(
focusManager.clearFocus() onClick = {
listState.removeAt(index) focusManager.clearFocus()
onValuesChanged(listState) listState.removeAt(index)
onValuesChanged(listState)
},
) {
Icon(
imageVector = Icons.TwoTone.Close,
contentDescription = stringResource(R.string.delete),
modifier = Modifier.wrapContentSize(),
)
} }
) {
Icon(
imageVector = Icons.TwoTone.Close,
contentDescription = stringResource(R.string.delete),
modifier = Modifier.wrapContentSize(),
)
} }
}
// handle lora.ignoreIncoming: List<Int> // handle lora.ignoreIncoming: List<Int>
if (value is Int) EditTextPreference( if (value is Int) {
title = "${index + 1}/$maxCount", EditTextPreference(
value = value, title = "${index + 1}/$maxCount",
enabled = enabled, value = value,
keyboardActions = keyboardActions, enabled = enabled,
onValueChanged = { keyboardActions = keyboardActions,
listState[index] = it as T onValueChanged = {
onValuesChanged(listState) listState[index] = it as T
}, onValuesChanged(listState)
modifier = modifier.fillMaxWidth(), },
trailingIcon = trailingIcon, modifier = modifier.fillMaxWidth(),
) trailingIcon = trailingIcon,
)
}
// handle security.adminKey: List<ByteString> // handle security.adminKey: List<ByteString>
if (value is ByteString) EditBase64Preference( if (value is ByteString) {
title = "${index + 1}/$maxCount", EditBase64Preference(
value = value, title = "${index + 1}/$maxCount",
enabled = enabled, value = value,
keyboardActions = keyboardActions, enabled = enabled,
onValueChange = { keyboardActions = keyboardActions,
listState[index] = it as T onValueChange = {
onValuesChanged(listState) listState[index] = it as T
}, onValuesChanged(listState)
modifier = modifier.fillMaxWidth(), },
trailingIcon = trailingIcon, modifier = modifier.fillMaxWidth(),
) trailingIcon = trailingIcon,
)
}
// handle remoteHardware.availablePins: List<RemoteHardwarePin> // handle remoteHardware.availablePins: List<RemoteHardwarePin>
if (value is RemoteHardwarePin) { if (value is RemoteHardwarePin) {
@ -131,9 +132,8 @@ inline fun <reified T> EditListPreference(
maxSize = 14, // name max_size:15 maxSize = 14, // name max_size:15
enabled = enabled, enabled = enabled,
isError = false, isError = false,
keyboardOptions = KeyboardOptions.Default.copy( keyboardOptions =
keyboardType = KeyboardType.Text, imeAction = ImeAction.Done KeyboardOptions.Default.copy(keyboardType = KeyboardType.Text, imeAction = ImeAction.Done),
),
keyboardActions = keyboardActions, keyboardActions = keyboardActions,
onValueChanged = { onValueChanged = {
listState[index] = value.copy { name = it } as T listState[index] = value.copy { name = it } as T
@ -144,7 +144,8 @@ inline fun <reified T> EditListPreference(
DropDownPreference( DropDownPreference(
title = stringResource(R.string.type), title = stringResource(R.string.type),
enabled = enabled, enabled = enabled,
items = RemoteHardwarePinType.entries items =
RemoteHardwarePinType.entries
.filter { it != RemoteHardwarePinType.UNRECOGNIZED } .filter { it != RemoteHardwarePinType.UNRECOGNIZED }
.map { it to it.name }, .map { it to it.name },
selectedItem = value.type, selectedItem = value.type,
@ -159,16 +160,19 @@ inline fun <reified T> EditListPreference(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
onClick = { onClick = {
// Add element based on the type T // Add element based on the type T
val newElement = when (T::class) { val newElement =
Int::class -> 0 as T when (T::class) {
ByteString::class -> ByteString.EMPTY as T Int::class -> 0 as T
RemoteHardwarePin::class -> remoteHardwarePin {} as T ByteString::class -> ByteString.EMPTY as T
else -> throw IllegalArgumentException("Unsupported type: ${T::class}") RemoteHardwarePin::class -> remoteHardwarePin {} as T
} else -> throw IllegalArgumentException("Unsupported type: ${T::class}")
}
listState.add(listState.size, newElement) listState.add(listState.size, newElement)
}, },
enabled = maxCount > listState.size, enabled = maxCount > listState.size,
) { Text(text = stringResource(R.string.add)) } ) {
Text(text = stringResource(R.string.add))
}
} }
} }
@ -186,7 +190,8 @@ private fun EditListPreferencePreview() {
) )
EditListPreference( EditListPreference(
title = "Available pins", title = "Available pins",
list = listOf( list =
listOf(
remoteHardwarePin { remoteHardwarePin {
gpioPin = 12 gpioPin = 12
name = "Front door" name = "Front door"