@@ -5,6 +5,7 @@ import androidx.lifecycle.SavedStateHandle
55import androidx.lifecycle.viewModelScope
66import com.threegap.bitnagil.domain.routine.model.RoutineCompletion
77import com.threegap.bitnagil.domain.routine.model.RoutineCompletionInfo
8+ import com.threegap.bitnagil.domain.routine.usecase.DeleteRoutineByDayUseCase
89import com.threegap.bitnagil.domain.routine.usecase.DeleteRoutineUseCase
910import com.threegap.bitnagil.domain.routine.usecase.FetchWeeklyRoutinesUseCase
1011import com.threegap.bitnagil.domain.routine.usecase.RoutineCompletionUseCase
@@ -15,6 +16,7 @@ import com.threegap.bitnagil.presentation.home.model.HomeState
1516import com.threegap.bitnagil.presentation.home.model.RoutineSortType
1617import com.threegap.bitnagil.presentation.home.model.RoutineUiModel
1718import com.threegap.bitnagil.presentation.home.model.RoutinesUiModel
19+ import com.threegap.bitnagil.presentation.home.model.toRoutineByDayDeletion
1820import com.threegap.bitnagil.presentation.home.model.toUiModel
1921import com.threegap.bitnagil.presentation.home.util.getCurrentWeekDays
2022import dagger.hilt.android.lifecycle.HiltViewModel
@@ -35,6 +37,7 @@ class HomeViewModel @Inject constructor(
3537 private val fetchWeeklyRoutinesUseCase : FetchWeeklyRoutinesUseCase ,
3638 private val routineCompletionUseCase : RoutineCompletionUseCase ,
3739 private val deleteRoutineUseCase : DeleteRoutineUseCase ,
40+ private val deleteRoutineByDayUseCase : DeleteRoutineByDayUseCase ,
3841) : MviViewModel<HomeState, HomeSideEffect, HomeIntent>(
3942 initState = HomeState (),
4043 savedStateHandle = savedStateHandle,
@@ -154,6 +157,30 @@ class HomeViewModel @Inject constructor(
154157 deletingRoutine = null ,
155158 )
156159 }
160+
161+ is HomeIntent .DeleteRoutineByDayOptimistically -> {
162+ val dateKey = intent.performedDate
163+ val updatedRoutinesByDate = state.routines.routinesByDate.toMutableMap()
164+ val routinesForDate = updatedRoutinesByDate[dateKey]?.toMutableList()
165+
166+ if (routinesForDate != null ) {
167+ updatedRoutinesByDate[dateKey] = routinesForDate.filterNot {
168+ it.routineId == intent.routineId
169+ }
170+ }
171+
172+ state.copy(
173+ routines = RoutinesUiModel (routinesByDate = updatedRoutinesByDate),
174+ showDeleteConfirmDialog = false ,
175+ deletingRoutine = null ,
176+ )
177+ }
178+
179+ is HomeIntent .RestoreRoutinesAfterDeleteByDayFailure -> {
180+ state.copy(routines = intent.backupRoutines)
181+ }
182+
183+ is HomeIntent .ConfirmRoutineByDayDeletion -> null
157184 }
158185 return newState
159186 }
@@ -395,4 +422,35 @@ class HomeViewModel @Inject constructor(
395422 )
396423 }
397424 }
425+
426+ fun deleteRoutineByDay (routineUiModel : RoutineUiModel ) {
427+ val currentRoutines = container.stateFlow.value.routines
428+ val performedDate = container.stateFlow.value.selectedDate.toString()
429+
430+ sendIntent(
431+ HomeIntent .DeleteRoutineByDayOptimistically (
432+ routineId = routineUiModel.routineId,
433+ performedDate = performedDate,
434+ ),
435+ )
436+
437+ viewModelScope.launch {
438+ val routineByDayDeletion = routineUiModel.toRoutineByDayDeletion(performedDate)
439+
440+ deleteRoutineByDayUseCase(routineByDayDeletion).fold(
441+ onSuccess = {
442+ sendIntent(
443+ HomeIntent .ConfirmRoutineByDayDeletion (
444+ routineId = routineUiModel.routineId,
445+ performedDate = performedDate,
446+ ),
447+ )
448+ },
449+ onFailure = {
450+ Log .e(" HomeViewModel" , " 루틴 삭제 실패: ${it.message} " )
451+ sendIntent(HomeIntent .RestoreRoutinesAfterDeleteByDayFailure (currentRoutines))
452+ },
453+ )
454+ }
455+ }
398456}
0 commit comments