Skip to content

Commit d5baa6a

Browse files
committed
Factorize CheckedContent in OudsControlItem
1 parent 5a42528 commit d5baa6a

File tree

3 files changed

+139
-156
lines changed

3 files changed

+139
-156
lines changed

Diff for: core/src/main/java/com/orange/ouds/core/component/OudsCheckboxItem.kt

+33-48
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import androidx.compose.ui.tooling.preview.PreviewParameter
3434
import androidx.compose.ui.unit.dp
3535
import com.orange.ouds.core.R
3636
import com.orange.ouds.core.extensions.collectInteractionStateAsState
37-
import com.orange.ouds.core.utilities.CheckedContent
3837
import com.orange.ouds.core.utilities.LoremIpsumText
3938
import com.orange.ouds.core.utilities.OudsPreview
4039
import com.orange.ouds.foundation.extensions.orElse
@@ -196,56 +195,42 @@ private fun OudsCheckboxItem(
196195
readOnly: Boolean = false,
197196
error: Boolean = false
198197
) {
199-
val isReadOnlyPreviewState = previewState == OudsControlItem.State.ReadOnly
200-
val isDisabledPreviewState = previewState == OudsControlItem.State.Disabled
201-
val isForbidden = error && (readOnly || !enabled || isReadOnlyPreviewState || isDisabledPreviewState)
202-
CheckedContent(
203-
expression = !isForbidden,
204-
exceptionMessage = {
205-
val parameter = if (readOnly) "readOnly" else "enabled"
206-
"An OudsCheckboxItem or OudsTriStateCheckboxItem set to $parameter with error parameter activated is not allowed."
198+
val context = LocalContext.current
199+
val interactionState by interactionSource.collectInteractionStateAsState()
200+
val state = previewState.orElse { rememberOudsControlItemState(enabled = enabled, readOnly = readOnly, interactionState = interactionState) }
201+
202+
OudsControlItem(
203+
state = state,
204+
text = text,
205+
helperText = helperText,
206+
icon = icon,
207+
divider = divider,
208+
inverted = inverted,
209+
enabled = enabled,
210+
readOnly = readOnly,
211+
error = error,
212+
errorComponentName = "OudsCheckboxItem or OudsTriStateCheckboxItem",
213+
indicator = {
214+
OudsCheckboxIndicator(
215+
state = checkboxState(state),
216+
value = value,
217+
error = error
218+
)
207219
},
208-
previewMessage = {
209-
val status = when (value) {
210-
ToggleableState.On -> "Selected"
211-
ToggleableState.Off -> "Unselected"
212-
ToggleableState.Indeterminate -> "Indeterminate"
220+
previewState = previewState,
221+
previewStatus = when (value) {
222+
ToggleableState.On -> "Selected"
223+
ToggleableState.Off -> "Unselected"
224+
ToggleableState.Indeterminate -> "Indeterminate"
225+
},
226+
modifier = modifier.semantics(mergeDescendants = true) {
227+
stateDescription = when (value) {
228+
ToggleableState.Off -> context.getString(R.string.core_checkbox_unchecked_a11y)
229+
ToggleableState.On -> context.getString(R.string.core_checkbox_checked_a11y)
230+
ToggleableState.Indeterminate -> context.getString(R.string.core_checkbox_indeterminate_a11y)
213231
}
214-
val state = if (isReadOnlyPreviewState) "Read only" else "Disabled"
215-
"Error $status status for $state state is not relevant"
216232
}
217-
) {
218-
val context = LocalContext.current
219-
val interactionState by interactionSource.collectInteractionStateAsState()
220-
val state = previewState.orElse { rememberOudsControlItemState(enabled = enabled, readOnly = readOnly, interactionState = interactionState) }
221-
222-
OudsControlItem(
223-
state = state,
224-
text = text,
225-
helperText = helperText,
226-
icon = icon,
227-
divider = divider,
228-
inverted = inverted,
229-
enabled = enabled,
230-
readOnly = readOnly,
231-
error = error,
232-
errorComponentName = "OudsCheckboxItem or OudsTriStateCheckboxItem",
233-
indicator = {
234-
OudsCheckboxIndicator(
235-
state = checkboxState(state),
236-
value = value,
237-
error = error
238-
)
239-
},
240-
modifier = modifier.semantics(mergeDescendants = true) {
241-
stateDescription = when (value) {
242-
ToggleableState.Off -> context.getString(R.string.core_checkbox_unchecked_a11y)
243-
ToggleableState.On -> context.getString(R.string.core_checkbox_checked_a11y)
244-
ToggleableState.Indeterminate -> context.getString(R.string.core_checkbox_indeterminate_a11y)
245-
}
246-
}
247-
)
248-
}
233+
)
249234
}
250235

251236
private fun checkboxState(state: OudsControlItem.State) = when (state) {

Diff for: core/src/main/java/com/orange/ouds/core/component/OudsControlItem.kt

+67-54
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import com.orange.ouds.core.extensions.InteractionState
4242
import com.orange.ouds.core.theme.OudsTheme
4343
import com.orange.ouds.core.theme.outerBorder
4444
import com.orange.ouds.core.theme.value
45-
45+
import com.orange.ouds.core.utilities.CheckedContent
4646

4747
@Composable
4848
internal fun OudsControlItem(
@@ -57,74 +57,87 @@ internal fun OudsControlItem(
5757
error: Boolean,
5858
errorComponentName: String,
5959
indicator: @Composable () -> Unit,
60+
previewState: OudsControlItem.State?,
61+
previewStatus: String,
6062
modifier: Modifier = Modifier,
6163
additionalText: String? = null,
6264
) {
63-
if (error) {
64-
if (readOnly) throw IllegalStateException("An $errorComponentName set to readOnly with error parameter activated is not allowed.")
65-
if (!enabled) throw IllegalStateException("An $errorComponentName set to disabled with error parameter activated is not allowed.")
66-
}
65+
val isReadOnlyPreviewState = previewState == OudsControlItem.State.ReadOnly
66+
val isDisabledPreviewState = previewState == OudsControlItem.State.Disabled
67+
val isForbidden = error && (readOnly || !enabled || isReadOnlyPreviewState || isDisabledPreviewState)
6768

68-
val controlItemTokens = OudsTheme.componentsTokens.controlItem
69+
CheckedContent(
70+
expression = !isForbidden,
71+
exceptionMessage = {
72+
val parameter = if (readOnly) "readOnly" else "disabled"
73+
"An $errorComponentName set to $parameter with error parameter activated is not allowed."
74+
},
75+
previewMessage = {
76+
val stateDescription = if (isReadOnlyPreviewState) "Read only" else "Disabled"
77+
"Error $previewStatus status for $stateDescription state is not relevant"
78+
}
79+
) {
80+
val controlItemTokens = OudsTheme.componentsTokens.controlItem
6981

70-
val itemIcon: (@Composable () -> Unit)? = icon?.let {
71-
{
72-
icon.Content(
73-
extraParameters = OudsControlItem.Icon.ExtraParameters(
74-
tint = if (state == OudsControlItem.State.Disabled) OudsTheme.colorScheme.content.disabled else OudsTheme.colorScheme.content.default
82+
val itemIcon: (@Composable () -> Unit)? = icon?.let {
83+
{
84+
icon.Content(
85+
extraParameters = OudsControlItem.Icon.ExtraParameters(
86+
tint = if (state == OudsControlItem.State.Disabled) OudsTheme.colorScheme.content.disabled else OudsTheme.colorScheme.content.default
87+
)
7588
)
76-
)
89+
}
7790
}
78-
}
7991

80-
val leadingElement: (@Composable () -> Unit)? = if (inverted) itemIcon else indicator
81-
val trailingElement: (@Composable () -> Unit)? = if (inverted) indicator else itemIcon
82-
val dividerThickness = 1.dp
92+
val leadingElement: (@Composable () -> Unit)? = if (inverted) itemIcon else indicator
93+
val trailingElement: (@Composable () -> Unit)? = if (inverted) indicator else itemIcon
94+
val dividerThickness = 1.dp
8395

84-
Column(
85-
modifier = modifier
86-
.height(IntrinsicSize.Min)
87-
.heightIn(min = controlItemTokens.sizeMinHeight.dp)
88-
.widthIn(min = controlItemTokens.sizeMinWidth.dp)
89-
.background(color = backgroundColor(state = state))
90-
.outerBorder(state = state)
91-
) {
92-
Row(
93-
modifier = Modifier
94-
.weight(1f)
95-
.padding(all = controlItemTokens.spaceInset.value),
96-
horizontalArrangement = Arrangement.spacedBy(controlItemTokens.spaceColumnGap.value)
96+
Column(
97+
modifier = modifier
98+
.height(IntrinsicSize.Min)
99+
.heightIn(min = controlItemTokens.sizeMinHeight.dp)
100+
.widthIn(min = controlItemTokens.sizeMinWidth.dp)
101+
.background(color = backgroundColor(state = state))
102+
.outerBorder(state = state)
97103
) {
98-
leadingElement?.let { LeadingTrailingBox(leadingElement) }
99-
Column(
104+
Row(
100105
modifier = Modifier
101106
.weight(1f)
102-
.align(Alignment.CenterVertically),
103-
verticalArrangement = Arrangement.spacedBy(controlItemTokens.spaceRowGap.value)
107+
.padding(all = controlItemTokens.spaceInset.value),
108+
horizontalArrangement = Arrangement.spacedBy(controlItemTokens.spaceColumnGap.value)
104109
) {
105-
Text(text = text, style = OudsTheme.typography.label.default.large, color = textColor(state = state, error = error))
106-
if (!additionalText.isNullOrBlank()) {
107-
Text(
108-
text = additionalText,
109-
style = OudsTheme.typography.label.strong.medium,
110-
color = additionalTextColor(state = state)
111-
)
112-
}
113-
if (!helperText.isNullOrBlank()) {
114-
Text(
115-
text = helperText,
116-
style = OudsTheme.typography.label.default.medium,
117-
color = helperTextColor(state = state)
118-
)
110+
leadingElement?.let { LeadingTrailingBox(leadingElement) }
111+
Column(
112+
modifier = Modifier
113+
.weight(1f)
114+
.align(Alignment.CenterVertically),
115+
verticalArrangement = Arrangement.spacedBy(controlItemTokens.spaceRowGap.value)
116+
) {
117+
Text(text = text, style = OudsTheme.typography.label.default.large, color = textColor(state = state, error = error))
118+
if (!additionalText.isNullOrBlank()) {
119+
Text(
120+
text = additionalText,
121+
style = OudsTheme.typography.label.strong.medium,
122+
color = additionalTextColor(state = state)
123+
)
124+
}
125+
if (!helperText.isNullOrBlank()) {
126+
Text(
127+
text = helperText,
128+
style = OudsTheme.typography.label.default.medium,
129+
color = helperTextColor(state = state)
130+
)
131+
}
119132
}
133+
trailingElement?.let { LeadingTrailingBox(trailingElement) }
134+
}
135+
if (divider) {
136+
HorizontalDivider(
137+
thickness = dividerThickness,
138+
color = OudsTheme.colorScheme.border.default.copy(alpha = 0.2f)
139+
) //TODO Replace with OudsHorizontalDivider when available
120140
}
121-
trailingElement?.let { LeadingTrailingBox(trailingElement) }
122-
}
123-
if (divider) {
124-
HorizontalDivider(
125-
thickness = dividerThickness,
126-
color = OudsTheme.colorScheme.border.default.copy(alpha = 0.2f)
127-
) //TODO Replace with OudsHorizontalDivider when available
128141
}
129142
}
130143
}

Diff for: core/src/main/java/com/orange/ouds/core/component/OudsRadioButtonItem.kt

+39-54
Original file line numberDiff line numberDiff line change
@@ -124,64 +124,49 @@ private fun OudsRadioButtonItem(
124124
error: Boolean = false,
125125
interactionSource: MutableInteractionSource? = null
126126
) {
127-
val isReadOnlyPreviewState = previewState == OudsControlItem.State.ReadOnly
128-
val isDisabledPreviewState = previewState == OudsControlItem.State.Disabled
129-
val isForbidden = error && (readOnly || !enabled || isReadOnlyPreviewState || isDisabledPreviewState)
127+
val radioButtonItemInteractionSource = interactionSource ?: remember { MutableInteractionSource() }
128+
val interactionState by radioButtonItemInteractionSource.collectInteractionStateAsState()
129+
val state = previewState.orElse { rememberOudsControlItemState(enabled = enabled, readOnly = readOnly, interactionState = interactionState) }
130130

131-
CheckedContent(
132-
expression = !isForbidden,
133-
exceptionMessage = {
134-
val parameter = if (readOnly) "readOnly" else "enabled"
135-
"An OudsCheckboxItem or OudsTriStateCheckboxItem set to $parameter with error parameter activated is not allowed."
136-
},
137-
previewMessage = {
138-
val status = if (selected) "Selected" else "Unselected"
139-
val state = if (isReadOnlyPreviewState) "Read only" else "Disabled"
140-
"Error $status status for $state state is not relevant"
141-
}
142-
) {
143-
val radioButtonItemInteractionSource = interactionSource ?: remember { MutableInteractionSource() }
144-
val interactionState by radioButtonItemInteractionSource.collectInteractionStateAsState()
145-
val state = previewState.orElse { rememberOudsControlItemState(enabled = enabled, readOnly = readOnly, interactionState = interactionState) }
131+
val selectableModifier = if (onClick != null) {
132+
Modifier.selectable(
133+
selected = selected,
134+
onClick = onClick,
135+
enabled = enabled && !readOnly,
136+
interactionSource = radioButtonItemInteractionSource,
137+
indication = null,
138+
role = Role.RadioButton,
139+
)
140+
} else {
141+
Modifier
142+
}
146143

147-
val selectableModifier = if (onClick != null) {
148-
Modifier.selectable(
144+
OudsControlItem(
145+
state = state,
146+
text = text,
147+
additionalText = additionalText,
148+
helperText = helperText,
149+
icon = icon,
150+
divider = if (outlined && outlineBorderColor(state = state, selected = selected, error = error) != null) false else divider,
151+
inverted = inverted,
152+
enabled = enabled,
153+
readOnly = readOnly,
154+
error = error,
155+
errorComponentName = "OudsRadioButtonItem",
156+
indicator = {
157+
OudsRadioButtonIndicator(
158+
state = radioButtonState(state),
149159
selected = selected,
150-
onClick = onClick,
151-
enabled = enabled && !readOnly,
152-
interactionSource = radioButtonItemInteractionSource,
153-
indication = null,
154-
role = Role.RadioButton,
160+
error = error
155161
)
156-
} else {
157-
Modifier
158-
}
159-
160-
OudsControlItem(
161-
state = state,
162-
text = text,
163-
additionalText = additionalText,
164-
helperText = helperText,
165-
icon = icon,
166-
divider = if (outlined && outlineBorderColor(state = state, selected = selected, error = error) != null) false else divider,
167-
inverted = inverted,
168-
enabled = enabled,
169-
readOnly = readOnly,
170-
error = error,
171-
errorComponentName = "OudsRadioButtonItem",
172-
indicator = {
173-
OudsRadioButtonIndicator(
174-
state = radioButtonState(state),
175-
selected = selected,
176-
error = error
177-
)
178-
},
179-
modifier = modifier
180-
.then(selectableModifier)
181-
.semantics(mergeDescendants = true) {}
182-
.border(outlined = outlined, selected = selected, error = error, state = state)
183-
)
184-
}
162+
},
163+
previewState = previewState,
164+
previewStatus = if (selected) "Selected" else "Unselected",
165+
modifier = modifier
166+
.then(selectableModifier)
167+
.semantics(mergeDescendants = true) {}
168+
.border(outlined = outlined, selected = selected, error = error, state = state)
169+
)
185170
}
186171

187172
private fun radioButtonState(state: OudsControlItem.State) = when (state) {

0 commit comments

Comments
 (0)