What is the proper way to expose paging from a vie...
# compose
z
What is the proper way to expose paging from a viewModel to a composable?. Ideally I would expose a paging source from the view model and create the pager in the composable, with remember. However this doesn't seem to work (using stateFlow) Since the pager is not calling the load function of the mediator.
c
don’t suppose there’s anyone who has tried to hook in the paging library the android team did, I was impressed by that thing…
a
Nope, we did. there’s compose-paging available. create the pager from the ViewModel, just expose the Flow into your ViewState
2
last version still had some critical bugs however. Check the issuetracker
c
I implementing a few of the screens in Tivi using `paging-compose`: https://github.com/chrisbanes/tivi/tree/main/ui-trending/src/main/java/app/tivi/home/trending
z
Thank you @cb I have been referencing tivi code for a while now, unfortunately I found it very complex, using interactors and various abstraction layers for delivering the data from the repository to the UI. However I did come away with the understanding that I should expose a PagingData flow rather then the paging source. So in my simple implementation, I create the pager flow in the repository using a mediator and exposing the flow of paging data to the UI via the viewModel. I would love it however if there would be some documentation on what are the roles of each component in the tivi code, since it seems very robust and effective, but I found it hard to wrap my head around the various levels of abstraction and the purpose of the interactors. Thanks again for the tivi app code. It is an amazing source of knowledge!.
c
Yep, that sounds right to me. The UI shouldn't have any idea about the underlying implementation, it just need a
Flow<PagingData>
to consume.
👍 1
The interactors stuff is probably a bit unnecessary in Tivi, but I mostly use them to re-use code. You could copy the the code from the interactors, and move them directly into the individual ViewModels. It would work the same.
z
That wouldn't work that well, since your abstractions include various types of entries, holding different types of data with generics and all. I managed to create the pagingData flow in the repository and expose it at the viewModel. However, I am now facing the challenge of syncing the page numbers, I suppose I should get them from the pageState somehow, I got some more digging to do. I appreciate your help very much.