Last active
May 11, 2022 07:53
-
-
Save ivanalvarado/726a6c3f5ffad54958fe4670269bd897 to your computer and use it in GitHub Desktop.
Correct implementation of Swipe-to-Refresh with LiveData.
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
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You just saved my day 🙏🏽