@@ -3,6 +3,7 @@ package com.threegap.bitnagil.presentation.home
33import android.util.Log
44import androidx.lifecycle.SavedStateHandle
55import androidx.lifecycle.viewModelScope
6+ import com.threegap.bitnagil.domain.emotion.usecase.GetMyEmotionUseCase
67import com.threegap.bitnagil.domain.routine.model.RoutineCompletion
78import com.threegap.bitnagil.domain.routine.model.RoutineCompletionInfo
89import com.threegap.bitnagil.domain.routine.usecase.DeleteRoutineByDayUseCase
@@ -11,6 +12,7 @@ import com.threegap.bitnagil.domain.routine.usecase.FetchWeeklyRoutinesUseCase
1112import com.threegap.bitnagil.domain.routine.usecase.RoutineCompletionUseCase
1213import com.threegap.bitnagil.domain.user.usecase.FetchUserProfileUseCase
1314import com.threegap.bitnagil.presentation.common.mviviewmodel.MviViewModel
15+ import com.threegap.bitnagil.presentation.home.model.EmotionBallType
1416import com.threegap.bitnagil.presentation.home.model.HomeIntent
1517import com.threegap.bitnagil.presentation.home.model.HomeSideEffect
1618import com.threegap.bitnagil.presentation.home.model.HomeState
@@ -37,6 +39,7 @@ class HomeViewModel @Inject constructor(
3739 savedStateHandle : SavedStateHandle ,
3840 private val fetchWeeklyRoutinesUseCase : FetchWeeklyRoutinesUseCase ,
3941 private val fetchUserProfileUseCase : FetchUserProfileUseCase ,
42+ private val getMyEmotionUseCase : GetMyEmotionUseCase ,
4043 private val routineCompletionUseCase : RoutineCompletionUseCase ,
4144 private val deleteRoutineUseCase : DeleteRoutineUseCase ,
4245 private val deleteRoutineByDayUseCase : DeleteRoutineByDayUseCase ,
@@ -53,6 +56,7 @@ class HomeViewModel @Inject constructor(
5356 observeRoutineUpdates()
5457 fetchWeeklyRoutines(container.stateFlow.value.currentWeeks)
5558 fetchUserProfile()
59+ getMyEmotion(container.stateFlow.value.selectedDate)
5660 }
5761
5862 override suspend fun SimpleSyntax <HomeState , HomeSideEffect >.reduceState (
@@ -188,6 +192,10 @@ class HomeViewModel @Inject constructor(
188192 }
189193
190194 is HomeIntent .ConfirmRoutineByDayDeletion -> null
195+
196+ is HomeIntent .LoadMyEmotion -> {
197+ state.copy(myEmotion = intent.emotion)
198+ }
191199 }
192200 return newState
193201 }
@@ -252,6 +260,23 @@ class HomeViewModel @Inject constructor(
252260 }
253261 }
254262
263+ private fun getMyEmotion (currentDate : LocalDate ) {
264+ sendIntent(HomeIntent .UpdateLoading (true ))
265+ viewModelScope.launch {
266+ getMyEmotionUseCase(currentDate.toString()).fold(
267+ onSuccess = { emotion ->
268+ val ballType = EmotionBallType .fromDomainEmotion(emotion.emotionMarbleType)
269+ sendIntent(HomeIntent .LoadMyEmotion (ballType))
270+ sendIntent(HomeIntent .UpdateLoading (false ))
271+ },
272+ onFailure = { error ->
273+ Log .e(" HomeViewModel" , " 나의 감정 실패: ${error.message} " )
274+ sendIntent(HomeIntent .UpdateLoading (false ))
275+ },
276+ )
277+ }
278+ }
279+
255280 fun toggleRoutineCompletion (routineId : String , isCompleted : Boolean ) {
256281 val originalState = container.stateFlow.value
257282 sendIntent(HomeIntent .OnRoutineCompletionToggle (routineId, isCompleted))
0 commit comments