I just can\'t see how to chain LiveData from Repo to VM, so I have tried to boil this down to the most simple of example!:
Fragment
Actually If you are maintaining LiveData
in repository, I don't think there's a need of separate LiveData
in ViewModel
as well. You just have to observe the LiveData once from the activity. Then make any changes to repository instance directly. So If I have to show it in your code, It might look something like this.
Activity class: Change makeToast method to observeCurrentName() like this:
private fun observeCurrentName() {
vm.getCurrentName().observe(this, Observer{
//Toast here
})
}
Your VM:
class LoginViewModel : ViewModel() {
...
fun getCurrentName(): MutableLiveData{
return repository.getCurrentName()
}
fun setCurrentName(name: String?){
repository.setCurrentName(name)
}
...
}
Your repository:
class FirestoreRepository {
private val mCurrentName = MutableLiveData()
fun getCurrentName(): MutableLiveData{
return mCurrentName
}
fun setCurrentName(name: String?){
mCurrentName.value = name //This will trigger observer in Activity
}
}