https://kotlinlang.org logo
#compose
Title
# compose
s

Shakil Karim

07/09/2021, 5:31 AM
Hi, i using a model class like this SomeClass( var badge: MutableState<Badge?> ...... ) But when i trying to update the badge property it Recompose even if the Badge Object is same.
t

Tash

07/09/2021, 6:50 AM
how are you updating badge?
make sure you're updating
badge.value
and not the
badge
MutableState
instance. might be easier to use property delegates and write it as
var badge by mutableStateOf<Badge?>
instead so that you can read/write to it as a regular
var
.
s

Shakil Karim

07/09/2021, 10:35 AM
I'm updating it like this
Copy code
pack.badgeText = OPEN(context.getString(R.string.open))
z

Zach Klippenstein (he/him) [MOD]

07/09/2021, 4:16 PM
Hard to debug this without seeing more code for context. Can you post the composable that’s recomposing and the code that updates that state?
s

Shakil Karim

07/10/2021, 6:50 PM
I'm not sure it's the correct way to solve this issue, but I have now a map that has Badges and it now only recomposes the item which is change
Copy code
val packBadges = mutableStateMapOf<String,BADGE?>()
and on the Observing side
Copy code
PackItem(
    modifier = Modifier
        .width(boxWidth)
        .padding(horizontal = 8.dp),
    entry.packName,
    entry.lections.map { it.name }.joinToString { it },
    emojiText = entry.emoji,
    coins = entry.coins,
    bagText = packBadges["${entry.lang}${entry.pacId}${entry.packOwnerId}${entry.func}"]
) {
    onPackSelected(entry)
    when(packBadges["${entry.lang}${entry.pacId}${entry.packOwnerId}${entry.func}"]) {
        is OPEN ->  onOpenLections()
        is GET ->  getLections()
        is COIN ->  getLectionsWithCoins(entry.coins)
    }
}
2 Views