0

i want to organize passing arguments with lazycolumn with different Item. My lazycolumn is loading different item cards. For example:

if id is 1 then CardItem1 is loaded, if id is 2 then CardItem2 is loaded ect.

In order to make all item cards clickable, I pass the onClick parameter to every Item I have. There can be more than 10 such Items.

It turns out that I need to write repetitive code many times. I find this approach not entirely clean.

Is there any other way to correctly implement the passing of arguments from the lazycolumn card to the Detail and avoid code repetition?

Or is this approach acceptable?

Item example

@Composable
fun Item1(course: Course, onClick: (Course) -> Unit) {
    Card(
        shape = RoundedCornerShape(16.dp),
        modifier = Modifier
            .fillMaxWidth()
            .padding(dimensionResource(id = R.dimen.card_padding))
            .clickable { onClick(course) },
    ) {
        //any code
    }
}

Lazy Column

        LazyColumn {
            items(items = getAllData) { course ->
                when (course.id) {
                    1 -> Item1(course) { toDetail(navController, course) }
                    2 -> Item2(course) { toDetail(navController, course) }
                    3 -> Item3(course) { toDetail(navController, course) }
                    4 -> Item4(course) { toDetail(navController, course) }
                    5 -> Item5(course) { toDetail(navController, course) }
                    6 -> Item6(course) { toDetail(navController, course) }
                }
            }
        }

ToDetail

private fun toDetail(
    navController: NavController,
    course: Course
) {
    navController.navigate(
        route = Screen.Course.passId(1)
    )
}

0 Answers0