@@ -13,7 +13,7 @@ import androidx.lifecycle.ViewModel
1313import androidx.lifecycle.viewModelScope
1414import androidx.navigation.toRoute
1515import dagger.hilt.android.lifecycle.HiltViewModel
16- import kotlinx.coroutines.Dispatchers
16+ import kotlinx.coroutines.CoroutineDispatcher
1717import kotlinx.coroutines.flow.MutableStateFlow
1818import kotlinx.coroutines.flow.SharingStarted
1919import kotlinx.coroutines.flow.StateFlow
@@ -25,6 +25,7 @@ import kotlinx.coroutines.flow.update
2525import kotlinx.coroutines.launch
2626import org.librefit.db.relations.WorkoutWithExercisesAndSets
2727import org.librefit.db.repository.WorkoutRepository
28+ import org.librefit.di.qualifiers.IoDispatcher
2829import org.librefit.enums.WorkoutState
2930import org.librefit.enums.chart.WorkoutChart
3031import org.librefit.helpers.DataHelper
@@ -43,7 +44,8 @@ import kotlin.random.Random
4344class InfoWorkoutScreenViewModel @Inject constructor(
4445 savedStateHandle : SavedStateHandle ,
4546 private val workoutRepository : WorkoutRepository ,
46- dataHelper : DataHelper
47+ dataHelper : DataHelper ,
48+ @param:IoDispatcher private val ioDispatcher : CoroutineDispatcher
4749) : ViewModel() {
4850
4951 private val workoutId = savedStateHandle.toRoute<Route .InfoWorkoutScreen >().workoutId
@@ -57,12 +59,14 @@ class InfoWorkoutScreenViewModel @Inject constructor(
5759 init {
5860 require(workoutId != 0L ) { " workoutId must be not equal to 0" }
5961
60- viewModelScope.launch(Dispatchers . IO ) {
62+ viewModelScope.launch(ioDispatcher ) {
6163 val workoutWithExercisesAndSets =
6264 workoutRepository.getWorkoutWithExercisesAndSets(workoutId)
6365
66+ val workout = workoutWithExercisesAndSets.workout
67+
6468 _workout .update {
65- workoutWithExercisesAndSets. workout
69+ workout
6670 }
6771
6872 _exercises .update {
@@ -71,25 +75,25 @@ class InfoWorkoutScreenViewModel @Inject constructor(
7175
7276 if (isRoutine()) {
7377 _routine .update {
74- workout.value
78+ workout
7579 }
7680
7781 // If the workout is a routine, then retrieve all past completed workout associated with it
7882 _completedWorkoutsWithExercises .update {
7983 workoutRepository
80- .getCompletedWorkoutsWithExercisesAndSetsFromRoutine(routineId = workout.value. routineId)
84+ .getCompletedWorkoutsWithExercisesAndSetsFromRoutine(routineId = workout.routineId)
8185 }
8286 } else {
8387 _routine .update {
84- workoutRepository.getRoutineFromRoutineID(workout.value. routineId).toUi()
88+ workoutRepository.getRoutineFromRoutineID(workout.routineId).toUi()
8589 }
8690 }
8791
8892
8993 // Calculate volume
9094 val volumeValue = dataHelper.fetchVolumeFromWorkout(
9195 WorkoutWithExercisesAndSets (
92- workout.value. toEntity(),
96+ workout.toEntity(),
9397 exercises.value.map { it.toEntity() })
9498 )
9599
@@ -109,7 +113,7 @@ class InfoWorkoutScreenViewModel @Inject constructor(
109113 }
110114
111115 fun deleteWorkout () {
112- viewModelScope.launch(Dispatchers . IO ) {
116+ viewModelScope.launch(ioDispatcher ) {
113117 workoutRepository.deleteWorkout(workout.value.toEntity())
114118 }
115119 }
@@ -125,7 +129,7 @@ class InfoWorkoutScreenViewModel @Inject constructor(
125129 UiWorkout ()
126130 }
127131
128- viewModelScope.launch(Dispatchers . IO ) {
132+ viewModelScope.launch(ioDispatcher ) {
129133 workoutRepository.updateWorkout(workout.value.toEntity())
130134 }
131135 }
0 commit comments