I'm in trouble :D I starting to use mvvm and LiveData, and I want to do it in better way. I will try to explain you my doubt with a simple example :
I have a container fragment "Exams" containing a ViewPager2 that contain some fragments "Question".There are also ExamsViewModel and QuestionViewModel.There isn't db and my only data source is MyExamDataSource, something like this:
class MyExamDataSource {
val questions:ArrayList<Question> = ArrayList()
fun saveQuestionResult(){
...
}
fun getQuestion():Question{
...
}
...
...
}
data class Question(val correct:Boolean, val selectedAnswers:ArrayList<Int>)
class ExamViewModel(myExamsDataSource: MyExamsDataSource): ViewModel() {
val correctAnswer:LiveData<Int> = MutableLiveData<Int>(0)
...
...
}
Every QuestionViewModel validate his own question, but in the sametime the correctAnswer LiveData should be incremented every time there are a new correct answer. MyExamData source is passed through the modelfactory to ExamsViewModel and QuestionViewModel.
But now I have some doubt:
- How can I update the correctAnswer LiveData ?
- If I trasform the questions field in a LiveData>, and the ExamsModelView observe it, the onChanged method is not called if I only edit an element of the list or I'm wrong ?
- Is better to make a directly comunication between modelview ?