Skip to content

Instantly share code, notes, and snippets.

@BraveEvidence
Created February 18, 2023 09:17
Show Gist options
  • Save BraveEvidence/dd5e20838faf56c6f21ad4b7c3d3e2cb to your computer and use it in GitHub Desktop.
Save BraveEvidence/dd5e20838faf56c6f21ad4b7c3d3e2cb to your computer and use it in GitHub Desktop.
Jetpack compose re-rendering issue
// Compose code
Image(
painter = if (expandedList.any { it.isSelected }) painterResource(id = R.drawable.drop_down_expand) else painterResource(
id = R.drawable.drop_down_collapse
),
contentDescription = text, modifier = Modifier.clickable {
manageCredentialViewModel.toggleList(text)
}
)
//View model code
private val _expandedList = MutableStateFlow(
mutableListOf(
VCItem(
item = "Claim requested credentials",
isSelected = false
),
VCItem(
item = "Claim received credentials",
isSelected = false
),
VCItem(
item = "Pending Requests",
isSelected = false
)
)
)
fun toggleList(value: String) {
val item = _expandedList.value.find { it.item == value }
val index = _expandedList.value.indexOfFirst { it.item == value }
if(item != null){
val isSelected = item.isSelected
_expandedList.value[index] = item.copy(isSelected = !isSelected)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment