0

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)
        }
    }
Puntogris
  • 325
  • 2
  • 13
  • Possibly you are getting a exception when the filter result is empty, because of this line: currentPage.documents[currentPage.size() - 1] – Diniz Jun 27 '21 at 22:29
  • I'm not sure I understand. What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? Please respond with @AlexMamo – Alex Mamo Jun 28 '21 at 15:51
  • I'm using paging 3 with Room and Firestore to populate a RecyclerView, in the repo i return the PagingData, using Room if there is data in the Rv and i query again but the query returns empy because no data match the new query, the Rv clears itself out of the box, but this doesn't happen when the data comes from Firestore, i asume that i'm doing something wrong in `FirestoreEventsPagingSource` as @Diniz told me @AlexMamo – Puntogris Jun 28 '21 at 16:16

0 Answers0