In Paging3 I encountered the problem that it is necessary to create a new instance of PagingSource each time. I cannot understand how to implement this properly. I found information on Google that you can create an instance directly in pagingSourceFactory, but the problem with this method is that I will not be able to do invalidate() later to reload the page. What solutions are there to this problem?
class SearchDialogComponentImpl(
componentContext: ComponentContext,
componentFactory: ComponentFactory,
private val searchService: SearchService,
) : SearchDialogComponent, BaseComponent<SearchDialogState>(componentContext, SearchDialogState()),
KoinComponent {
private val searchProductRepositoryPagingSource: SearchProductRepositoryPagingSource by inject {
parametersOf(
viewState.searchTextField
)
}
override val products: Flow<PagingData<Product>> = Pager(
initialKey = BuildKonfig.PAGING_INITIAL_PAGE,
config = PagingConfig(
pageSize = BuildKonfig.PAGING_OFFSET,
initialLoadSize = BuildKonfig.PAGING_OFFSET,
),
pagingSourceFactory = { searchProductRepositoryPagingSource }
).flow
override fun onSearchTextFieldValueChanged(value: String) {
viewState = viewState.copy(searchTextField = value)
searchTextFieldValueChanged(value)
if (viewState.searchedProduct) {
searchJob?.cancel()
if (viewState.searchTextField.isNotEmpty()) {
searchJob = scope.launch(Dispatchers.IO) {
delay(500)
searchProductRepositoryPagingSource.invalidate()
}
} else {
viewState =
viewState.copy(productsLoadingState = LoadingState.Empty(EmptyType.EmptyTextField))
}
}
}
}
Tried to do it through InvalidatingPagingSourceFactory
Егор Логинов is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.