Skip to content

Commit

Permalink
fix(PasswordInputDialog): Fix the ugly addition of space for the error
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Nov 28, 2024
1 parent 149d6b3 commit 4e42861
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private fun BasicAlertDialogContent(
Spacer(Modifier.height(Margin.Large))
additionalContent?.let {
it()
Spacer(Modifier.height(Margin.Large))
Spacer(Modifier.height(Margin.Mini))
}
ActionButtons(onDismiss, onConfirmation, shouldEnableConfirmButton)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme

/**
* Wrapper for Material's [OutlinedTextField] that enforce our design needs.
*
* By default, this TextField is single lined. You can specify [maxLineNumber] or [minLineNumber] to make it multi-lined
*
* If [isPassword] value is true, the [keyboardType] field will be ignored to force [KeyboardType.Password]
*
* To set an error message, you need to pass this message as [supportingText] and set [isError] to true
*/
@Composable
fun SwissTransferTextField(
Expand All @@ -62,8 +66,8 @@ fun SwissTransferTextField(
imeAction: ImeAction = ImeAction.Default,
keyboardActions: KeyboardActions = KeyboardActions.Default,
isReadOnly: Boolean = false,
errorMessage: @Composable () -> String? = { null },
supportingText: String? = null,
isError: Boolean = false,
supportingText: @Composable ((Boolean) -> Unit)? = null,
onValueChange: ((String) -> Unit)? = null,
) {

Expand All @@ -72,6 +76,7 @@ fun SwissTransferTextField(
var text by rememberSaveable { mutableStateOf(initialValue) }

val displayLabel = if (isRequired) label else "$label ${stringResource(R.string.textFieldOptional)}"
val shouldDisplayError = isError && text.isNotEmpty()
val textFieldColors = OutlinedTextFieldDefaults.colors(
unfocusedLabelColor = SwissTransferTheme.colors.tertiaryTextColor,
unfocusedSupportingTextColor = SwissTransferTheme.colors.tertiaryTextColor,
Expand All @@ -80,17 +85,6 @@ fun SwissTransferTextField(
disabledTrailingIconColor = SwissTransferTheme.colors.iconColor,
)

@Composable
fun getSupportingText(): (@Composable () -> Unit)? {
val displayText = if (text.isEmpty()) {
supportingText
} else {
errorMessage() ?: supportingText
}

return displayText?.let { @Composable { Text(it) } }
}

OutlinedTextField(
modifier = modifier,
readOnly = isReadOnly,
Expand Down Expand Up @@ -119,8 +113,8 @@ fun SwissTransferTextField(
imeAction = imeAction,
),
keyboardActions = keyboardActions,
isError = errorMessage() != null && text.isNotEmpty(),
supportingText = getSupportingText(),
isError = shouldDisplayError,
supportingText = supportingText?.let { { it.invoke(shouldDisplayError) } },
)
}

Expand All @@ -139,18 +133,21 @@ private fun getShowPasswordButton(shouldShowPassword: Boolean, onClick: () -> Un
@Composable
@Preview
private fun Preview() {
val supportingText = "supporting Text"
val initialValue = "initialValue"

SwissTransferTheme {
Surface {
Column(Modifier.padding(Margin.Medium)) {
SwissTransferTextField(
label = stringResource(R.string.transferMessagePlaceholder),
initialValue = "test",
errorMessage = { null },
)
SwissTransferTextField(
keyboardType = KeyboardType.Email,
initialValue = "a@[email protected]",
errorMessage = { "Invalid address" },
supportingText = { Text("Invalid address") },
isError = true,
label = stringResource(R.string.transferRecipientAddressPlaceholder),
)
SwissTransferTextField(
Expand All @@ -160,25 +157,25 @@ private fun Preview() {
)
SwissTransferTextField(
maxLineNumber = 10,
initialValue = "initial value",
initialValue = initialValue,
isRequired = false,
label = stringResource(R.string.transferMessagePlaceholder),
supportingText = "supporting Text",
supportingText = { Text(supportingText) },
)
SwissTransferTextField(
maxLineNumber = 10,
initialValue = "initial value",
initialValue = initialValue,
isPassword = true,
label = stringResource(R.string.settingsOptionPassword),
supportingText = "supporting Text",
supportingText = { Text(supportingText) },
)
SwissTransferTextField(
maxLineNumber = 10,
initialValue = "initial value",
initialValue = initialValue,
isPassword = true,
label = stringResource(R.string.settingsOptionPassword),
errorMessage = { "Wrong password" },
supportingText = "supporting Text",
isError = true,
supportingText = { Text("Wrong password") },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ val AppIcons.QrCode: ImageVector
arcToRelative(1.5f, 1.5f, 0.0f, false, true, -1.5f, -1.5f)
verticalLineToRelative(-3.75f)
}
}
.build()
}.build()

return _qrCode!!
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
package com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Surface
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
Expand Down Expand Up @@ -75,9 +72,10 @@ fun PasswordOptionAlertDialog(
descriptionRes = R.string.settingsPasswordDescription,
onDismiss = ::onDismiss,
onConfirmation = ::onConfirmButtonClicked,
shouldEnableConfirmButton = { if (isPasswordActivated) isPasswordValid() else true },
shouldEnableConfirmButton = { !isPasswordActivated || isPasswordValid() },
) {
ActivatePasswordSwitch(isChecked = isPasswordActivated, onCheckedChange = { isPasswordActivated = it })
Spacer(Modifier.height(Margin.Medium))
AnimatedPasswordInput(isPasswordActivated, password, isPasswordValid)
}
}
Expand All @@ -102,13 +100,17 @@ private fun ColumnScope.AnimatedPasswordInput(
isPasswordValid: () -> Boolean
) {
AnimatedVisibility(isChecked) {
Spacer(Modifier.height(Margin.Mini))
SwissTransferTextField(
modifier = Modifier.fillMaxWidth(),
label = stringResource(R.string.settingsOptionPassword),
isPassword = true,
initialValue = password.get(),
imeAction = ImeAction.Done,
errorMessage = { if (isPasswordValid()) null else stringResource(R.string.errorTransferPasswordLength) },
isError = !isPasswordValid(),
supportingText = { isError ->
val errorText = if (isError) stringResource(R.string.errorTransferPasswordLength) else ""
Text(text = errorText, minLines = 2)
},
onValueChange = { password.set(it) },
)
}
Expand Down

0 comments on commit 4e42861

Please sign in to comment.