How can the template I use for my LazyColumn
items be modfied to enable reuse? What can I use instead of it
to ensure my Clothes
sealed interface can be picked up and reused? The code that is a comment is what I was using previously.
@Composable
fun ReusableTitleSubtitle() {
val text1 = when (it) {
is Clothes.FixedSizeClothing -> stringResource(id = it.itemName)
is Clothes.MultiSizeClothing -> stringResource(id = it.itemName)
}
val text2 = when (it) {
is Clothes.FixedSizeClothing ->
stringResource(
id = R.string.size_placeholder,
it.itemPlaceholder
)
is Clothes.MultiSizeClothing ->
stringResource(
id = R.string.sizes_placeholder_and_placeholder,
it.itemPlaceholders[0], it.itemPlaceholders[1]
)
}
Column() {
Text(text = text1)
Text(text = text2)
}
}
sealed interface Clothes {
val itemName: Int
data class FixedSizeClothing(override val itemName: Int, val itemPlaceholder: Int): Clothes
data class MultiSizeClothing(override val itemName: Int, val itemPlaceholders: List<Int>): Clothes
}
@Composable
fun MyScreenContent(
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues()
) {
Box(modifier = modifier.fillMaxSize()) {
val clothesItems = remember {
arrayOf(
Clothes.FixedSizeClothing(itemName = R.string.chine, itemPlaceholder = 3),
Clothes.MultiSizeClothing(itemName = R.string.paisley, itemPlaceholders = listOf(1, 2)),
Clothes.FixedSizeClothing(itemName = R.string.stripy, itemPlaceholder = 7),
Clothes.FixedSizeClothing(itemName = R.string.tartan, itemPlaceholder = 5),
Clothes.FixedSizeClothing(itemName = R.string.tattersall, itemPlaceholder = 8)
)
}
MyLazyColumn(
lazyItems = clothesItems,
modifier = Modifier.padding(contentPadding)
) {
ReusableTitleSubtitle()
// val text1 = when (it) {
// is Clothes.FixedSizeClothing -> stringResource(id = it.itemName)
// is Clothes.MultiSizeClothing -> stringResource(id = it.itemName)
// }
// val text2 = when (it) {
// is Clothes.FixedSizeClothing ->
// stringResource(
// id = R.string.size_placeholder,
// it.itemPlaceholder
// )
// is Clothes.MultiSizeClothing ->
// stringResource(
// id = R.string.sizes_placeholder_and_placeholder,
// it.itemPlaceholders[0], it.itemPlaceholders[1]
// )
// }
// Column() {
// Text(text = text1)
// Text(text = text2)
// }
}
}
}
CodePudding user response:
You can pass as parameter Clothes
:
@Composable
fun ReusableTitleSubtitle(clothes:Clothes) {
val text1 = when (clothes) {
is Clothes.FixedSizeClothing -> //...
is Clothes.MultiSizeClothing -> //...
}
//...
}
@Composable
fun MyLazyColumn(
lazyItems : Array<Clothes>,
modifier : Modifier = Modifier
) {
LazyColumn(
modifier = modifier
) {
items(lazyItems) {
ReusableTitleSubtitle(it)
}
}
}
and then:
MyLazyColumn(
lazyItems = clothesItems,
modifier = Modifier.padding(contentPadding)
)