architecture-components-samples icon indicating copy to clipboard operation
architecture-components-samples copied to clipboard

How to pass multiple parameters to the server when using paging and flow components

Open smileusers opened this issue 4 years ago • 0 comments

The requirements are as follows: when obtaining the product list, you need to pass multiple parameters, for example, a query parameter and an order sorting parameter, but you don't know how to change them based on the pagingwithnetworksample project. The code is as follows:

ProductViewModel.kt

private val query = MutableLiveData<String>()
private val order = MutableLiveData<Int>(0)

fun setQuery(data: String?) {
    clearListChannel.trySend(Unit)

    query.value = data
}

fun setOrder(data: Int) {
    clearListChannel.trySend(Unit)

    order.value = data
}

init {
    query.value = null
}

val products = flowOf(
    clearListChannel.receiveAsFlow().map { PagingData.empty<Product>() },

  //Here is how to listen to the query and order fields and call the repository. Products method to query data as long as one field changes,Now just listen to the query field
    query.asFlow()
    .flatMapLatest {
        repository.products(order=0, query=it,pageSize = 10)
    }
    .cachedIn(viewModelScope)
).flattenMerge(2)

Because I'm not familiar with kotlin, flow and livedata, I don't know how to implement this function. I'm sorry to ask my friends for help; At the same time, how can the above functions be implemented in Java using the flowable implementation of rxjava

smileusers avatar Oct 04 '21 05:10 smileusers