In my ViewModel I have the code shown below:
private val _sender = MutableLiveData<DialogListItem.ParticipantItem>()
val sender: LiveData<DialogListItem.ParticipantItem>
get() = _sender
private val _receiverListItems: MutableLiveData<List<DialogListItem>> =
CombinedLiveData<List<ParticipantTypeA>, List<ParticipantTypeB>, List<DialogListItem>>(
_participantsA, _participantsB
) { participantsA, participantsB ->
val sender = _sender.value
...
...
return@CombinedLiveData ...
}
val receiverListItems: LiveData<List<DialogListItem>>
get() = _receiverListItems
When a new value is posted by doing _sender.postValue(selectedSender)
, _receiverListItems
should be notified of that change. However, when I log the value of the _sender
, I get null
. How can I get _receiverListItems
value dependent on _sender
value?
Since you are using a custom implementation of LiveData, this answer might be limited in what it says due to that but in general, you want Transformation.switchMap
.
private val _receiverListItems: LiveData<List<DialogListItem>> =
Transformation.switchMap(sender).map { sender ->
CombinedLiveData<
List<ParticipantTypeA>, List<ParticipantTypeB>, List<DialogListItem>
>(
_participantsA,
_participantsB
) { participantsA, participantsB ->
...
...
return@CombinedLiveData ...
}
}