I am using PagingSource. From the official doc here is an example of paging source class implementation.
class ExamplePagingSource(
val backend: ExampleBackendService,
val query: String
) : PagingSource<Int, User>() {
........
........
In ViewModel i have setup flow of paging data this way
val flow = Pager(
PagingConfig(pageSize = 20)
) {
ExamplePagingSource(backend, query)
}.flow
.cachedIn(viewModelScope)
Now if I want Hilt to provide ExamplePagingSource dependency how can I do that?
ExamplePagingSource took query in the constructor which changes every search action, I always have to provide a new instance of ExamplePagingSource.
One way could be to initialize ExamplePagingSource with ExampleBackendService and set query by setter every time. But this will not work if I call PagingDataAdapter.refresh() for swipe refresh action and it throws exception
java.lang.IllegalStateException: An instance of PagingSource was re-used when Pager expected to create a new instance.Ensure that the pagingSourceFactory passed to Pager always returns a new instance of PagingSource.
So how can I config Hilt so that Hilt can provide ExamplePagingSource as a dependency
You can make use of dagger's assisted injection. The
querywill be@Assistedand you will have to pass it tocreatefunction, butExampleBackendServiceand everything else will be provided by dagger.