So i have a RecyclerView and i fetch all the data, then if a user clicks on some buttons it triggers the same function but with a different filter.
The issue i'm having is it won't clear the old data when the new query returns empty. So if it load all the data and they i click on a filter that has no data, it won't clear the data that is the screen.
I tested it and when i select a filter that has data it will clear the old data and show the new one.
I tried this with room and it work as inteded out of the box but i don't know how to make this with Firestore.
Any ideas on how can i fix this?
This is my repo, here it checks if it has to fetch the data from Firestore or Room.
override suspend fun getEventPagingDataFlow(filter: String): Flow<PagingData<Event>> = withContext(Dispatchers.IO){
Pager(
PagingConfig(
pageSize = 30,
enablePlaceholders = true,
maxSize = 200)
) {
if(currentBusinessType() == "ONLINE"){
val query = firestoreQueries.getEventsCollectionQuery()
if (filter == "ALL") FirestoreEventsPagingSource(query)
else FirestoreEventsPagingSource(query.whereEqualTo("status", filter))
}
else{
if (filter == "ALL") eventsDao.getAllPaged()
else eventsDao.getPagedEventsWithFilter(filter)
}
}.flow
}
This is the Firestore paging source
class FirestoreEventsPagingSource(
private val query: Query
) : PagingSource<QuerySnapshot, Event>() {
override suspend fun load(params: LoadParams<QuerySnapshot>): LoadResult<QuerySnapshot, Event> {
return try {
val currentPage = params.key ?: query
.get()
.await()
val lastDocumentSnapshot = currentPage.documents[currentPage.size() - 1]
val nextPage = query.startAfter(lastDocumentSnapshot)
.get()
.await()
LoadResult.Page(
data = currentPage.toObjects(Event::class.java),
prevKey = null,
nextKey = nextPage
)
} catch (e: Exception) {
LoadResult.Error(e)
}
}
override fun getRefreshKey(state: PagingState<QuerySnapshot, Event>): QuerySnapshot? {
return null
}
}
Here is where i update the adapter:
lifecycleScope.launchWhenStarted {
viewModel.events.collectLatest {
calendarEventsAdapter.submitData(it)
}
}