Created
June 10, 2019 23:28
-
-
Save ivanalvarado/ac37fe6213fd6b073b9ec38576cdaeed to your computer and use it in GitHub Desktop.
Passing arguments to ViewModel with AssistedInject.
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 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