@@ -46,7 +46,6 @@ class HomeViewModel @Inject constructor(
4646 init {
4747 observeWeekChanges()
4848 observeRoutineUpdates()
49- observeSideEffects()
5049 fetchWeeklyRoutines(container.stateFlow.value.currentWeeks)
5150 }
5251
@@ -84,15 +83,11 @@ class HomeViewModel @Inject constructor(
8483 }
8584
8685 is HomeIntent .OnRoutineCompletionToggle -> {
87- val updatedState = updateMainRoutine(state, intent.routineId, intent.isCompleted)
88- sendSideEffect(HomeSideEffect .ProcessRoutineToggle (state))
89- updatedState
86+ updateMainRoutine(state, intent.routineId, intent.isCompleted)
9087 }
9188
9289 is HomeIntent .OnSubRoutineCompletionToggle -> {
93- val updatedState = updateSubRoutine(state, intent.routineId, intent.subRoutineId, intent.isCompleted)
94- sendSideEffect(HomeSideEffect .ProcessRoutineToggle (state))
95- updatedState
90+ updateSubRoutine(state, intent.routineId, intent.subRoutineId, intent.isCompleted)
9691 }
9792
9893 is HomeIntent .OnSortTypeChange -> {
@@ -178,18 +173,6 @@ class HomeViewModel @Inject constructor(
178173 }
179174 }
180175
181- private fun observeSideEffects () {
182- viewModelScope.launch {
183- sideEffectFlow.collect { sideEffect ->
184- when (sideEffect) {
185- is HomeSideEffect .ProcessRoutineToggle -> {
186- handleRoutineToggle(sideEffect.originalState)
187- }
188- }
189- }
190- }
191- }
192-
193176 @OptIn(FlowPreview ::class )
194177 private fun observeRoutineUpdates () {
195178 viewModelScope.launch {
@@ -220,17 +203,35 @@ class HomeViewModel @Inject constructor(
220203 }
221204 }
222205
223- private fun handleRoutineToggle (originalState : HomeState ) {
206+ fun toggleRoutineCompletion (routineId : String , isCompleted : Boolean ) {
207+ val originalState = container.stateFlow.value
208+ sendIntent(HomeIntent .OnRoutineCompletionToggle (routineId, isCompleted))
209+
210+ val predictedUpdatedState = updateMainRoutine(originalState, routineId, isCompleted)
211+ processRoutineToggleChanges(originalState, predictedUpdatedState)
212+ }
213+
214+ fun toggleSubRoutineCompletion (routineId : String , subRoutineId : String , isCompleted : Boolean ) {
215+ val originalState = container.stateFlow.value
216+ sendIntent(HomeIntent .OnSubRoutineCompletionToggle (routineId, subRoutineId, isCompleted))
217+
218+ val predictedUpdatedState = updateSubRoutine(originalState, routineId, subRoutineId, isCompleted)
219+ processRoutineToggleChanges(originalState, predictedUpdatedState)
220+ }
221+
222+ private fun processRoutineToggleChanges (
223+ originalState : HomeState ,
224+ updatedState : HomeState ,
225+ ) {
224226 val selectedDate = originalState.selectedDate
225227 val dateKey = selectedDate.toString()
226228
227229 if (! backupStatesByDate.containsKey(dateKey)) {
228230 backupStatesByDate[dateKey] = originalState.routines
229231 }
230232
231- val currentState = container.stateFlow.value
232233 val originalRoutines = backupStatesByDate[dateKey] ? : originalState.routines
233- val changes = calculateStateChanges(originalRoutines, currentState .routines, selectedDate)
234+ val changes = calculateStateChanges(originalRoutines, updatedState .routines, selectedDate)
234235
235236 if (changes.isNotEmpty()) {
236237 pendingChangesByDate[dateKey] = changes.toMutableList()
0 commit comments