Last active
August 17, 2017 09:52
-
-
Save AkshayChordiya/03062bbf9835ad0041c0ed073f893a97 to your computer and use it in GitHub Desktop.
UserViewModel with LiveData transformations
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 UserViewModel() : ViewModel() { | |
/** | |
* The id of the user | |
*/ | |
private var userId: LiveData<Long> | |
/** | |
* The user | |
*/ | |
private var user: LiveData<User> | |
init { | |
// Get the user when id is changed | |
user = Transformations.switchMap(userId) { id -> getUser(id) } | |
} | |
fun setUserId(id: Long) { | |
userId.value = id | |
} | |
/** | |
* Get the data | |
*/ | |
fun getUser() = user | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment