Skip to content

Instantly share code, notes, and snippets.

@addeeandra
Created May 14, 2020 14:19
Show Gist options
  • Save addeeandra/e632f5c9baea251c2e24c71bf7072881 to your computer and use it in GitHub Desktop.
Save addeeandra/e632f5c9baea251c2e24c71bf7072881 to your computer and use it in GitHub Desktop.
Combined Transformation of LiveData(s) using MediatorLiveData
class CombinedTransformation<T>(
vararg liveData: LiveData<*>,
private val onAllChanged: (data: List<Any?>) -> T
) : MediatorLiveData<T>() {
private val mLiveDataList: MutableList<Any?> = MutableList(liveData.size) { null }
init {
liveData.forEachIndexed { index, liveDatum ->
super.addSource(liveDatum) { datum ->
mLiveDataList[index] = datum
value = onAllChanged(mLiveDataList)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment