Last active
June 10, 2019 23:18
-
-
Save ivanalvarado/e5484d97187ca31ab268f743cceb7a2b to your computer and use it in GitHub Desktop.
Pass argument through public setter method
This file contains hidden or 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 @Inject constructor( | |
private val userRepository: UserRepository | |
) : ViewModel() { | |
private lateinit var userId: String | |
private val reloadTrigger = MutableLiveData<Boolean>() | |
private val userDetail: LiveData<UserDetailModel> = Transformations.switchMap(reloadTrigger) { | |
userRepository.getUserDetail(userId, reloadTrigger.value!!) | |
} | |
// Passing our argument through a public setter method | |
fun setUserId(userId: String) { | |
this.userId = userId | |
refreshUserDetail() | |
} | |
fun getUserDetail(): LiveData<UserDetailModel> = userDetail | |
fun refreshUserDetail(forceRefresh: Boolean = false) { | |
reloadTrigger.value = forceRefresh | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment