In the following variables, how do I dynamically pass user.id and friend.id
class WindViewModel @Inject constructor() : BaseViewModel() {
val userWindList = Pager(config = pagingConfig, remoteMediator = WindRemoteMediator("userWindList", user.id, friend.id, database!!, api)) {
windRepository.pagingModelList(friend.id, "userWindList")
}.flow.map { pagingData ->
pagingData.map { it.json.toWind() }
}.cachedIn(viewModelScope)
}
CodePudding user response:
I am assuming you specifically mean how to base a Flow on dynamically passed-in values.
I have not used Paging, so I'm not 100% sure this is correct. Specifically, I don't know if is OK to swap to a different Pager source in the same Flow.
But assuming it is OK, one way is to use a MutableSharedFlow as a base for the flow, and use flatMapLatest
on it. You can dynamically change the parameters that your Flow is based on by emitting to the MutableSharedFlow.
data class WindRemoteMediatorParams(val userId: String, val friendId: String) // helper class
private val mediatorParams = MutableSharedFlow<WindRemoteMediatorParams>(replay = 1)
@OptIn(ExperimentalCoroutinesApi::class)
val userWindList = mediatorParams.flatMapLatest { (userId, friendId) ->
Pager(config = pagingConfig, remoteMediator = WindRemoteMediator("userWindList", userId, friendId, database!!, api)) {
windRepository.pagingModelList(friend.id, "userWindList")
}.flow
}.map { pagingData ->
pagingData.map { it.json.toWind() }
}.cachedIn(viewModelScope)
fun beginPaging(userId: String, friendId: String) {
mediatorParams.tryEmit(WindRemoteMediatorParams(userId, friendId))
}
CodePudding user response:
The way to pass arguments dynamically on a Kotlin
function is like this:
...
val sumOfSomeInts = sumOfSomeInts(1, 3, 7, 2)
...
fun sumOfSomeInts(vararg num: Int): Int {
var sum = 0
...
return sum
}
...
However, I would suggest you leave vararg
parameters on the front of the parameters list, or at least you use named arguments
like in this answer