I have an App that fetches a list of 158 Items from an API, stores it in Room, and displays it to the user. RoomDB is the source of truth.
This is the code on my ViewModel that gets the result from the database:
private val pagingConfig =
PagingConfig(pageSize = 20, enablePlaceholders = false, maxSize = 300)
fun getList(filters: Filters): Flow<PagingData<Character>> {
return Pager(pagingConfig) {
repository.getCharacters(filters)
}.flow.map {
it.asDomainModel()
}
}
This is the code on my fragment that populates the adapter:
private fun fetchData(filters: Filters) {
lifecycleScope.launch {
charactersViewModel.getList(filters).collectLatest { pagedList ->
characterAdapter.submitData(pagedList)
}
}
}
Current behaviour:
What I have already tried:
Since I'm fetching the data asynchronously I've tried using this piece of code from this article to prevent loading the data when adapter is empty. but it didn't work
characterAdapter.stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
What I expect to achieve:
Be able to scroll to the bottom of the list and do configuration changes without loosing the scroll position "without having the need to increase my pageSize as the list gets bigger"
https://github.com/doilio/DC-Characters
Don't return a new instance of Flow<PagingData>
evertime from your getList
method.
Do something like this:
class YourViewModel: ViewModel() {
private mPagingData = Flow<PagingData<Character>>? = null;
fun getList(filters: Filters): Flow<PagingData<Character>> {
if(mPagingData != null) return mPagingData; //This is important
else
mPagingData = Pager(pagingConfig) {
repository.getCharacters(filters)
}.flow.map {
it.asDomainModel()
}
return mPagingData;
}
}
Apart from this, make sure you initialize your adapter in onCreate
of your fragment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With