https://kotlinlang.org logo
#compose-android
Title
# compose-android
u

undermark5

02/27/2024, 10:40 PM
How are you all handling optimistic UI?
We've got a scenario where we'd like to ensure immediate UI updates after user interaction. These interactions ultimately will result in some network calls for validation. However, it may be possible to allow the user to interact multiple times within a short time frame, meaning that if the user interacts twice before the first network call get's back, a naive approach for optimistic UI would appear to be "rolled back" before then having the second interaction committed. In theory, debouncing the UI state would prevent this in most circumstances, but that feels wrong.
s

Stylianos Gakis

02/27/2024, 10:55 PM
Feels like not a concern for your UI layer to solve per say, but how your business logic handles such interactions and updates your UiState. Depending on the scenario there’s different things you can do. Loading states being the easiest approach generally, but it really depends.
u

undermark5

02/27/2024, 11:15 PM
I mean, I kinda feel like loading states for interactions (think like likes/upvotes on social media) kinda go against optimistic UI, and aren't necessarily within the vision for the product. Obviously if it's impossible to manage without them, then the product people will just have to deal with a compromise.
s

Stylianos Gakis

02/27/2024, 11:22 PM
Oh yeah, wouldn’t suggest loading state for a like. But in the end the decision of what happens when that request does not actually go through is not for your compose UI to handle. Hence I think this does not really have much to do with compose itself, so it might be a question for another channel. What do you think? Would you do this any differently in an app using Views?
u

undermark5

02/27/2024, 11:44 PM
I mean, I suppose it would the same regardless of the UI framework. That being said, I'm still curious how others are handling optimistic UI, if it's simply handling outside of the view layer itself then that's fine.
👍 1
s

Stylianos Gakis

02/28/2024, 7:50 AM
Compose really works best when you have a single source of truth. So "handling outside of the view layer" in my opinion is the only sensible approach in the first place 😊
a

Abinet Zelalem

02/28/2024, 11:04 PM
You can maintain the state within the composable without hitting any network until another interaction happens on another element.
s

Stylianos Gakis

02/28/2024, 11:11 PM
You can, then you got two sources of truth, and one of them is completely ephemeral and will be lost quite easily
a

Abinet Zelalem

02/29/2024, 1:41 AM
That should be ok I think. A simple form that changes it’s state based on interaction can retain 2 different states. One intentionally ephemeral state to handle information about the UI and another that you can use to hit your backend/services. This second state can rely on the first.
7 Views