Skip to content

Commit 72a9e84

Browse files
authored
fix: null pointer exception stateflow info workout viewmodel (#71)
* refactor: use local workout variable * refactor: use injected dispatchers * docs: update changelog
1 parent 425cbc2 commit 72a9e84

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3131
- Workout metrics did not update after the initial save. [#42](https://github.com/LibreFitOrg/LibreFit/issues/42)
3232
- Rest timer notification sound was suppressed by other media. [#45](https://github.com/LibreFitOrg/LibreFit/issues/45)
3333
- Scroll wheel animation not allowing input [#47](https://github.com/LibreFitOrg/LibreFit/issues/47)
34+
- Rare crash in info workout screen [#64](https://github.com/LibreFitOrg/LibreFit/issues/64)
3435
- Parsing logic when typing weight [#43](https://github.com/LibreFitOrg/LibreFit/issues/43)
3536

3637
> [!TIP]

app/src/main/java/org/librefit/ui/screens/infoWorkout/InfoWorkoutScreenViewModel.kt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import androidx.lifecycle.ViewModel
1313
import androidx.lifecycle.viewModelScope
1414
import androidx.navigation.toRoute
1515
import dagger.hilt.android.lifecycle.HiltViewModel
16-
import kotlinx.coroutines.Dispatchers
16+
import kotlinx.coroutines.CoroutineDispatcher
1717
import kotlinx.coroutines.flow.MutableStateFlow
1818
import kotlinx.coroutines.flow.SharingStarted
1919
import kotlinx.coroutines.flow.StateFlow
@@ -25,6 +25,7 @@ import kotlinx.coroutines.flow.update
2525
import kotlinx.coroutines.launch
2626
import org.librefit.db.relations.WorkoutWithExercisesAndSets
2727
import org.librefit.db.repository.WorkoutRepository
28+
import org.librefit.di.qualifiers.IoDispatcher
2829
import org.librefit.enums.WorkoutState
2930
import org.librefit.enums.chart.WorkoutChart
3031
import org.librefit.helpers.DataHelper
@@ -43,7 +44,8 @@ import kotlin.random.Random
4344
class 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

Comments
 (0)