Skip to content

Instantly share code, notes, and snippets.

@Raiden18
Last active June 2, 2022 09:42
Show Gist options
  • Save Raiden18/f84fd23705fd0093634f76094800c2b1 to your computer and use it in GitHub Desktop.
Save Raiden18/f84fd23705fd0093634f76094800c2b1 to your computer and use it in GitHub Desktop.
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment