flatMapBySource

fun <T, S> LiveData<out Collection<S>>.flatMapBySource(mapFunction: (S) -> LiveData<out Collection<T>>): LiveData<Collection<Pair<S, T>>>

Transforms a LiveData containing a list of type T, which contains all the child elements from each element in thisLiveData list together with its source, by flattening it.

For example:

// GIVEN
val source1 = Holder(1, 2, 3)
val source2 = Holder(4, 5)
val source = MutableLiveData<List<Holder>>(listOf(source1, source2))

// WHEN
val flatMapped = sources.flatMapBySource { holder -> holder.contents }

// THEN
// Observing flatMapped.value yields:
listOf(
Pair(source1, 1),
Pair(source1, 2),
Pair(source1, 3),
Pair(source2, 4),
Pair(source2, 5)
)

Whenever either this or one of the lists inside of this changes, the returned LiveData value is also updated.

Parameters

T

The type of the list elements to flatten.

S

The type of the source list elements, containing lists of type T.

mapFunction

A function that maps source lists to lists of type T.