kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
feat: add EditIPv4Preference
rodzic
4ab9e18a82
commit
eaaadf3b34
|
@ -26,6 +26,7 @@ import com.geeksville.mesh.model.getInitials
|
|||
import com.geeksville.mesh.service.MeshService
|
||||
import com.geeksville.mesh.ui.components.BitwisePreference
|
||||
import com.geeksville.mesh.ui.components.DropDownPreference
|
||||
import com.geeksville.mesh.ui.components.EditIPv4Preference
|
||||
import com.geeksville.mesh.ui.components.EditTextPreference
|
||||
import com.geeksville.mesh.ui.components.PreferenceCategory
|
||||
import com.geeksville.mesh.ui.components.PreferenceFooter
|
||||
|
@ -478,7 +479,7 @@ fun DeviceSettingsItemList(viewModel: UIViewModel) {
|
|||
item { PreferenceCategory(text = "IPv4 Config") }
|
||||
|
||||
item {
|
||||
EditTextPreference(title = "IP",
|
||||
EditIPv4Preference(title = "IP",
|
||||
value = networkInput.ipv4Config.ip,
|
||||
enabled = connected && networkInput.addressMode == NetworkConfig.AddressMode.STATIC,
|
||||
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
|
||||
|
@ -489,7 +490,7 @@ fun DeviceSettingsItemList(viewModel: UIViewModel) {
|
|||
}
|
||||
|
||||
item {
|
||||
EditTextPreference(title = "Gateway",
|
||||
EditIPv4Preference(title = "Gateway",
|
||||
value = networkInput.ipv4Config.gateway,
|
||||
enabled = connected && networkInput.addressMode == NetworkConfig.AddressMode.STATIC,
|
||||
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
|
||||
|
@ -500,7 +501,7 @@ fun DeviceSettingsItemList(viewModel: UIViewModel) {
|
|||
}
|
||||
|
||||
item {
|
||||
EditTextPreference(title = "Subnet",
|
||||
EditIPv4Preference(title = "Subnet",
|
||||
value = networkInput.ipv4Config.subnet,
|
||||
enabled = connected && networkInput.addressMode == NetworkConfig.AddressMode.STATIC,
|
||||
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
|
||||
|
@ -511,7 +512,7 @@ fun DeviceSettingsItemList(viewModel: UIViewModel) {
|
|||
}
|
||||
|
||||
item {
|
||||
EditTextPreference(title = "DNS",
|
||||
EditIPv4Preference(title = "DNS",
|
||||
value = networkInput.ipv4Config.dns,
|
||||
enabled = connected && networkInput.addressMode == NetworkConfig.AddressMode.STATIC,
|
||||
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.geeksville.mesh.ui.components
|
||||
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
|
@ -112,6 +113,49 @@ fun EditTextPreference(
|
|||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun EditIPv4Preference(
|
||||
title: String,
|
||||
value: Int,
|
||||
enabled: Boolean,
|
||||
keyboardActions: KeyboardActions,
|
||||
onValueChanged: (Int) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
fun convertIntToIpAddress(int: Int): String {
|
||||
return "${int shr 24 and 0xff}.${int shr 16 and 0xff}.${int shr 8 and 0xff}.${int and 0xff}"
|
||||
}
|
||||
fun convertIpAddressToInt(ipAddress: String): Int? {
|
||||
return ipAddress.split(".")
|
||||
.map { it.toIntOrNull() }
|
||||
.fold(0) { total, next ->
|
||||
if (next == null) return null else total shl 8 or next
|
||||
}
|
||||
}
|
||||
var valueState by remember(value) { mutableStateOf(convertIntToIpAddress(value)) }
|
||||
|
||||
EditTextPreference(
|
||||
title = title,
|
||||
value = valueState,
|
||||
enabled = enabled,
|
||||
isError = convertIntToIpAddress(value) != valueState,
|
||||
keyboardOptions = KeyboardOptions.Default.copy(
|
||||
keyboardType = KeyboardType.Number, imeAction = ImeAction.Done
|
||||
),
|
||||
keyboardActions = keyboardActions,
|
||||
onValueChanged = {
|
||||
val pattern = """\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b""".toRegex()
|
||||
val isValid = pattern.matches(it)
|
||||
if (it.isEmpty() || !isValid) valueState = it
|
||||
else convertIpAddressToInt(it)?.let { int ->
|
||||
valueState = it
|
||||
onValueChanged(int)
|
||||
}
|
||||
},
|
||||
modifier = modifier
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun EditTextPreference(
|
||||
title: String,
|
||||
|
@ -142,11 +186,20 @@ fun EditTextPreference(
|
|||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
private fun EditTextPreferencePreview() {
|
||||
EditTextPreference(
|
||||
title = "Advanced Settings",
|
||||
value = UInt.MAX_VALUE.toInt(),
|
||||
enabled = true,
|
||||
keyboardActions = KeyboardActions {},
|
||||
onValueChanged = {}
|
||||
)
|
||||
Column {
|
||||
EditTextPreference(
|
||||
title = "Advanced Settings",
|
||||
value = UInt.MAX_VALUE.toInt(),
|
||||
enabled = true,
|
||||
keyboardActions = KeyboardActions {},
|
||||
onValueChanged = {}
|
||||
)
|
||||
EditIPv4Preference(
|
||||
title = "IP Address",
|
||||
value = 3232235521.toInt(),
|
||||
enabled = true,
|
||||
keyboardActions = KeyboardActions {},
|
||||
onValueChanged = {}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue