refactor(ui): вынести LabeledTextField и убрать бойлерплейт полей
Общая обёртка над OutlinedTextField для простых полей-настроек (метка/плейсхолдер/подсказка из ресурсов, fillMaxWidth+singleLine). Внедрена в ConnectionCard, ServerConfigCard и SSH-форму.
This commit is contained in:
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))
|
||||
SettingsCard {
|
||||
SettingsFieldSlot {
|
||||
OutlinedTextField(
|
||||
LabeledTextField(
|
||||
value = ip,
|
||||
onValueChange = onIpChange,
|
||||
label = { Text(stringResource(R.string.server_ip_label)) },
|
||||
placeholder = { Text(stringResource(R.string.server_ip_placeholder)) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
labelRes = R.string.server_ip_label,
|
||||
placeholderRes = R.string.server_ip_placeholder,
|
||||
isError = showErrors && ip.isBlank(),
|
||||
supportingText = if (showErrors && ip.isBlank()) {
|
||||
{ Text(stringResource(R.string.setup_field_required)) }
|
||||
} else null,
|
||||
errorRes = R.string.setup_field_required,
|
||||
keyboardOptions = KeyboardOptions(
|
||||
keyboardType = KeyboardType.Uri,
|
||||
imeAction = ImeAction.Next
|
||||
@@ -73,16 +69,12 @@ fun SshFormFields(
|
||||
}
|
||||
SettingsRowDivider()
|
||||
SettingsFieldSlot {
|
||||
OutlinedTextField(
|
||||
LabeledTextField(
|
||||
value = port,
|
||||
onValueChange = { onPortChange(it.filter { c -> c.isDigit() }) },
|
||||
label = { Text(stringResource(R.string.ssh_port)) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
labelRes = R.string.ssh_port,
|
||||
isError = showErrors && portInvalid,
|
||||
supportingText = if (showErrors && portInvalid) {
|
||||
{ Text(stringResource(R.string.setup_port_invalid)) }
|
||||
} else null,
|
||||
errorRes = R.string.setup_port_invalid,
|
||||
keyboardOptions = KeyboardOptions(
|
||||
keyboardType = KeyboardType.Number,
|
||||
imeAction = ImeAction.Next
|
||||
@@ -95,12 +87,10 @@ fun SshFormFields(
|
||||
SectionLabel(stringResource(R.string.authentication))
|
||||
SettingsCard {
|
||||
SettingsFieldSlot {
|
||||
OutlinedTextField(
|
||||
LabeledTextField(
|
||||
value = username,
|
||||
onValueChange = onUsernameChange,
|
||||
label = { Text(stringResource(R.string.username)) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
labelRes = R.string.username,
|
||||
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,16 +1,11 @@
|
||||
@file:OptIn(androidx.compose.material3.ExperimentalMaterial3Api::class)
|
||||
|
||||
package com.freeturn.app.ui.screens.clientsetup
|
||||
|
||||
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.KeyboardType
|
||||
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.SettingsCard
|
||||
import com.freeturn.app.ui.components.SettingsFieldSlot
|
||||
@@ -32,46 +27,40 @@ internal fun ConnectionCard(
|
||||
SectionLabel(stringResource(R.string.connection_title))
|
||||
SettingsCard {
|
||||
SettingsFieldSlot {
|
||||
OutlinedTextField(
|
||||
LabeledTextField(
|
||||
value = serverAddress.redact(privacyMode),
|
||||
onValueChange = { if (!privacyMode) onServerAddress(it) },
|
||||
label = { Text(stringResource(R.string.server_address_label)) },
|
||||
placeholder = { Text(stringResource(R.string.server_address_placeholder)) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
labelRes = R.string.server_address_label,
|
||||
placeholderRes = R.string.server_address_placeholder,
|
||||
supportingRes = R.string.server_address_support,
|
||||
readOnly = privacyMode,
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri),
|
||||
supportingText = { Text(stringResource(R.string.server_address_support)) }
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri)
|
||||
)
|
||||
}
|
||||
if (showVkLink) {
|
||||
SettingsRowDivider()
|
||||
SettingsFieldSlot {
|
||||
OutlinedTextField(
|
||||
LabeledTextField(
|
||||
value = vkLink.redact(privacyMode),
|
||||
onValueChange = { if (!privacyMode) onVkLink(it) },
|
||||
label = { Text(stringResource(R.string.call_link_label)) },
|
||||
placeholder = { Text(stringResource(R.string.call_link_placeholder)) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
labelRes = R.string.call_link_label,
|
||||
placeholderRes = R.string.call_link_placeholder,
|
||||
supportingRes = R.string.call_link_support,
|
||||
readOnly = privacyMode,
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri),
|
||||
supportingText = { Text(stringResource(R.string.call_link_support)) }
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri)
|
||||
)
|
||||
}
|
||||
}
|
||||
SettingsRowDivider()
|
||||
SettingsFieldSlot {
|
||||
OutlinedTextField(
|
||||
LabeledTextField(
|
||||
value = localPort.redact(privacyMode),
|
||||
onValueChange = { if (!privacyMode) onLocalPort(it) },
|
||||
label = { Text(stringResource(R.string.local_listen_address)) },
|
||||
placeholder = { Text(stringResource(R.string.local_listen_placeholder)) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
labelRes = R.string.local_listen_address,
|
||||
placeholderRes = R.string.local_listen_placeholder,
|
||||
supportingRes = R.string.local_listen_support,
|
||||
readOnly = privacyMode,
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri),
|
||||
supportingText = { Text(stringResource(R.string.local_listen_support)) }
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+16
-27
@@ -1,16 +1,11 @@
|
||||
@file:OptIn(androidx.compose.material3.ExperimentalMaterial3Api::class)
|
||||
|
||||
package com.freeturn.app.ui.screens.servermanagement
|
||||
|
||||
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.KeyboardType
|
||||
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.SettingsCard
|
||||
import com.freeturn.app.ui.components.SettingsFieldSlot
|
||||
@@ -29,41 +24,35 @@ internal fun ServerConfigCard(
|
||||
SectionLabel(stringResource(R.string.server_config))
|
||||
SettingsCard {
|
||||
SettingsFieldSlot {
|
||||
OutlinedTextField(
|
||||
LabeledTextField(
|
||||
value = listenIp,
|
||||
onValueChange = { v -> onListenIp(v.filter { c -> c.isDigit() || c == '.' || c == ':' }) },
|
||||
label = { Text(stringResource(R.string.listen_ip)) },
|
||||
placeholder = { Text(stringResource(R.string.listen_ip_placeholder)) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri),
|
||||
supportingText = { Text(stringResource(R.string.listen_ip_desc)) }
|
||||
labelRes = R.string.listen_ip,
|
||||
placeholderRes = R.string.listen_ip_placeholder,
|
||||
supportingRes = R.string.listen_ip_desc,
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri)
|
||||
)
|
||||
}
|
||||
SettingsRowDivider()
|
||||
SettingsFieldSlot {
|
||||
OutlinedTextField(
|
||||
LabeledTextField(
|
||||
value = listenPort,
|
||||
onValueChange = { onListenPort(it.filter { c -> c.isDigit() }) },
|
||||
label = { Text(stringResource(R.string.listen_port)) },
|
||||
placeholder = { Text(stringResource(R.string.listen_port_placeholder)) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
|
||||
supportingText = { Text(stringResource(R.string.listen_port_desc)) }
|
||||
labelRes = R.string.listen_port,
|
||||
placeholderRes = R.string.listen_port_placeholder,
|
||||
supportingRes = R.string.listen_port_desc,
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
|
||||
)
|
||||
}
|
||||
SettingsRowDivider()
|
||||
SettingsFieldSlot {
|
||||
OutlinedTextField(
|
||||
LabeledTextField(
|
||||
value = connect,
|
||||
onValueChange = onConnect,
|
||||
label = { Text(stringResource(R.string.turn_client_address)) },
|
||||
placeholder = { Text(stringResource(R.string.turn_client_placeholder)) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri),
|
||||
supportingText = { Text(stringResource(R.string.turn_client_desc)) }
|
||||
labelRes = R.string.turn_client_address,
|
||||
placeholderRes = R.string.turn_client_placeholder,
|
||||
supportingRes = R.string.turn_client_desc,
|
||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user