Skip to content

Instantly share code, notes, and snippets.

@ivanalvarado
Created June 10, 2019 23:28
Show Gist options
  • Save ivanalvarado/ac37fe6213fd6b073b9ec38576cdaeed to your computer and use it in GitHub Desktop.
Save ivanalvarado/ac37fe6213fd6b073b9ec38576cdaeed to your computer and use it in GitHub Desktop.
Passing arguments to ViewModel with AssistedInject.
class UserDetailViewModel @AssistedInject constructor(
private val userRepository: UserRepository,
@Assisted private val userId: Int
) : ViewModel() {
private val reloadTrigger = MutableLiveData<Boolean>()
private val userDetail: LiveData<UserDetailModel> = Transformations.switchMap(reloadTrigger) {
userRepository.getUserDetail(userId.toString(), reloadTrigger.value!!)
}
init {
refreshUserDetail()
}
fun getUserDetail(): LiveData<UserDetailModel> = userDetail
fun refreshUserDetail(forceRefresh: Boolean = false) {
reloadTrigger.value = forceRefresh
}
@AssistedInject.Factory
interface Factory {
fun create(userId: Int): UserDetailViewModel
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment