xxfast
05/18/2022, 3:20 PMephemient
05/18/2022, 3:36 PMvalue class Fruit(val name: String) {
@StringRes val stringRes: Int
get() = when (this) {
Apple -> R.string.fruit_apple
else -> 0
}
companion object {
val Apple = Fruit("Apple")
}
}
but YMMVxxfast
05/18/2022, 8:31 PMephemient
05/19/2022, 5:04 AMfun Fruit.getLocalizedName(resources: Resources): String {
val id = stringRes
return if (id != Resources.ID_NULL) resources.getString(id) else name
}
to avoid having to write that check in multiple places thoughxxfast
05/19/2022, 7:51 AM@Composable
fun FruitView(fruit: Fruit) {
when (fruit) {
is Pome -> when (fruit) {
Apple -> AppleView(fruit)
Pears -> PearView(fruit)
Quince -> QuinceView(fruit)
}
is Citrus -> when(fruit){
Oranges -> OrangeView(fruit)
Lime -> LimeView(fruit)
Lemon -> LemonView(fruit)
}
else -> OtherFruitView(fruit.name)
}
}
p.s mind the nested whens here