I am new to MVVVM and retrofit i have successfully implemented MVVM and is able to move data from retrofit to repository then Repository to ViewModel and view.
While doing this i came up to a massive confusion which is mentioned below. In first scenario my code looks like this:
Repository:
fun iniateOTPprocess() : LiveData<GenericResponse> {
RetroUtils.getApiManager().listRepos().enqueue(object : RetrofitCallback() {
override fun onResponse(call: Call<GenericResponse>, response: Response<GenericResponse>) {
super.onResponse(call, response)
result.value = response.body()
}
}
)
return result
}
ViewModel:
class LoginViewModel2(application: Application) : AndroidViewModel(application) {
lateinit var username: MutableLiveData<String>
lateinit var password: MutableLiveData<String>
var repository: LoginRepository = LoginRepository(application)
var data = MediatorLiveData<GenericResponse>()
var result = MutableLiveData<GenericResponse>()
init {
data.addSource(result , Observer {
data.postValue(it)
})
}
fun onLoginBtnCLicked() {
initiateOTP()
}
private fun initiateOTP() {
result = repository.iniateOTPprocess()
}
fun getResponse() : MediatorLiveData<GenericResponse>{
return data
}
}
The Mediator live data is never updated this way once the data is updated.
But if i change this code to
Repository
class LoginRepository(var application: Application) {
var callback: RetrofitCallback = RetrofitCallback()
var result = MutableLiveData<GenericResponse>()
fun iniateOTPprocess() {
RetroUtils.getApiManager().listRepos().enqueue(object : RetrofitCallback() {
override fun onResponse(call: Call<GenericResponse>, response: Response<GenericResponse>) {
super.onResponse(call, response)
result.value = response.body()
}
}
)
}
fun getData(): MutableLiveData<GenericResponse> {
return result
}
}
ViewModel
class LoginViewModel2(application: Application) : AndroidViewModel(application) {
lateinit var username: MutableLiveData<String>
lateinit var password: MutableLiveData<String>
var repository: LoginRepository = LoginRepository(application)
var data = MediatorLiveData<GenericResponse>()
var result = MutableLiveData<GenericResponse>()
init {
data.addSource(repository.getData(), Observer {
data.postValue(it)
})
}
fun onLoginBtnCLicked() {
initiateOTP()
}
private fun initiateOTP() {
repository.iniateOTPprocess()
}
fun getResponse() : MediatorLiveData<GenericResponse>{
return data
}
}
This code starts working magically. In the view i am observing getResponse() method in both the scenarios. Can anyone here help with the confusion and can explain where the magic is happening. Thanks in advance.