Reputation: 67
this is my viewModel code:
val workout = state.get<Workout>("workout")
var steps : List<Step> = emptyList()
fun getSteps() = viewModelScope.launch {
steps = workout?.let { stepDao.getWorkoutSteps(it.workoutId) }!!
Log.e("KEK", steps.size.toString())
}
and the fragment:
viewModel.getSteps()
if (viewModel.steps.isNotEmpty()) {
binding.apply {
currentStepName.text = viewModel.steps[0].name
currentStepDuration.text = viewModel.steps[0].length.toString()
nextStep.text = "${viewModel.steps[1].name} : ${viewModel.steps[1].length.toString()}"
}
}
this part where i fill the nextStep text doesn't work, a second item in the list exists, but the text doesn't get shown `
thank you
Upvotes: 0
Views: 43
Reputation: 8335
When you call viewModel.getSteps()
it starts a coroutine, so you need to wait for it to finish. but you don't wait and try to update the UI using viewModel.steps
which may not have been updated yet, because coroutine may not have started.
in your case you can use a LiveData
object to fix the issue.
In your ViewModel
val liveSteps: MutableLiveData<List<Step>> = MutableLiveData()
fun getSteps() = viewModelScope.launch {
steps = workout?.let { stepDao.getWorkoutSteps(it.workoutId) }!!
liveSteps.postValue(steps)
Log.e("KEK", steps.size.toString())
}
Now observe this LiveData
in Fragment and update UI
viewModel.liveSteps.observe(viewLifeCycleOwner, androidx.lifecycle.observe{
if(!(it.isNullOrEmpty()) && it.size == 2){
binding.apply {
currentStepName.text = it[0].name
currentStepDuration.text = it[0].length.toString()
nextStep.text = "${it[1].name} : ${it[1].length.toString()}"
}
}
})
Upvotes: 1