Skip to content

Commit

Permalink
[#415] Review: Remove PreviewTrailingType and rename OdsTextFieldTrai…
Browse files Browse the repository at this point in the history
…ling and TrailingPreview methods
  • Loading branch information
florentmaitre committed Jan 30, 2023
1 parent f9adc10 commit c2676a5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 42 deletions.
1 change: 1 addition & 0 deletions lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ dependencies {
api(project(":theme-orange"))

implementation(Dependencies.kotlinStdlibJdk8)
implementation(Dependencies.kotlinReflect)
compileOnly(project(":component-processor"))

implementation(Dependencies.accompanistFlowLayout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ private fun PreviewOdsTextField(@PreviewParameter(OdsTextFieldPreviewParameterPr
onValueChange = { value = it },
placeholder = "Placeholder",
leadingIcon = painterResource(id = android.R.drawable.ic_dialog_info),
trailing = if (parameter.previewTrailingType != PreviewTrailingType.None) {
{ TrailingPreview(parameter.previewTrailingType, value) }
} else null,
trailing = getTrailingPreview(parameter = parameter, value = value),
isError = parameter.hasErrorMessage,
errorMessage = getPreviewErrorMessage(parameter.hasErrorMessage, parameter.isVeryLongErrorMessage),
characterCounter = if (parameter.hasCounter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ private fun PreviewOdsOutlinedTextField(@PreviewParameter(OdsTextFieldPreviewPar
onValueChange = { value = it },
placeholder = "Placeholder",
leadingIcon = painterResource(id = android.R.drawable.ic_dialog_info),
trailing = if (parameter.previewTrailingType != PreviewTrailingType.None) {
{ TrailingPreview(parameter.previewTrailingType, value) }
} else null,
trailing = getTrailingPreview(parameter = parameter, value = value),
isError = parameter.hasErrorMessage,
errorMessage = if (parameter.hasErrorMessage) "Error message" else null
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fun OdsTextField(
leadingIcon = leadingIcon,
leadingIconContentDescription = leadingIconContentDescription,
onLeadingIconClick = onLeadingIconClick,
trailing = { OdsTextFieldTrailing(trailing = trailing, value = value, enabled = enabled) },
trailing = getTrailing(trailing = trailing, value = value, enabled = enabled),
isError = isError,
errorMessage = errorMessage,
visualTransformation = visualTransformation,
Expand Down Expand Up @@ -262,9 +262,7 @@ private fun PreviewOdsTextField(@PreviewParameter(OdsTextFieldPreviewParameterPr
onValueChange = { value = it },
placeholder = "Placeholder",
leadingIcon = painterResource(id = android.R.drawable.ic_dialog_info),
trailing = if (parameter.previewTrailingType != PreviewTrailingType.None) {
{ TrailingPreview(parameter.previewTrailingType, value) }
} else null,
trailing = getTrailingPreview(parameter = parameter, value = value),
isError = parameter.hasErrorMessage,
errorMessage = getPreviewErrorMessage(parameter.hasErrorMessage, parameter.isVeryLongErrorMessage),
characterCounter = if (parameter.hasCounter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.orange.ods.compose.component.utilities.BasicPreviewParameterProvider
import com.orange.ods.compose.component.utilities.DisabledInteractionSource
import com.orange.ods.compose.text.OdsTextCaption
import com.orange.ods.compose.theme.OdsTheme
import kotlin.reflect.KClass

/**
* A character counter to display below the text field
Expand Down Expand Up @@ -76,23 +77,26 @@ internal fun OdsTextFieldIcon(painter: Painter, contentDescription: String?, onC
}
}

@Composable
internal fun OdsTextFieldTrailing(trailing: OdsTextFieldTrailing, value: String, enabled: Boolean = true) {
when (trailing) {
internal fun getTrailing(trailing: OdsTextFieldTrailing, value: String, enabled: Boolean = true): @Composable (() -> Unit) {
return when (trailing) {
is OdsTextTrailing -> {
Text(
modifier = Modifier.padding(end = dimensionResource(id = R.dimen.spacing_s)),
text = trailing.text,
style = OdsTheme.typography.subtitle1,
color = OdsTextFieldDefaults.trailingTextColor(value.isEmpty(), enabled)
)
{
Text(
modifier = Modifier.padding(end = dimensionResource(id = R.dimen.spacing_s)),
text = trailing.text,
style = OdsTheme.typography.subtitle1,
color = OdsTextFieldDefaults.trailingTextColor(value.isEmpty(), enabled)
)
}
}
is OdsIconTrailing -> {
OdsTextFieldIcon(
painter = trailing.painter,
contentDescription = trailing.contentDescription,
onClick = if (enabled) trailing.onClick else null,
)
{
OdsTextFieldIcon(
painter = trailing.painter,
contentDescription = trailing.contentDescription,
onClick = if (enabled) trailing.onClick else null,
)
}
}
}
}
Expand All @@ -110,39 +114,30 @@ private fun OdsTextFieldErrorText(message: String) {
)
}

internal enum class PreviewTrailingType {
Text, Icon, None
}

@Composable
internal fun TrailingPreview(previewTrailingType: PreviewTrailingType, value: String) {
when (previewTrailingType) {
PreviewTrailingType.Icon -> {
OdsTextFieldTrailing(
trailing = OdsIconTrailing(painter = painterResource(id = android.R.drawable.ic_input_add)),
value = value
)
}
PreviewTrailingType.Text -> {
OdsTextFieldTrailing(trailing = OdsTextTrailing(text = "units"), value = value)
}
else -> {}
internal fun getTrailingPreview(parameter: OdsTextFieldPreviewParameter, value: String): @Composable (() -> Unit)? {
val trailing = when (parameter.previewTrailingType) {
OdsTextTrailing::class -> OdsTextTrailing(text = "units")
OdsIconTrailing::class -> OdsIconTrailing(painter = painterResource(id = android.R.drawable.ic_input_add))
else -> null
}

return trailing?.let { getTrailing(trailing = it, value = value) }
}

internal data class OdsTextFieldPreviewParameter(
val hasCounter: Boolean,
val hasErrorMessage: Boolean,
val isVeryLongErrorMessage: Boolean,
val previewTrailingType: PreviewTrailingType
val previewTrailingType: KClass<out OdsTextFieldTrailing>?
)

internal class OdsTextFieldPreviewParameterProvider : BasicPreviewParameterProvider<OdsTextFieldPreviewParameter>(*previewParameterValues.toTypedArray())

private val previewParameterValues: List<OdsTextFieldPreviewParameter>
get() {
val booleanValues = listOf(true, false)
val trailings = PreviewTrailingType.values().toList()
val trailings = listOf(null, OdsTextTrailing::class, OdsIconTrailing::class)

return booleanValues.flatMap { hasCounter ->
booleanValues.flatMap { hasErrorMessage ->
Expand Down

0 comments on commit c2676a5

Please sign in to comment.