Hello all. Wondering if someone can point me in t...
# compose
b
Hello all. Wondering if someone can point me in the right direction to detect any change in value from flow which in a composable. I would like to force a recompose even if the value is the same. Is this possible? Example in reply
p
Google Compose state update strategy policy
There are a couple of strategies to check for equality
b
In view model:
Copy code
private val _flow = MutableSharedFlow<String>(
   replay = 1,
   onBufferOverflow = BufferOverflow.DROP_OLDEST
)
val flow: SharedFlow<String> = _flow
Observe in composable:
Copy code
val noticeToMarinersGraphic by viewModel.flow.collectAsState(null)
However, even when using a shared flow, changes in the composable state do not force a recompose if the value is the same. Fairly certain that is because compose sees the state as not changing
k
Compose transforms your state (aka the flow value) into UI, if the state is the same, why should it recompose if nothing actually changed?
p
It could be the case of a container state where 'equal()' is not fully well implemented.
b
@Kevin Del Castillo sure, so maybe I am thinking about this wrong. When a user taps a button, I need to perform some background action. On completion I want to present a dialog. I want to present that dialog every time, regardless of if the value has not changed.
k
Then you'd have a value in your state describing whether the dialog is visible or not, so if for example one time you showed the dialog, hiding it means that you change your state to 'invisible', next time you want to show it again by making it 'visible' it'll work because you're changing the state
b
@Kevin Del Castillo, perfect. I was thinking about “hacking” by providing something that changed. Your example is not a hack but how it should work, I was just not thinking about it that way.
Thank you
e
before you mentioned button: if you have something with a broken
equals
, you'd absolute be better off fixing that. it could be worked around with
Copy code
val value: T? by remember { mutableStateOf(null, referentialEqualityPolicy()) }
LaunchedEffect(...) {
    flow.collect { value = it }
}
but since you mentioned button: events are not state
k
Events update the state, and by updating your state your UI might or might not get recomposed