refactor(ui): вынести LabeledTextField и убрать бойлерплейт полей

Общая обёртка над OutlinedTextField для простых полей-настроек
(метка/плейсхолдер/подсказка из ресурсов, fillMaxWidth+singleLine).
Внедрена в ConnectionCard, ServerConfigCard и SSH-форму.
This commit is contained in:
samosvalishe committed 2026-06-14 02:05:09 +03:00
1 parent 1ce6a89b32
commit 6f09992fe0
4 files changed
+96 -73

No files matched your search

@@ -0,0 +1,55 @@
@file:OptIn(androidx.compose.material3.ExperimentalMaterial3Api::class)
package com.freeturn.app.ui.components
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.VisualTransformation
/**
* OutlinedTextField для простого поля-настройки: метка/плейсхолдер/подсказка из ресурсов,
* fillMaxWidth. При [isError] подсказка подменяется [errorRes]. Фильтрацию ввода держит
* вызывающий в [onValueChange].
*/
@Composable
fun LabeledTextField(
value: String,
onValueChange: (String) -> Unit,
@StringRes labelRes: Int,
modifier: Modifier = Modifier,
@StringRes placeholderRes: Int? = null,
@StringRes supportingRes: Int? = null,
@StringRes errorRes: Int? = null,
isError: Boolean = false,
readOnly: Boolean = false,
singleLine: Boolean = true,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
visualTransformation: VisualTransformation = VisualTransformation.None,
trailingIcon: @Composable (() -> Unit)? = null
) {
val supporting: @Composable (() -> Unit)? = when {
isError && errorRes != null -> { { Text(stringResource(errorRes)) } }
supportingRes != null -> { { Text(stringResource(supportingRes)) } }
else -> null
}
OutlinedTextField(
value = value,
onValueChange = onValueChange,
label = { Text(stringResource(labelRes)) },
placeholder = placeholderRes?.let { { Text(stringResource(it)) } },
modifier = modifier.fillMaxWidth(),
singleLine = singleLine,
readOnly = readOnly,
isError = isError,
supportingText = supporting,
keyboardOptions = keyboardOptions,
visualTransformation = visualTransformation,
trailingIcon = trailingIcon
)
}
@@ -54,17 +54,13 @@ fun SshFormFields(
SectionLabel(stringResource(R.string.server_data)) SectionLabel(stringResource(R.string.server_data))
SettingsCard { SettingsCard {
SettingsFieldSlot { SettingsFieldSlot {
OutlinedTextField( LabeledTextField(
value = ip, value = ip,
onValueChange = onIpChange, onValueChange = onIpChange,
label = { Text(stringResource(R.string.server_ip_label)) }, labelRes = R.string.server_ip_label,
placeholder = { Text(stringResource(R.string.server_ip_placeholder)) }, placeholderRes = R.string.server_ip_placeholder,
modifier = Modifier.fillMaxWidth(),
singleLine = true,
isError = showErrors && ip.isBlank(), isError = showErrors && ip.isBlank(),
supportingText = if (showErrors && ip.isBlank()) { errorRes = R.string.setup_field_required,
{ Text(stringResource(R.string.setup_field_required)) }
} else null,
keyboardOptions = KeyboardOptions( keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Uri, keyboardType = KeyboardType.Uri,
imeAction = ImeAction.Next imeAction = ImeAction.Next
@@ -73,16 +69,12 @@ fun SshFormFields(
} }
SettingsRowDivider() SettingsRowDivider()
SettingsFieldSlot { SettingsFieldSlot {
OutlinedTextField( LabeledTextField(
value = port, value = port,
onValueChange = { onPortChange(it.filter { c -> c.isDigit() }) }, onValueChange = { onPortChange(it.filter { c -> c.isDigit() }) },
label = { Text(stringResource(R.string.ssh_port)) }, labelRes = R.string.ssh_port,
modifier = Modifier.fillMaxWidth(),
singleLine = true,
isError = showErrors && portInvalid, isError = showErrors && portInvalid,
supportingText = if (showErrors && portInvalid) { errorRes = R.string.setup_port_invalid,
{ Text(stringResource(R.string.setup_port_invalid)) }
} else null,
keyboardOptions = KeyboardOptions( keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number, keyboardType = KeyboardType.Number,
imeAction = ImeAction.Next imeAction = ImeAction.Next
@@ -95,12 +87,10 @@ fun SshFormFields(
SectionLabel(stringResource(R.string.authentication)) SectionLabel(stringResource(R.string.authentication))
SettingsCard { SettingsCard {
SettingsFieldSlot { SettingsFieldSlot {
OutlinedTextField( LabeledTextField(
value = username, value = username,
onValueChange = onUsernameChange, onValueChange = onUsernameChange,
label = { Text(stringResource(R.string.username)) }, labelRes = R.string.username,
modifier = Modifier.fillMaxWidth(),
singleLine = true,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next) keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next)
) )
} }
@@ -1,16 +1,11 @@
@file:OptIn(androidx.compose.material3.ExperimentalMaterial3Api::class)
package com.freeturn.app.ui.screens.clientsetup package com.freeturn.app.ui.screens.clientsetup
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.KeyboardType
import com.freeturn.app.R import com.freeturn.app.R
import com.freeturn.app.ui.components.LabeledTextField
import com.freeturn.app.ui.components.SectionLabel import com.freeturn.app.ui.components.SectionLabel
import com.freeturn.app.ui.components.SettingsCard import com.freeturn.app.ui.components.SettingsCard
import com.freeturn.app.ui.components.SettingsFieldSlot import com.freeturn.app.ui.components.SettingsFieldSlot
@@ -32,46 +27,40 @@ internal fun ConnectionCard(
SectionLabel(stringResource(R.string.connection_title)) SectionLabel(stringResource(R.string.connection_title))
SettingsCard { SettingsCard {
SettingsFieldSlot { SettingsFieldSlot {
OutlinedTextField( LabeledTextField(
value = serverAddress.redact(privacyMode), value = serverAddress.redact(privacyMode),
onValueChange = { if (!privacyMode) onServerAddress(it) }, onValueChange = { if (!privacyMode) onServerAddress(it) },
label = { Text(stringResource(R.string.server_address_label)) }, labelRes = R.string.server_address_label,
placeholder = { Text(stringResource(R.string.server_address_placeholder)) }, placeholderRes = R.string.server_address_placeholder,
modifier = Modifier.fillMaxWidth(), supportingRes = R.string.server_address_support,
singleLine = true,
readOnly = privacyMode, readOnly = privacyMode,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri), keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri)
supportingText = { Text(stringResource(R.string.server_address_support)) }
) )
} }
if (showVkLink) { if (showVkLink) {
SettingsRowDivider() SettingsRowDivider()
SettingsFieldSlot { SettingsFieldSlot {
OutlinedTextField( LabeledTextField(
value = vkLink.redact(privacyMode), value = vkLink.redact(privacyMode),
onValueChange = { if (!privacyMode) onVkLink(it) }, onValueChange = { if (!privacyMode) onVkLink(it) },
label = { Text(stringResource(R.string.call_link_label)) }, labelRes = R.string.call_link_label,
placeholder = { Text(stringResource(R.string.call_link_placeholder)) }, placeholderRes = R.string.call_link_placeholder,
modifier = Modifier.fillMaxWidth(), supportingRes = R.string.call_link_support,
singleLine = true,
readOnly = privacyMode, readOnly = privacyMode,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri), keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri)
supportingText = { Text(stringResource(R.string.call_link_support)) }
) )
} }
} }
SettingsRowDivider() SettingsRowDivider()
SettingsFieldSlot { SettingsFieldSlot {
OutlinedTextField( LabeledTextField(
value = localPort.redact(privacyMode), value = localPort.redact(privacyMode),
onValueChange = { if (!privacyMode) onLocalPort(it) }, onValueChange = { if (!privacyMode) onLocalPort(it) },
label = { Text(stringResource(R.string.local_listen_address)) }, labelRes = R.string.local_listen_address,
placeholder = { Text(stringResource(R.string.local_listen_placeholder)) }, placeholderRes = R.string.local_listen_placeholder,
modifier = Modifier.fillMaxWidth(), supportingRes = R.string.local_listen_support,
singleLine = true,
readOnly = privacyMode, readOnly = privacyMode,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri), keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri)
supportingText = { Text(stringResource(R.string.local_listen_support)) }
) )
} }
} }
@@ -1,16 +1,11 @@
@file:OptIn(androidx.compose.material3.ExperimentalMaterial3Api::class)
package com.freeturn.app.ui.screens.servermanagement package com.freeturn.app.ui.screens.servermanagement
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.input.KeyboardType
import com.freeturn.app.R import com.freeturn.app.R
import com.freeturn.app.ui.components.LabeledTextField
import com.freeturn.app.ui.components.SectionLabel import com.freeturn.app.ui.components.SectionLabel
import com.freeturn.app.ui.components.SettingsCard import com.freeturn.app.ui.components.SettingsCard
import com.freeturn.app.ui.components.SettingsFieldSlot import com.freeturn.app.ui.components.SettingsFieldSlot
@@ -29,41 +24,35 @@ internal fun ServerConfigCard(
SectionLabel(stringResource(R.string.server_config)) SectionLabel(stringResource(R.string.server_config))
SettingsCard { SettingsCard {
SettingsFieldSlot { SettingsFieldSlot {
OutlinedTextField( LabeledTextField(
value = listenIp, value = listenIp,
onValueChange = { v -> onListenIp(v.filter { c -> c.isDigit() || c == '.' || c == ':' }) }, onValueChange = { v -> onListenIp(v.filter { c -> c.isDigit() || c == '.' || c == ':' }) },
label = { Text(stringResource(R.string.listen_ip)) }, labelRes = R.string.listen_ip,
placeholder = { Text(stringResource(R.string.listen_ip_placeholder)) }, placeholderRes = R.string.listen_ip_placeholder,
modifier = Modifier.fillMaxWidth(), supportingRes = R.string.listen_ip_desc,
singleLine = true, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri)
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri),
supportingText = { Text(stringResource(R.string.listen_ip_desc)) }
) )
} }
SettingsRowDivider() SettingsRowDivider()
SettingsFieldSlot { SettingsFieldSlot {
OutlinedTextField( LabeledTextField(
value = listenPort, value = listenPort,
onValueChange = { onListenPort(it.filter { c -> c.isDigit() }) }, onValueChange = { onListenPort(it.filter { c -> c.isDigit() }) },
label = { Text(stringResource(R.string.listen_port)) }, labelRes = R.string.listen_port,
placeholder = { Text(stringResource(R.string.listen_port_placeholder)) }, placeholderRes = R.string.listen_port_placeholder,
modifier = Modifier.fillMaxWidth(), supportingRes = R.string.listen_port_desc,
singleLine = true, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
supportingText = { Text(stringResource(R.string.listen_port_desc)) }
) )
} }
SettingsRowDivider() SettingsRowDivider()
SettingsFieldSlot { SettingsFieldSlot {
OutlinedTextField( LabeledTextField(
value = connect, value = connect,
onValueChange = onConnect, onValueChange = onConnect,
label = { Text(stringResource(R.string.turn_client_address)) }, labelRes = R.string.turn_client_address,
placeholder = { Text(stringResource(R.string.turn_client_placeholder)) }, placeholderRes = R.string.turn_client_placeholder,
modifier = Modifier.fillMaxWidth(), supportingRes = R.string.turn_client_desc,
singleLine = true, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri)
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri),
supportingText = { Text(stringResource(R.string.turn_client_desc)) }
) )
} }
} }