square / cycler

Apache License 2.0
792 stars 27 forks source link

Why not use ListAdapter? #10

Open emmano opened 4 years ago

emmano commented 4 years ago

Great library. I was just wondering why RecylcerView.Adapter and DiffUtil were used instead of ListAdapter and DiffUtil.ItemCallback? Was it to leverage coroutines for diffing operations?

helios175 commented 4 years ago

Hi, thanks!

This started as a small effort inside Square to provide a way to build our recycler views. To be honest, I didn't know of the ListAdapter at that point. When I introduced diffing, the question about how to do "async" came up, and coroutines were the answer. So yes, ListAdapter wouldn't have worked in that setting.

Nevertheless there's a side-effect which I think is positive: we are in control on how the update works. We use the same diffing algoritm (DiffUtil) but we decide what to do with the results. If you check the Recycler and Update classes you'll see how a second update invoked while the first updated is being diffed, works just fine. When a diffing (non synchronous list of "notifiy" updates) ends the "original data" gets compared to know if this diff is still up-to-date or was superseded by a different update.

Also there's the Update.addChunk feature, which is handled differently. Maybe it can still be done with ListAdapter (I didn't check) but having complete control doesn't hurt.

Otherwise –control and coroutines– there's no special reason it didn't use ListAdapter.

PS: in Square we are working with declarative UI, and in that context, a recycler component, would receive "added chunks" or new data pages, as a completely new data source. We still want to avoid a big diff in such cases, so we will need to come out with a way to provide new chunks of data to a declarative-recycler-component. Some mechanism for it to know that "oh, this is a different data source, but it's the old one plus some extra data at the end". So there might be improvements in the update area of the Recycler, where maybe the "notifications lambda generation" gets moved out from Update.

emmano commented 4 years ago

I see. Thanks a lot for the detailed response!

helios175 commented 4 years ago

No problem. I'll look more closely into ListAdapter as it might be nice to rely on the standard library. I just need to know how it relates to asynchronous execution, and if I can do the same stuff as here:

helios175 commented 4 years ago

Reopening to follow up on ListAdapter suitability.