|
sealed class ButtonSize() { |
|
object Small : ButtonSize() |
|
object Medium : ButtonSize() |
|
object Large : ButtonSize() |
|
object Huge : ButtonSize() |
|
data class Custom( |
|
val heightDpInt: Int, |
|
val color: Color, |
|
val textResId: Int |
|
) : ButtonSize() |
|
} |
|
|
|
@Composable |
|
fun RenderButton( |
|
buttonSize: ButtonSize, |
|
onButtonClick: () -> Unit |
|
) { |
|
Button( |
|
modifier = Modifier |
|
.height(buttonSize.getButtonHeight()) |
|
.fillMaxWidth(), |
|
onClick = onButtonClick, |
|
colors = ButtonDefaults.buttonColors( |
|
backgroundColor = buttonSize.getButtonColor(), |
|
), |
|
content = { |
|
Text(text = stringResource(buttonSize.getButtonText())) |
|
} |
|
) |
|
} |
|
|
|
private fun ButtonSize.getButtonHeight(): Dp { |
|
return when (this) { |
|
ButtonSize.Small -> 16 // Code duplication |
|
ButtonSize.Medium -> 24 // Code duplication |
|
ButtonSize.Large -> 32 // Code duplication |
|
ButtonSize.Huge -> 40 // Code duplication |
|
is ButtonSize.Custom -> this.heightDpInt // Code duplication |
|
} |
|
} |
|
|
|
private fun ButtonSize.getButtonText(): Int { |
|
return when (this) { |
|
ButtonSize.Small -> R.string.small_button_text // Code duplication |
|
ButtonSize.Medium -> R.string.medium_button_text // Code duplication |
|
ButtonSize.Large -> R.string.large_button_text // Code duplication |
|
ButtonSize.Huge -> R.string.huge_button_text // Code duplication |
|
is ButtonSize.Custom -> this.textResId // Code duplication |
|
} |
|
} |
|
|
|
private fun ButtonSize.getButtonColor(): Color { |
|
return when (this) { |
|
ButtonSize.Small -> Color.Red // Code duplication |
|
ButtonSize.Medium -> Color.Gray // Code duplication |
|
ButtonSize.Large -> Color.Green // Code duplication |
|
ButtonSize.Huge -> Color.Blue // Code duplication |
|
is ButtonSize.Custom -> this.color // Code duplication |
|
} |
|
} |