-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgramsViewModel.kt
105 lines (90 loc) · 4.06 KB
/
ProgramsViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package agdesigns.elevatefitness.viewmodels
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import agdesigns.elevatefitness.data.Repository
import agdesigns.elevatefitness.data.exercise.ProgramExerciseAndInfo
import agdesigns.elevatefitness.data.workout_program.WorkoutProgram
import agdesigns.elevatefitness.data.workout_program.WorkoutProgramRename
import agdesigns.elevatefitness.data.workout_program.WorkoutProgramReorder
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject
data class ProgramsState(
val programs: List<WorkoutProgram> = emptyList(),
val exercisesAndInfo: Map<Long, List<ProgramExerciseAndInfo>> = emptyMap(),
val openAddProgramDialog: Boolean = false,
val openChangeNameDialog: Boolean = false,
val programToBeChanged: Long = 0
)
sealed class ProgramsEvent{
data object ToggleAddProgramDialog : ProgramsEvent()
data class ToggleChangeNameDialog(val programId: Long = 0) : ProgramsEvent()
data class GetPrograms(val planId: Long): ProgramsEvent()
data class AddProgram(val workoutProgram: WorkoutProgram): ProgramsEvent()
data class RenameProgram(val workoutProgramRename: WorkoutProgramRename): ProgramsEvent()
data class ReorderProgram(val workoutProgramReorders: List<WorkoutProgramReorder>): ProgramsEvent()
data class DeleteProgram(val programId: Long): ProgramsEvent()
}
@HiltViewModel
class ProgramsViewModel @Inject constructor(private val repository: Repository): ViewModel() {
private val _state = mutableStateOf(ProgramsState())
val state: State<ProgramsState> = _state
private var getProgramsJob: Job? = null
private var getProgramExercisesJob: Job? = null
fun onEvent(event: ProgramsEvent){
when (event) {
is ProgramsEvent.GetPrograms -> {
getProgramsJob?.cancel()
getProgramsJob = viewModelScope.launch {
repository.getPrograms(event.planId).collect {
_state.value = state.value.copy(
programs = it.sortedBy { prog -> prog.orderInWorkoutPlan }
)
getProgramExercisesJob?.cancel()
getProgramExercisesJob = this.launch {
repository.getProgramExercisesAndInfo(it.map { prg -> prg.programId }).collect{ exList ->
_state.value = state.value.copy(
exercisesAndInfo = exList.groupBy { ex -> ex.extProgramId } // FIXME: should sort each list
)
}
}
}
}
}
is ProgramsEvent.AddProgram -> {
viewModelScope.launch {
repository.addProgram(event.workoutProgram)
}
}
is ProgramsEvent.ToggleAddProgramDialog -> {
_state.value = state.value.copy(
openAddProgramDialog = !state.value.openAddProgramDialog
)
}
is ProgramsEvent.ToggleChangeNameDialog -> {
_state.value = state.value.copy(
openChangeNameDialog = !state.value.openChangeNameDialog,
programToBeChanged = event.programId
)
}
is ProgramsEvent.RenameProgram -> {
viewModelScope.launch {
repository.renameProgram(event.workoutProgramRename)
}
}
is ProgramsEvent.ReorderProgram -> {
viewModelScope.launch {
repository.reorderPrograms(event.workoutProgramReorders)
}
}
is ProgramsEvent.DeleteProgram -> {
viewModelScope.launch {
repository.removeProgramFromPlan(event.programId)
}
}
}
}
}