From d404b95b6d12452d5bce476794e358800bf4b1ae Mon Sep 17 00:00:00 2001 From: David Perez Date: Wed, 29 Nov 2023 11:39:32 -0600 Subject: [PATCH] Add BitwardenPasswordFieldWithActions with manual password visibility (#294) --- .../BitwardenPasswordFieldWithActions.kt | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/BitwardenPasswordFieldWithActions.kt b/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/BitwardenPasswordFieldWithActions.kt index af4d2577ac..3bbd6abfbb 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/BitwardenPasswordFieldWithActions.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/platform/components/BitwardenPasswordFieldWithActions.kt @@ -4,6 +4,10 @@ import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.padding import androidx.compose.runtime.Composable +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.res.painterResource @@ -18,10 +22,12 @@ import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme * Represents a Bitwarden-styled password field that hoists show/hide password state to the caller * and provides additional actions. * - * See overloaded [BitwardenPasswordField] for self managed show/hide state. + * See overloaded [BitwardenPasswordFieldWithActions] for managed show/hide state. * * @param label Label for the text field. * @param value Current next on the text field. + * @param showPassword Whether or not password should be shown. + * @param showPasswordChange Lambda that is called when user request show/hide be toggled. * @param onValueChange Callback that is triggered when the password changes. * @param modifier Modifier for the composable. * @param readOnly `true` if the input should be read-only and not accept user interactions. @@ -35,6 +41,8 @@ import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme fun BitwardenPasswordFieldWithActions( label: String, value: String, + showPassword: Boolean, + showPasswordChange: (Boolean) -> Unit, onValueChange: (String) -> Unit, modifier: Modifier = Modifier, readOnly: Boolean = false, @@ -48,6 +56,8 @@ fun BitwardenPasswordFieldWithActions( BitwardenPasswordField( label = label, value = value, + showPasswordChange = showPasswordChange, + showPassword = showPassword, onValueChange = onValueChange, readOnly = readOnly, singleLine = singleLine, @@ -59,6 +69,50 @@ fun BitwardenPasswordFieldWithActions( } } +/** + * Represents a Bitwarden-styled password field that manages the state of a show/hide indicator + * internally. + * + * See overloaded [BitwardenPasswordFieldWithActions] for self managed show/hide state. + * + * @param label Label for the text field. + * @param value Current next on the text field. + * @param onValueChange Callback that is triggered when the password changes. + * @param modifier Modifier for the composable. + * @param readOnly `true` if the input should be read-only and not accept user interactions. + * @param singleLine when `true`, this text field becomes a single line that horizontally scrolls + * instead of wrapping onto multiple lines. + * @param initialShowPassword The initial state of the show/hide password control. A value of + * `false` (the default) indicates that that password should begin in the hidden state. + * @param actions A lambda containing the set of actions (usually icons or similar) to display + * in the app bar's trailing side. This lambda extends [RowScope], allowing flexibility in + * defining the layout of the actions. + */ +@Composable +fun BitwardenPasswordFieldWithActions( + label: String, + value: String, + onValueChange: (String) -> Unit, + modifier: Modifier = Modifier, + readOnly: Boolean = false, + singleLine: Boolean = false, + initialShowPassword: Boolean = false, + actions: @Composable RowScope.() -> Unit = {}, +) { + var shouldShowPassword by remember { mutableStateOf(initialShowPassword) } + BitwardenPasswordFieldWithActions( + label = label, + value = value, + showPassword = shouldShowPassword, + showPasswordChange = { shouldShowPassword = !shouldShowPassword }, + onValueChange = onValueChange, + modifier = modifier, + readOnly = readOnly, + singleLine = singleLine, + actions = actions, + ) +} + @Preview(showBackground = true) @Composable private fun BitwardenPasswordFieldWithActions_preview() {