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

Billy Newman

02/01/2023, 9:26 PM
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

Pablichjenkov

02/01/2023, 9:26 PM
Google Compose state update strategy policy
There are a couple of strategies to check for equality
b

Billy Newman

02/01/2023, 9:28 PM
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

Kevin Del Castillo

02/01/2023, 9:30 PM
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

Pablichjenkov

02/01/2023, 9:33 PM
It could be the case of a container state where 'equal()' is not fully well implemented.
b

Billy Newman

02/01/2023, 9:34 PM
@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

Kevin Del Castillo

02/01/2023, 9:36 PM
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

Billy Newman

02/01/2023, 9:37 PM
@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

ephemient

02/01/2023, 9:38 PM
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

Kevin Del Castillo

02/01/2023, 9:39 PM
Events update the state, and by updating your state your UI might or might not get recomposed
3 Views