https://kotlinlang.org logo
Title
r

Ravi

07/16/2021, 11:30 AM
hi I’m facing recomposition issue, more in 🧵
val moduleItems: SnapshotStateList<TestModuleItem> = mutableStateListOf<TestModuleItem>().apply {
    repeat(15) {
        add(
            TestModuleItem(
                title = "Item: $it",
                selected = false
            )
        )
    }
}

@Composable
fun TestRender() {
    Scaffold(
        topBar = {
            Text(text = "Test Application")
        }
    ) {
        TestList(
            items = moduleItems,
            onSelected = { index, flag ->
                val item = moduleItems[index]
                moduleItems[index] = item.copy(selected = flag)
            }
        )
    }
}

@Composable
fun TestList(
    modifier: Modifier = Modifier,
    items: List<TestModuleItem>,
    onSelected: (index: Int, selected: Boolean) -> Unit
) {
    LazyColumn(modifier = modifier.fillMaxSize(), verticalArrangement = Arrangement.SpaceAround) {
        itemsIndexed(items) { index, item ->
            TestCompose(index, item, onSelected)
        }
    }
}


@Composable
fun TestCompose(
    index: Int,
    item: TestModuleItem,
    onSelected: (index: Int, selected: Boolean) -> Unit
) {
    println("current item, ${item.title}")
    Row(
        modifier = Modifier.fillMaxWidth()
    ) {
        Text(text = "post ${item.title}")
        Checkbox(checked = item.selected, onCheckedChange = {
            onSelected(index, it)
        })
    }
}
data class TestModuleItem(
    val title: String,
    val selected: Boolean
)
if
TestModuleItem
is present in different module, on checkbox change all the items are getting recomposed.
current item
is getting printed for all items when I toggle any checkbox
I’ve found two ways to fix this issue 1. Enable compose add compose-rutime dep to other module where
TestModuleItem
is present
buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
    }
2. instead of passing
TestModuleItem
in
TestCompose
pass its properties,
TestCompose(index, item.title, item.selected, onSelected)
I’m not facing this issue if
TestModuleItem
is present in the same module as compose code
m

miqbaldc

07/17/2021, 5:26 PM
This might be intended (as of for now): https://issuetracker.google.com/issues/191068806#comment2