2

I am trying to implement a search option in my app using firebase and paging 3. to make that happen, I have two functions that return a Query from firestore.

Get a query without filtering:

fun getReports(): Query {
    return firestore.collection("users/$uid/reports")
        .orderBy("localUid", Query.Direction.DESCENDING)
        .limit(20L)
}

Get a filtered query:

fun getReportsQuery(filter: String): Query {
    return firestore.collection("users/$uid/reports")
        .whereEqualTo("plateNumber", filter)
        .orderBy("localUid", Query.Direction.DESCENDING)
        .limit(20L)
}

When I create the flow in the viewmodel I give it the getReports() function:

var dataFlow = Pager(
    PagingConfig(pageSize = PAGE_SIZE)
) {
    val loggedUser = authRepository.getLoggedUser()!!.uid
    FirestorePagingDataSource(
        dataRepository.getReports()
    )
}.flow.cachedIn(viewModelScope)

I observe the flow as such:

private fun setupReportsFlow() {
    lifecycleScope.launch {
        viewModel.dataFlow.collectLatest {
            adapter.submitData(it)
        }
    }
}

My question is, how do I pass it the other function with the query when needed and update the flow with the new data?

Emek Cohen
  • 233
  • 1
  • 3
  • 14
  • I that this article, [How to paginate Firestore using Paging 3 on Android?](https://medium.com/firebase-tips-tricks/how-to-paginate-firestore-using-paging-3-on-android-c485acb0a2df), might help. [Here](https://github.com/alexmamo/FirestorePagination) is the corresponding repo. It's not with Flow, but I think you can get the idea. – Alex Mamo Jun 11 '21 at 10:33
  • 1
    I asked a very similar question here: https://stackoverflow.com/questions/66537608/android-paging-3-how-to-change-parameters-of-remotemediator Maybe this helps you. The most important part here is the use of `pathFlow.flatMapLatest { ... }` – muetzenflo Jun 14 '21 at 20:14
  • Answer from the comments: https://stackoverflow.com/questions/66537608/android-paging-3-how-to-change-parameters-of-remotemediator Works for me. – Emek Cohen Jun 16 '21 at 05:31

0 Answers0