With databinding, how can I best structure data to localize UI redraw corresponding to just one small aspect of a larger LiveData?
My UI is a fragment representing a Workout with a collection of TableLayout
s, each corresponding to a Group. The TableLayout
s each have a collection of TableRow
s corresponding to the Lifts' Sets. The user can add Lifts to a Group (UI must reflect this) and also can add Sets to a Lift (UI must reflect this).
Right now this is all fetched via one Room query returning LiveData (which is a hierarchy of Workout->Groups->Lifts->Sets).
Problem is right now if you change one property of a set, it triggers the observer for the whole LiveData. I could write something that checks for uniqueness before notifying that the data has changed, but that would take some complicated deep equality stuff.
Alternatively, I could fetch LiveData and then for each group fetch LiveData (being a hierarchy of Lifts with Sets). Then changing a set would trigger a Lift change and redraw that part of the UI, but not the entire UI.
Still, if you do this, say, putting text into a set (which triggers a DB update) would redraw the fragment with the set in it, and focus goes away from the right thing.
Ultimately, how would you structure a large hierarchy of data, all of it shown on screen at once, to minimize UI redraws triggered via LiveData changes + data binding?
Discussion in the comments turned out to resolve the question. Posting here for easier access.
Answer
If you use a RecyclerView to display your data, then you can use a ListAdapter paired with a DiffUtil.Callback to only update rows which change. When the entire LiveData is updated, the ListAdapter will only update the rows that have changed, according to your DiffUtil.Callback.
As for your exact question, I'm not sure yet they best approach besides splitting the large structure into smaller LiveData pieces.