https://kotlinlang.org logo
Title
r

Rafs

11/22/2021, 9:52 PM
Is there a way to always trigger recomposition by setting a
MutableState
variable, including when the contents are the same? I've Tried creating my own
SnapshotMutationPolicy
but the
merge
method never gets called.
a

André Kindwall

11/23/2021, 2:46 AM
Override the
equals()
function and return false I guess
t

tad

11/23/2021, 3:29 AM
This is a code smell. What are you really trying to do?
a

Andrew Neal

11/23/2021, 3:39 AM
r

Rafs

11/23/2021, 8:46 AM
I've done that already @André Kindwall. Here is a full implementation of my
SnapshotMutationPolicy
private object AlwaysApplyPolicy : SnapshotMutationPolicy<String> {

    override fun equivalent(a: String, b: String): Boolean = false

    override fun merge(previous: String, current: String, applied: String): String? {
        return current
    }

    override fun toString() = "AlwaysApplyPolicy"
}
What I want is for the
State
backed by this snapshot policy to always force a recomposition when it's value get's set, even if the previous value is equal to the current value.
a

André Kindwall

11/23/2021, 12:43 PM
You should probably ask a new question where you describe what you want to achieve, not how you think you can achieve it (the xy problem). It sounds to me like you are trying to trigger some event, if that is the case then that should be handled by collecting a flow inside a sideeffect
t

tad

11/23/2021, 5:49 PM
Why do you want to recompose with the same value?