Skip to content

Commit

Permalink
[#83] Improve code by using enum directly in the MutableState
Browse files Browse the repository at this point in the history
  • Loading branch information
paulinea committed May 12, 2022
1 parent 7305566 commit 5d16ba8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ fun SubComponentTextField(subComponent: SubComponent) {
) {
LabelledRadioButton(
selectedRadio = customizationState.selectedState,
currentRadio = TextFieldCustomizationState.Companion.DisplayType.DEFAULT.name,
currentRadio = TextFieldCustomizationState.Companion.DisplayType.DEFAULT,
label = stringResource(id = R.string.component_state_default)
)
LabelledRadioButton(
selectedRadio = customizationState.selectedState,
currentRadio = TextFieldCustomizationState.Companion.DisplayType.ERROR.name,
currentRadio = TextFieldCustomizationState.Companion.DisplayType.ERROR,
label = stringResource(id = R.string.component_state_error)
)
LabelledRadioButton(
selectedRadio = customizationState.selectedState,
currentRadio = TextFieldCustomizationState.Companion.DisplayType.DISABLED.name,
currentRadio = TextFieldCustomizationState.Companion.DisplayType.DISABLED,
label = stringResource(id = R.string.component_state_disabled)
)
}
Expand All @@ -71,17 +71,17 @@ fun SubComponentTextField(subComponent: SubComponent) {
) {
LabelledRadioButton(
selectedRadio = customizationState.selectedTrailingElement,
currentRadio = TextFieldCustomizationState.Companion.TrailingElement.NONE.name,
currentRadio = TextFieldCustomizationState.Companion.TrailingElement.NONE,
label = stringResource(id = R.string.component_element_none)
)
LabelledRadioButton(
selectedRadio = customizationState.selectedTrailingElement,
currentRadio = TextFieldCustomizationState.Companion.TrailingElement.ICON.name,
currentRadio = TextFieldCustomizationState.Companion.TrailingElement.ICON,
label = stringResource(id = R.string.component_element_icon)
)
LabelledRadioButton(
selectedRadio = customizationState.selectedTrailingElement,
currentRadio = TextFieldCustomizationState.Companion.TrailingElement.TEXT.name,
currentRadio = TextFieldCustomizationState.Companion.TrailingElement.TEXT,
label = stringResource(id = R.string.component_element_text)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import com.orange.ods.demo.ui.components.textfields.TextFieldCustomizationState.Companion.DisplayType
import com.orange.ods.demo.ui.components.textfields.TextFieldCustomizationState.Companion.TrailingElement

@Composable
fun rememberTextFieldCustomizationState(
leadingIconChecked: MutableState<Boolean> = rememberSaveable { mutableStateOf(false) },
selectedState: MutableState<String> = rememberSaveable { mutableStateOf(TextFieldCustomizationState.Companion.DisplayType.DEFAULT.name) },
selectedTrailingElement: MutableState<String> = rememberSaveable { mutableStateOf(TextFieldCustomizationState.Companion.TrailingElement.NONE.name) }
selectedDisplayType: MutableState<DisplayType> = rememberSaveable { mutableStateOf(DisplayType.DEFAULT) },
selectedTrailingElement: MutableState<TrailingElement> = rememberSaveable { mutableStateOf(TrailingElement.NONE) }
) =
remember(leadingIconChecked, selectedState, selectedTrailingElement) {
TextFieldCustomizationState(leadingIconChecked, selectedState, selectedTrailingElement)
remember(leadingIconChecked, selectedDisplayType, selectedTrailingElement) {
TextFieldCustomizationState(leadingIconChecked, selectedDisplayType, selectedTrailingElement)
}

class TextFieldCustomizationState(
val leadingIconChecked: MutableState<Boolean>,
val selectedState: MutableState<String>,
val selectedTrailingElement: MutableState<String>
val selectedState: MutableState<DisplayType>,
val selectedTrailingElement: MutableState<TrailingElement>
) {
companion object {
enum class DisplayType {
Expand All @@ -42,14 +44,14 @@ class TextFieldCustomizationState(
}

val enabled
get() = selectedState.value != DisplayType.DISABLED.name
get() = selectedState.value != DisplayType.DISABLED

val isError
get() = selectedState.value == DisplayType.ERROR.name
get() = selectedState.value == DisplayType.ERROR

val hasTrailingIcon
get() = selectedTrailingElement.value == TrailingElement.ICON.name
get() = selectedTrailingElement.value == TrailingElement.ICON

val hasTrailingText
get() = selectedTrailingElement.value == TrailingElement.TEXT.name
get() = selectedTrailingElement.value == TrailingElement.TEXT
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ fun LabelledCheckbox(
}

@Composable
fun LabelledRadioButton(
selectedRadio: MutableState<String>,
currentRadio: String,
fun <T> LabelledRadioButton(
selectedRadio: MutableState<T>,
currentRadio: T,
label: String,
onClick: () -> Unit = {},
enabled: Boolean = true
Expand Down

0 comments on commit 5d16ba8

Please sign in to comment.