Last active
April 18, 2021 14:14
-
-
Save DanielKnauf/3f638393f5df5a89189dcb66fea059af to your computer and use it in GitHub Desktop.
MergerLiveData use case for medium article
This file contains 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
/* | |
* before | |
*/ | |
class ViewModel { | |
val nameInput = MutableLiveData("") | |
val ageInput = MutableLiveData(0) | |
} | |
<layout> | |
<data> | |
<variable | |
name="viewModel" | |
type="ViewModel" /> | |
<import type="android.text.TextUtils" /> | |
</data> | |
<ViewGroup .. > | |
.. | |
<Button | |
.. | |
android:enabled="@{!TextUtils.isEmpty(viewModel.nameInput) && viewModel.ageInput >= 18}" | |
/> | |
</ViewGroup> | |
</layout> | |
/* | |
* after | |
*/ | |
class ViewModel { | |
val nameInput = MutableLiveData("") | |
val ageInput = MutableLiveData(0) | |
val isButtonEnabled = | |
MergerLiveData.Two( | |
firstSource = nameInput, | |
secondSource = ageInput | |
) { name, age -> | |
name.isNotEmpty() && age >= 18 | |
} | |
} | |
<layout> | |
<data> | |
<variable | |
name="viewModel" | |
type="ViewModel" /> | |
</data> | |
<ViewGroup .. > | |
.. | |
<Button | |
.. | |
android:enabled="@{viewModel.isButtonEnabled}" | |
/> | |
</ViewGroup> | |
</layout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment