Created
May 14, 2020 14:19
-
-
Save addeeandra/e632f5c9baea251c2e24c71bf7072881 to your computer and use it in GitHub Desktop.
Combined Transformation of LiveData(s) using MediatorLiveData
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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