nalexn / clean-architecture-swiftui

SwiftUI sample app using Clean Architecture. Examples of working with CoreData persistence, networking, dependency injection, unit testing, and more.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: infinite scroll

kmenager opened this issue · comments

I found your repository and architecture like Redux is quite new for me.
I wonder how to use your architecture with infinite scroll.
Imagine your application with only network call. How will you handle infinite scroll with your architecture? Where do you store the current list of country + the fresh new one from the server? How would you use your loadable class in such architecture for this kind of use case?
Thanks

Good question. You can obviously introduce another enum similar to Loadable that would better suit your needs, but I can see how this problem can be solved with the original enum.
We can include the pagination info in the generic parameter for Loadable, that is if we had Loadable<[Feed]>, this can be Loadable<([Feed], PageInfo)>.

Use case 1: Opening the screen for the first time
The enum value is notRequested, you query the first page

Use case 2: First page is loaded, scrolling to the bottom
As you identify that the scroll view is approaching the bottom end of the list, get the pagination info from the enum and query the next page.

In this case, the enum value would change to isLoading, which retains already loaded records, so you can keep displaying it while the next page is loading. Moreover, the value isLoading can be a flag for showing the loading indicator at the bottom of the list.

The loaded data just gets appended to the existing array.

Use Case 3: Refreshing the most recent records
Same as for loading the next page at the bottom with one exception: we need to maintain the previous pagination info and not overwrite it with the info returned from the refreshing of the first page.

This is how you'd do this with Loadable, but you may just consider using the persistence layer for storing the records, while pagination info can remain in the Loadable.

Thanks for your feedback 👌