Sunday, 13 June 2021

Give a new query to PagingDataSource

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?



from Give a new query to PagingDataSource

No comments:

Post a Comment