Question regarding
Flow
collection, `RecyclerView`s and MVVM. For a simple list to be displayed in a
RecyclerView
, the
ViewModel
exposes a
Flow
of
List<Item>
which is collected in the
Fragment
which in turn sets the items on a `RecyclerView`’s adapter (and calls
notifyDataSetChanged()
). I believe this is standard procedure.
However, suppose I want to ‘decorate’ certain items in
List<Item>
(think of making a specific DB call for an item ID to obtain a
Flow
specific to that item).
Where to collect such Flows?
1.
ViewModel
- hide this complexity from the fragment/adapter, so that
List<DecoratedItem>
contains an immutable snapshot of the data with the ‘decoration’ already applied
2.
Fragment
-
Item
could expose a ‘decoration’
Flow
which is collected in the
Fragment
. With this level of granularity, the fragment would be better able to call the relevant
notifyItemChanged()
function
3.
RecyclerView.Adapter
- when binding the item we can start collecting the ‘decoration’ flow for that item. This fits well, because there is no point collecting that when the item is not bound.
My notes:
(1) Seems like the more correct, since it keeps non-UI logic out of the fragment/adapter, but then we have a potentially long list with some update caused by an item that’s long been scrolled past, and applying
DiffUtil
etc
(2) I’m not particularly convinced by this approach, but I’ve included it because it’s where collection already occurs
(3) Seems neat because we are only collecting items that are currently bound
Maybe it depends on how active these ‘decoration’ flows are? Low activity would mean the concerns of (1) are not such a big deal.
Any thoughts, please?