Byte 5 – Transformations
Working closer with MVVM, I got to the point where I got to use Transformations.
1 2 3 4 5 |
val phoneNumber = MutableLiveData<string>() val isPhoneNumberValid: LiveData = Transformations.map(phoneNumber) { phone -> phone.isPhoneNumberValid() } </string> |
What this basically does is:
Whenever the phoneNumber variable changed, the function from the Transformation.map is triggered and isPhoneNumberValid changes.
Basically if isPhoneNumberValid
is observed (and only if it’s observed), it will change to true or false based on the isPhoneNumberValid function without requiring any other extra steps.
This is particulary usefull in cases where the phoneNumber might be changed from multipel sources like:
- user input
- local data source getting queried
- remote dat source getting downloaded
- any other function that would maybe format the phone number.
More info about it here.
By: Adrian Coman