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.service.MeshService
|
||||||
import com.geeksville.mesh.ui.components.BitwisePreference
|
import com.geeksville.mesh.ui.components.BitwisePreference
|
||||||
import com.geeksville.mesh.ui.components.DropDownPreference
|
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.EditTextPreference
|
||||||
import com.geeksville.mesh.ui.components.PreferenceCategory
|
import com.geeksville.mesh.ui.components.PreferenceCategory
|
||||||
import com.geeksville.mesh.ui.components.PreferenceFooter
|
import com.geeksville.mesh.ui.components.PreferenceFooter
|
||||||
|
@ -478,7 +479,7 @@ fun DeviceSettingsItemList(viewModel: UIViewModel) {
|
||||||
item { PreferenceCategory(text = "IPv4 Config") }
|
item { PreferenceCategory(text = "IPv4 Config") }
|
||||||
|
|
||||||
item {
|
item {
|
||||||
EditTextPreference(title = "IP",
|
EditIPv4Preference(title = "IP",
|
||||||
value = networkInput.ipv4Config.ip,
|
value = networkInput.ipv4Config.ip,
|
||||||
enabled = connected && networkInput.addressMode == NetworkConfig.AddressMode.STATIC,
|
enabled = connected && networkInput.addressMode == NetworkConfig.AddressMode.STATIC,
|
||||||
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
|
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
|
||||||
|
@ -489,7 +490,7 @@ fun DeviceSettingsItemList(viewModel: UIViewModel) {
|
||||||
}
|
}
|
||||||
|
|
||||||
item {
|
item {
|
||||||
EditTextPreference(title = "Gateway",
|
EditIPv4Preference(title = "Gateway",
|
||||||
value = networkInput.ipv4Config.gateway,
|
value = networkInput.ipv4Config.gateway,
|
||||||
enabled = connected && networkInput.addressMode == NetworkConfig.AddressMode.STATIC,
|
enabled = connected && networkInput.addressMode == NetworkConfig.AddressMode.STATIC,
|
||||||
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
|
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
|
||||||
|
@ -500,7 +501,7 @@ fun DeviceSettingsItemList(viewModel: UIViewModel) {
|
||||||
}
|
}
|
||||||
|
|
||||||
item {
|
item {
|
||||||
EditTextPreference(title = "Subnet",
|
EditIPv4Preference(title = "Subnet",
|
||||||
value = networkInput.ipv4Config.subnet,
|
value = networkInput.ipv4Config.subnet,
|
||||||
enabled = connected && networkInput.addressMode == NetworkConfig.AddressMode.STATIC,
|
enabled = connected && networkInput.addressMode == NetworkConfig.AddressMode.STATIC,
|
||||||
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
|
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
|
||||||
|
@ -511,7 +512,7 @@ fun DeviceSettingsItemList(viewModel: UIViewModel) {
|
||||||
}
|
}
|
||||||
|
|
||||||
item {
|
item {
|
||||||
EditTextPreference(title = "DNS",
|
EditIPv4Preference(title = "DNS",
|
||||||
value = networkInput.ipv4Config.dns,
|
value = networkInput.ipv4Config.dns,
|
||||||
enabled = connected && networkInput.addressMode == NetworkConfig.AddressMode.STATIC,
|
enabled = connected && networkInput.addressMode == NetworkConfig.AddressMode.STATIC,
|
||||||
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
|
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.geeksville.mesh.ui.components
|
package com.geeksville.mesh.ui.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.text.KeyboardActions
|
import androidx.compose.foundation.text.KeyboardActions
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
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
|
@Composable
|
||||||
fun EditTextPreference(
|
fun EditTextPreference(
|
||||||
title: String,
|
title: String,
|
||||||
|
@ -142,6 +186,7 @@ fun EditTextPreference(
|
||||||
@Preview(showBackground = true)
|
@Preview(showBackground = true)
|
||||||
@Composable
|
@Composable
|
||||||
private fun EditTextPreferencePreview() {
|
private fun EditTextPreferencePreview() {
|
||||||
|
Column {
|
||||||
EditTextPreference(
|
EditTextPreference(
|
||||||
title = "Advanced Settings",
|
title = "Advanced Settings",
|
||||||
value = UInt.MAX_VALUE.toInt(),
|
value = UInt.MAX_VALUE.toInt(),
|
||||||
|
@ -149,4 +194,12 @@ private fun EditTextPreferencePreview() {
|
||||||
keyboardActions = KeyboardActions {},
|
keyboardActions = KeyboardActions {},
|
||||||
onValueChanged = {}
|
onValueChanged = {}
|
||||||
)
|
)
|
||||||
|
EditIPv4Preference(
|
||||||
|
title = "IP Address",
|
||||||
|
value = 3232235521.toInt(),
|
||||||
|
enabled = true,
|
||||||
|
keyboardActions = KeyboardActions {},
|
||||||
|
onValueChanged = {}
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue