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

nglauber

07/13/2021, 12:17 AM
Having in mind a form with 10 fields (for instance) of different types (text, radio button, checkbox, combobox, … ). What do you folks think is the best approach? 1️⃣ Expose one `Flow`/`LiveData` for each field in ViewModel and collect/observe them individually in the screen. 2️⃣ Expose one
State
for each field in ViewModel and simply access them from the screen. 3️⃣ Encapsulate these 10 fields in a data class, expose one single `Flow`/`LiveData` and each “setter” create a
copy
of this data class instance to update the state. 4️⃣ Encapsulate these 10 fields in a data class, each field is a state, ViewModel has an instance of this class and you simply access it from the screen. 5️⃣ Other (please explain)
2️⃣ 1
5️⃣ 2
3️⃣ 6
I’m currently using 3️⃣ because I don’t want my viewmodel depends on Compose classes (State).
c

Colton Idle

07/13/2021, 1:53 AM
A single state class with multiple mutable state ofs
💯 3
t

tad

07/13/2021, 2:09 AM
Been migrating from Flow to snapshot state (^), this is the way to go for sure
o

Oleg Khotskin

07/13/2021, 3:30 AM
Flow/StateFlow are not great as observable properties, their asynchrony can lead to unexpected behavior. See https://github.com/Kotlin/kotlinx.coroutines/issues/2280 for more information.
g

gildor

07/13/2021, 5:57 AM
Flow/StateFlow are not great as observable properties
It’s completely fine behaviour for observable property, otherwise you will get all those “unexpected behavior” not when you do this syncronously, but when you do this asyncronously