Skip to content

Instantly share code, notes, and snippets.

@ivanalvarado
Last active May 11, 2022 07:53
Show Gist options
  • Save ivanalvarado/726a6c3f5ffad54958fe4670269bd897 to your computer and use it in GitHub Desktop.
Save ivanalvarado/726a6c3f5ffad54958fe4670269bd897 to your computer and use it in GitHub Desktop.
Correct implementation of Swipe-to-Refresh with LiveData.
class UserListViewModel @Inject constructor(private val userRepository: UserRepository) : ViewModel() {
private val reloadTrigger = MutableLiveData<Boolean>()
private val users: LiveData<List<UserModel>> = Transformations.switchMap(reloadTrigger) {
userRepository.getUsers()
}
init {
refreshUsers()
}
fun getUsers(): LiveData<List<UserModel>> = users
fun refreshUsers() {
reloadTrigger.value = true
}
}
@norrisboat
Copy link

You just saved my day 🙏🏽

@k-antonov
Copy link

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment