Skip to content

Instantly share code, notes, and snippets.

@AkshayChordiya
Last active August 17, 2017 09:52
Show Gist options
  • Save AkshayChordiya/03062bbf9835ad0041c0ed073f893a97 to your computer and use it in GitHub Desktop.
Save AkshayChordiya/03062bbf9835ad0041c0ed073f893a97 to your computer and use it in GitHub Desktop.
UserViewModel with LiveData transformations
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