We're trying to enforce that everyone uses our pre...
# compose
c
We're trying to enforce that everyone uses our predefined text styles via a lint rule (that warns if you use a Text composable) and by having this MyText defined.
Copy code
@Composable
fun MyText(text: String, style:MyTextStyle, color: Color) {
  val selectedStyle = when (style) {
    MyTextStyle.H1 -> MyTypography.H1 //maps enum to TextStyle
    MyTextStyle.H2 -> MyTypography.H2
  }
  Text(text = text, color = color, style = selectedStyle)
}
Does this seem like a bad idea to anyone? The map of an enum to a text style feels off, but I think this would get the job done.
f
You could define an extension property on the enum to return the text style. But I would think that, instead of passing an enum, it would be better to define dedicated composables for each text style, something like this
Copy code
@Composable
fun HeaderText(text: String, color: Color, ...) {
    Text(
        text = text,
        style = MaterialTheme.typography.h1,
        ...
}
and similar for every other one
👍 1
z
We did exactly that (enum that maps to style) and it's been great. There was a discussion previously on whether you should have multiple composables for the different styles of a composable (outline or normal textfield, etc); it had some good discussions but I can't find it right now (on mobile).
👍 1