Skip to content

Commit

Permalink
feat: add EditTextPreference byte counter
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir committed Mar 29, 2023
1 parent da1988b commit 1bc8fb3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
32 changes: 16 additions & 16 deletions app/src/main/java/com/geeksville/mesh/ui/DeviceSettingsItemList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ fun DeviceSettingsItemList(viewModel: UIViewModel = viewModel()) {
item {
EditTextPreference(title = "Long name",
value = userInput?.longName ?: stringResource(id = R.string.unknown_username),
maxSize = 39, // long_name max_size:40
enabled = connected && userInput?.longName != null,
isError = userInput?.longName.isNullOrEmpty(),
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Text, imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
onValueChanged = { value ->
if (value.toByteArray().size <= 39) // long_name max_size:40
userInput?.let { userInput = it.copy(longName = value) }
userInput?.let { userInput = it.copy(longName = value) }
if (getInitials(value).toByteArray().size <= 4) // short_name max_size:5
userInput?.let { userInput = it.copy(shortName = getInitials(value)) }
})
Expand All @@ -88,15 +88,15 @@ fun DeviceSettingsItemList(viewModel: UIViewModel = viewModel()) {
item {
EditTextPreference(title = "Short name",
value = userInput?.shortName ?: stringResource(id = R.string.unknown),
maxSize = 4, // short_name max_size:5
enabled = connected && userInput?.shortName != null,
isError = userInput?.shortName.isNullOrEmpty(),
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Text, imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
onValueChanged = { value ->
if (value.toByteArray().size <= 4) // short_name max_size:5
userInput?.let { userInput = it.copy(shortName = value) }
userInput?.let { userInput = it.copy(shortName = value) }
})
}

Expand Down Expand Up @@ -436,60 +436,60 @@ fun DeviceSettingsItemList(viewModel: UIViewModel = viewModel()) {
item {
EditTextPreference(title = "SSID",
value = networkInput.wifiSsid,
maxSize = 32, // wifi_ssid max_size:33
enabled = connected && hasWifi,
isError = false,
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Text, imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
onValueChanged = { value ->
if (value.toByteArray().size <= 32) // wifi_ssid max_size:33
networkInput = networkInput.copy { wifiSsid = value }
onValueChanged = {
networkInput = networkInput.copy { wifiSsid = it }
})
}

item {
EditTextPreference(title = "PSK",
value = networkInput.wifiPsk,
maxSize = 63, // wifi_psk max_size:64
enabled = connected && hasWifi,
isError = false,
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Password, imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
onValueChanged = { value ->
if (value.toByteArray().size <= 63) // wifi_psk max_size:64
networkInput = networkInput.copy { wifiPsk = value }
onValueChanged = {
networkInput = networkInput.copy { wifiPsk = it }
})
}

item {
EditTextPreference(title = "NTP server",
value = networkInput.ntpServer,
maxSize = 32, // ntp_server max_size:33
enabled = connected && hasWifi,
isError = networkInput.ntpServer.isEmpty(),
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Uri, imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
onValueChanged = { value ->
if (value.toByteArray().size <= 32) // ntp_server max_size:33
networkInput = networkInput.copy { ntpServer = value }
onValueChanged = {
networkInput = networkInput.copy { ntpServer = it }
})
}

item {
EditTextPreference(title = "rsyslog server",
value = networkInput.rsyslogServer,
maxSize = 32, // rsyslog_server max_size:33
enabled = connected && hasWifi,
isError = false,
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Uri, imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
onValueChanged = { value ->
if (value.toByteArray().size <= 32) // rsyslog_server max_size:33
networkInput = networkInput.copy { rsyslogServer = value }
onValueChanged = {
networkInput = networkInput.copy { rsyslogServer = it }
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.geeksville.mesh.ui.components

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Icon
Expand All @@ -15,10 +17,13 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusEvent
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@Composable
fun EditTextPreference(
Expand Down Expand Up @@ -166,27 +171,62 @@ fun EditTextPreference(
keyboardActions: KeyboardActions,
onValueChanged: (String) -> Unit,
modifier: Modifier = Modifier,
maxSize: Int = 0, // max_size - 1 (in bytes)
) {
var isFocused by remember { mutableStateOf(false) }

TextField(
value = value,
singleLine = true,
modifier = modifier.fillMaxWidth(),
modifier = modifier
.fillMaxWidth()
.onFocusEvent { isFocused = it.isFocused },
enabled = enabled,
isError = isError,
onValueChange = onValueChanged,
onValueChange = {
if (maxSize > 0) {
if (it.toByteArray().size <= maxSize) {
onValueChanged(it)
}
} else onValueChanged(it)
},
label = { Text(title) },
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
trailingIcon = {
if (isError) Icon(Icons.TwoTone.Info, "Error", tint = MaterialTheme.colors.error)
}
)

if (maxSize > 0 && isFocused) {
Box(
contentAlignment = Alignment.BottomEnd,
modifier = modifier.fillMaxWidth()
) {
Text(
text = "${value.toByteArray().size}/$maxSize",
style = MaterialTheme.typography.caption,
color = if (isError) MaterialTheme.colors.error else MaterialTheme.colors.onBackground,
modifier = Modifier.padding(end = 8.dp, bottom = 4.dp)
)
}
}
}

@Preview(showBackground = true)
@Composable
private fun EditTextPreferencePreview() {
Column {
EditTextPreference(
title = "String",
value = "Meshtastic",
maxSize = 39,
enabled = true,
isError = false,
keyboardOptions = KeyboardOptions.Default,
keyboardActions = KeyboardActions {},
onValueChanged = {},
)
EditTextPreference(
title = "Advanced Settings",
value = UInt.MAX_VALUE.toInt(),
Expand Down

0 comments on commit 1bc8fb3

Please sign in to comment.