From eaaadf3b34e78539fbffb275a3b233c5fae7ce2e Mon Sep 17 00:00:00 2001 From: andrekir Date: Sun, 8 Jan 2023 10:38:04 -0300 Subject: [PATCH] feat: add EditIPv4Preference --- .../mesh/ui/DeviceSettingsItemList.kt | 9 +-- .../mesh/ui/components/EditTextPreference.kt | 67 +++++++++++++++++-- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/ui/DeviceSettingsItemList.kt b/app/src/main/java/com/geeksville/mesh/ui/DeviceSettingsItemList.kt index 02e7c6598..f8aba16d3 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/DeviceSettingsItemList.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/DeviceSettingsItemList.kt @@ -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() }), diff --git a/app/src/main/java/com/geeksville/mesh/ui/components/EditTextPreference.kt b/app/src/main/java/com/geeksville/mesh/ui/components/EditTextPreference.kt index 248bf679f..8c17bd2a9 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/components/EditTextPreference.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/components/EditTextPreference.kt @@ -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 = {} + ) + } }