@@ -3,18 +3,15 @@ package com.threegap.bitnagil.presentation.screen.routinelist
33import android.util.Log
44import androidx.lifecycle.SavedStateHandle
55import androidx.lifecycle.ViewModel
6- import androidx.lifecycle.viewModelScope
76import com.threegap.bitnagil.domain.routine.usecase.DeleteRoutineForDayUseCase
87import com.threegap.bitnagil.domain.routine.usecase.DeleteRoutineUseCase
9- import com.threegap.bitnagil.domain.routine.usecase.FetchWeeklyRoutinesUseCase
10- import com.threegap.bitnagil.domain.routine.usecase.GetWriteRoutineEventFlowUseCase
8+ import com.threegap.bitnagil.domain.routine.usecase.ObserveWeeklyRoutinesUseCase
119import com.threegap.bitnagil.presentation.screen.home.util.getCurrentWeekDays
1210import com.threegap.bitnagil.presentation.screen.routinelist.contract.RoutineListSideEffect
1311import com.threegap.bitnagil.presentation.screen.routinelist.contract.RoutineListState
1412import com.threegap.bitnagil.presentation.screen.routinelist.model.RoutineUiModel
1513import com.threegap.bitnagil.presentation.screen.routinelist.model.toUiModel
1614import dagger.hilt.android.lifecycle.HiltViewModel
17- import kotlinx.coroutines.launch
1815import org.orbitmvi.orbit.Container
1916import org.orbitmvi.orbit.ContainerHost
2017import org.orbitmvi.orbit.viewmodel.container
@@ -24,10 +21,9 @@ import javax.inject.Inject
2421@HiltViewModel
2522class RoutineListViewModel @Inject constructor(
2623 savedStateHandle : SavedStateHandle ,
27- private val fetchWeeklyRoutinesUseCase : FetchWeeklyRoutinesUseCase ,
24+ private val observeWeeklyRoutinesUseCase : ObserveWeeklyRoutinesUseCase ,
2825 private val deleteRoutineUseCase : DeleteRoutineUseCase ,
2926 private val deleteRoutineForDayUseCase : DeleteRoutineForDayUseCase ,
30- private val getWriteRoutineEventFlowUseCase : GetWriteRoutineEventFlowUseCase ,
3127) : ContainerHost<RoutineListState, RoutineListSideEffect>, ViewModel() {
3228
3329 override val container: Container <RoutineListState , RoutineListSideEffect > = container(initialState = RoutineListState .INIT )
@@ -41,8 +37,7 @@ class RoutineListViewModel @Inject constructor(
4137
4238 init {
4339 updateDate(selectedDate)
44- fetchRoutines()
45- observeRoutineChanges()
40+ observeWeeklyRoutines()
4641 }
4742
4843 fun updateDate (selectedDate : LocalDate ) {
@@ -75,29 +70,14 @@ class RoutineListViewModel @Inject constructor(
7570 }
7671 }
7772
78- private fun observeRoutineChanges () {
79- viewModelScope.launch {
80- getWriteRoutineEventFlowUseCase().collect {
81- fetchRoutines()
82- }
83- }
84- }
85-
86- private fun fetchRoutines () {
73+ private fun observeWeeklyRoutines () {
8774 intent {
8875 reduce { state.copy(isLoading = true ) }
89- val currentWeek = state.selectedDate.getCurrentWeekDays()
90- val startDate = currentWeek.first().toString()
91- val endDate = currentWeek.last().toString()
92- fetchWeeklyRoutinesUseCase(startDate, endDate).fold(
93- onSuccess = { routineSchedule ->
94- reduce { state.copy(isLoading = false , routines = routineSchedule.toUiModel()) }
95- },
96- onFailure = {
97- Log .e(" RoutineListViewModel" , " 루틴 가져오기 실패: ${it.message} " )
98- reduce { state.copy(isLoading = false ) }
99- },
100- )
76+ val weekDays = state.selectedDate.getCurrentWeekDays()
77+ observeWeeklyRoutinesUseCase(weekDays.first().toString(), weekDays.last().toString())
78+ .collect { schedule ->
79+ reduce { state.copy(isLoading = false , routines = schedule.toUiModel()) }
80+ }
10181 }
10282 }
10383
@@ -107,8 +87,7 @@ class RoutineListViewModel @Inject constructor(
10787 reduce { state.copy(isLoading = true ) }
10888 deleteRoutineUseCase(selectedRoutine.routineId).fold(
10989 onSuccess = {
110- fetchRoutines()
111- reduce { state.copy(deleteConfirmBottomSheetVisible = false ) }
90+ reduce { state.copy(isLoading = false , deleteConfirmBottomSheetVisible = false ) }
11291 postSideEffect(RoutineListSideEffect .ShowToast (" 삭제가 완료되었습니다." ))
11392 },
11493 onFailure = {
@@ -121,12 +100,11 @@ class RoutineListViewModel @Inject constructor(
121100
122101 fun deleteRoutineForToday () {
123102 intent {
124- reduce { state.copy(isLoading = true ) }
125103 val selectedRoutine = state.selectedRoutine ? : return @intent
104+ reduce { state.copy(isLoading = true ) }
126105 deleteRoutineForDayUseCase(selectedRoutine.routineId).fold(
127106 onSuccess = {
128- fetchRoutines()
129- reduce { state.copy(deleteConfirmBottomSheetVisible = false ) }
107+ reduce { state.copy(isLoading = false , deleteConfirmBottomSheetVisible = false ) }
130108 postSideEffect(RoutineListSideEffect .ShowToast (" 삭제가 완료되었습니다." ))
131109 },
132110 onFailure = {
0 commit comments