11package com.threegap.bitnagil.presentation.recommendroutine
22
3- import androidx.lifecycle.SavedStateHandle
3+ import androidx.lifecycle.ViewModel
44import androidx.lifecycle.viewModelScope
55import com.threegap.bitnagil.domain.emotion.usecase.GetEmotionChangeEventFlowUseCase
66import com.threegap.bitnagil.domain.recommendroutine.model.RecommendCategory
77import com.threegap.bitnagil.domain.recommendroutine.model.RecommendLevel
88import com.threegap.bitnagil.domain.recommendroutine.usecase.FetchRecommendRoutinesUseCase
9- import com.threegap.bitnagil.presentation.common.mviviewmodel.MviViewModel
10- import com.threegap.bitnagil.presentation.recommendroutine.model.RecommendRoutineIntent
119import com.threegap.bitnagil.presentation.recommendroutine.model.RecommendRoutineSideEffect
1210import com.threegap.bitnagil.presentation.recommendroutine.model.RecommendRoutineState
1311import com.threegap.bitnagil.presentation.recommendroutine.model.RecommendRoutineUiModel
1412import com.threegap.bitnagil.presentation.recommendroutine.model.RecommendRoutinesUiModel
1513import com.threegap.bitnagil.presentation.recommendroutine.model.toUiModel
1614import dagger.hilt.android.lifecycle.HiltViewModel
1715import kotlinx.coroutines.launch
18- import org.orbitmvi.orbit.syntax.Syntax
16+ import org.orbitmvi.orbit.Container
17+ import org.orbitmvi.orbit.ContainerHost
18+ import org.orbitmvi.orbit.viewmodel.container
1919import javax.inject.Inject
2020
2121@HiltViewModel
2222class RecommendRoutineViewModel @Inject constructor(
23- savedStateHandle : SavedStateHandle ,
2423 private val fetchRecommendRoutinesUseCase : FetchRecommendRoutinesUseCase ,
2524 private val getEmotionChangeEventFlowUseCase : GetEmotionChangeEventFlowUseCase ,
26- ) : MviViewModel <RecommendRoutineState, RecommendRoutineSideEffect, RecommendRoutineIntent>(
27- initState = RecommendRoutineState (),
28- savedStateHandle = savedStateHandle,
29- ) {
25+ ) : ContainerHost <RecommendRoutineState, RecommendRoutineSideEffect>, ViewModel() {
26+
27+ override val container : Container < RecommendRoutineState , RecommendRoutineSideEffect > =
28+ container(initialState = RecommendRoutineState . INIT )
3029
3130 init {
3231 loadRecommendRoutines()
@@ -35,49 +34,37 @@ class RecommendRoutineViewModel @Inject constructor(
3534
3635 private var recommendRoutines: RecommendRoutinesUiModel = RecommendRoutinesUiModel ()
3736
38- override suspend fun Syntax <RecommendRoutineState , RecommendRoutineSideEffect >.reduceState (
39- intent : RecommendRoutineIntent ,
40- state : RecommendRoutineState ,
41- ): RecommendRoutineState ? = when (intent) {
42- is RecommendRoutineIntent .UpdateLoading -> {
43- state.copy(isLoading = intent.isLoading)
44- }
45-
46- is RecommendRoutineIntent .LoadRecommendRoutines -> {
47- state.copy(
48- isLoading = false ,
49- currentRoutines = getCurrentRoutines(state.selectedCategory, state.selectedRecommendLevel),
50- emotionMarbleType = recommendRoutines.emotionMarbleType,
51- )
52- }
53-
54- is RecommendRoutineIntent .OnCategorySelected -> {
55- state.copy(
56- selectedCategory = intent.category,
57- currentRoutines = getCurrentRoutines(intent.category, state.selectedRecommendLevel),
58- )
37+ fun updateRoutineCategory (category : RecommendCategory ) {
38+ intent {
39+ reduce {
40+ state.copy(
41+ selectedCategory = category,
42+ currentRoutines = getCurrentRoutines(category, state.selectedRecommendLevel),
43+ )
44+ }
5945 }
46+ }
6047
61- is RecommendRoutineIntent .ShowRecommendLevelBottomSheet -> {
62- state.copy(recommendLevelBottomSheetVisible = true )
48+ fun showRecommendLevelBottomSheet () {
49+ intent {
50+ reduce { state.copy(recommendLevelBottomSheetVisible = true ) }
6351 }
52+ }
6453
65- is RecommendRoutineIntent .HideRecommendLevelBottomSheet -> {
66- state.copy(recommendLevelBottomSheetVisible = false )
67- }
68-
69- is RecommendRoutineIntent .OnRecommendLevelSelected -> {
70- state.copy(
71- selectedRecommendLevel = intent.recommendLevel,
72- currentRoutines = getCurrentRoutines(state.selectedCategory, intent.recommendLevel),
73- )
54+ fun hideRecommendLevelBottomSheet () {
55+ intent {
56+ reduce { state.copy(recommendLevelBottomSheetVisible = false ) }
7457 }
58+ }
7559
76- RecommendRoutineIntent .ClearRecommendLevelFilter -> {
77- state.copy(
78- selectedRecommendLevel = null ,
79- currentRoutines = getCurrentRoutines(state.selectedCategory, null ),
80- )
60+ fun updateRecommendLevel (recommendLevel : RecommendLevel ? ) {
61+ intent {
62+ reduce {
63+ state.copy(
64+ selectedRecommendLevel = recommendLevel,
65+ currentRoutines = getCurrentRoutines(state.selectedCategory, recommendLevel),
66+ )
67+ }
8168 }
8269 }
8370
@@ -102,17 +89,35 @@ class RecommendRoutineViewModel @Inject constructor(
10289 }
10390
10491 private fun loadRecommendRoutines () {
105- sendIntent( RecommendRoutineIntent . UpdateLoading ( true ))
106- viewModelScope.launch {
92+ intent {
93+ reduce { state.copy(isLoading = true ) }
10794 fetchRecommendRoutinesUseCase().fold(
10895 onSuccess = {
109- recommendRoutines = it.toUiModel()
110- sendIntent(RecommendRoutineIntent .LoadRecommendRoutines )
96+ reduce {
97+ recommendRoutines = it.toUiModel()
98+ state.copy(
99+ isLoading = false ,
100+ currentRoutines = getCurrentRoutines(state.selectedCategory, state.selectedRecommendLevel),
101+ emotionMarbleType = recommendRoutines.emotionMarbleType,
102+ )
103+ }
111104 },
112105 onFailure = {
113- sendIntent( RecommendRoutineIntent . UpdateLoading ( false ))
106+ reduce { state.copy(isLoading = false ) }
114107 },
115108 )
116109 }
117110 }
111+
112+ fun navigateToEmotion () {
113+ intent {
114+ postSideEffect(RecommendRoutineSideEffect .NavigateToEmotion )
115+ }
116+ }
117+
118+ fun navigateToRegisterRoutine (routineId : String ) {
119+ intent {
120+ postSideEffect(RecommendRoutineSideEffect .NavigateToRegisterRoutine (routineId))
121+ }
122+ }
118123}
0 commit comments