Skip to content

Commit 75cd566

Browse files
committed
Refactor: Recommend MviViewModel 제거
1 parent c7499c5 commit 75cd566

5 files changed

Lines changed: 97 additions & 98 deletions

File tree

presentation/src/main/java/com/threegap/bitnagil/presentation/recommendroutine/RecommendRoutineScreen.kt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import androidx.compose.ui.Modifier
2626
import androidx.compose.ui.tooling.preview.Preview
2727
import androidx.compose.ui.unit.dp
2828
import androidx.hilt.navigation.compose.hiltViewModel
29-
import androidx.lifecycle.compose.collectAsStateWithLifecycle
3029
import com.threegap.bitnagil.designsystem.BitnagilTheme
3130
import com.threegap.bitnagil.designsystem.R
3231
import com.threegap.bitnagil.designsystem.component.atom.BitnagilIcon
@@ -38,39 +37,40 @@ import com.threegap.bitnagil.presentation.recommendroutine.component.block.Emoti
3837
import com.threegap.bitnagil.presentation.recommendroutine.component.block.RecommendRoutineItem
3938
import com.threegap.bitnagil.presentation.recommendroutine.component.template.EmptyRecommendRoutineView
4039
import com.threegap.bitnagil.presentation.recommendroutine.component.template.RecommendLevelBottomSheet
41-
import com.threegap.bitnagil.presentation.recommendroutine.model.RecommendRoutineIntent
40+
import com.threegap.bitnagil.presentation.recommendroutine.model.RecommendRoutineSideEffect
4241
import com.threegap.bitnagil.presentation.recommendroutine.model.RecommendRoutineState
42+
import org.orbitmvi.orbit.compose.collectAsState
43+
import org.orbitmvi.orbit.compose.collectSideEffect
4344

4445
@Composable
4546
fun RecommendRoutineScreenContainer(
46-
viewmodel: RecommendRoutineViewModel = hiltViewModel(),
47+
viewModel: RecommendRoutineViewModel = hiltViewModel(),
4748
navigateToEmotion: () -> Unit,
4849
navigateToRegisterRoutine: (String?) -> Unit,
4950
) {
50-
val uiState by viewmodel.container.stateFlow.collectAsStateWithLifecycle()
51+
val uiState by viewModel.collectAsState()
52+
53+
viewModel.collectSideEffect { sideEffect ->
54+
when (sideEffect) {
55+
is RecommendRoutineSideEffect.NavigateToEmotion -> navigateToEmotion()
56+
is RecommendRoutineSideEffect.NavigateToRegisterRoutine -> navigateToRegisterRoutine(sideEffect.routineId)
57+
}
58+
}
5159

5260
if (uiState.recommendLevelBottomSheetVisible) {
5361
RecommendLevelBottomSheet(
5462
selectedRecommendLevel = uiState.selectedRecommendLevel,
55-
onRecommendLevelSelected = { selectedLevel ->
56-
viewmodel.sendIntent(RecommendRoutineIntent.OnRecommendLevelSelected(selectedLevel))
57-
},
58-
onDismiss = {
59-
viewmodel.sendIntent(RecommendRoutineIntent.HideRecommendLevelBottomSheet)
60-
},
63+
onRecommendLevelSelected = viewModel::updateRecommendLevel,
64+
onDismiss = viewModel::hideRecommendLevelBottomSheet,
6165
)
6266
}
6367

6468
RecommendRoutineScreen(
6569
uiState = uiState,
66-
onCategorySelected = { category ->
67-
viewmodel.sendIntent(RecommendRoutineIntent.OnCategorySelected(category))
68-
},
69-
onShowDifficultyBottomSheet = {
70-
viewmodel.sendIntent(RecommendRoutineIntent.ShowRecommendLevelBottomSheet)
71-
},
72-
onRecommendRoutineByEmotionClick = navigateToEmotion,
73-
onRegisterRoutineClick = navigateToRegisterRoutine,
70+
onCategorySelected = viewModel::updateRoutineCategory,
71+
onShowDifficultyBottomSheet = viewModel::showRecommendLevelBottomSheet,
72+
onRecommendRoutineByEmotionClick = viewModel::navigateToEmotion,
73+
onRegisterRoutineClick = viewModel::navigateToRegisterRoutine,
7474
)
7575
}
7676

@@ -204,7 +204,7 @@ private fun RecommendRoutineScreen(
204204
@Composable
205205
private fun RoutineRecommendScreenPreview() {
206206
RecommendRoutineScreen(
207-
uiState = RecommendRoutineState(),
207+
uiState = RecommendRoutineState.INIT,
208208
onCategorySelected = {},
209209
onShowDifficultyBottomSheet = {},
210210
onRecommendRoutineByEmotionClick = {},
Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
package com.threegap.bitnagil.presentation.recommendroutine
22

3-
import androidx.lifecycle.SavedStateHandle
3+
import androidx.lifecycle.ViewModel
44
import androidx.lifecycle.viewModelScope
55
import com.threegap.bitnagil.domain.emotion.usecase.GetEmotionChangeEventFlowUseCase
66
import com.threegap.bitnagil.domain.recommendroutine.model.RecommendCategory
77
import com.threegap.bitnagil.domain.recommendroutine.model.RecommendLevel
88
import 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
119
import com.threegap.bitnagil.presentation.recommendroutine.model.RecommendRoutineSideEffect
1210
import com.threegap.bitnagil.presentation.recommendroutine.model.RecommendRoutineState
1311
import com.threegap.bitnagil.presentation.recommendroutine.model.RecommendRoutineUiModel
1412
import com.threegap.bitnagil.presentation.recommendroutine.model.RecommendRoutinesUiModel
1513
import com.threegap.bitnagil.presentation.recommendroutine.model.toUiModel
1614
import dagger.hilt.android.lifecycle.HiltViewModel
1715
import 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
1919
import javax.inject.Inject
2020

2121
@HiltViewModel
2222
class 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
}

presentation/src/main/java/com/threegap/bitnagil/presentation/recommendroutine/model/RecommendRoutineIntent.kt

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.threegap.bitnagil.presentation.recommendroutine.model
22

3-
import com.threegap.bitnagil.presentation.common.mviviewmodel.MviSideEffect
4-
5-
interface RecommendRoutineSideEffect : MviSideEffect
3+
sealed interface RecommendRoutineSideEffect {
4+
data object NavigateToEmotion : RecommendRoutineSideEffect
5+
data class NavigateToRegisterRoutine(val routineId: String) : RecommendRoutineSideEffect
6+
}

presentation/src/main/java/com/threegap/bitnagil/presentation/recommendroutine/model/RecommendRoutineState.kt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@ package com.threegap.bitnagil.presentation.recommendroutine.model
33
import com.threegap.bitnagil.domain.recommendroutine.model.EmotionMarbleType
44
import com.threegap.bitnagil.domain.recommendroutine.model.RecommendCategory
55
import com.threegap.bitnagil.domain.recommendroutine.model.RecommendLevel
6-
import com.threegap.bitnagil.presentation.common.mviviewmodel.MviState
7-
import kotlinx.parcelize.Parcelize
86

9-
@Parcelize
107
data class RecommendRoutineState(
11-
val isLoading: Boolean = false,
12-
val currentRoutines: List<RecommendRoutineUiModel> = emptyList(),
13-
val selectedCategory: RecommendCategory = RecommendCategory.PERSONALIZED,
14-
val recommendLevelBottomSheetVisible: Boolean = false,
15-
val selectedRecommendLevel: RecommendLevel? = null,
16-
val emotionMarbleType: EmotionMarbleType? = null,
17-
) : MviState {
8+
val isLoading: Boolean,
9+
val currentRoutines: List<RecommendRoutineUiModel>,
10+
val selectedCategory: RecommendCategory,
11+
val recommendLevelBottomSheetVisible: Boolean,
12+
val selectedRecommendLevel: RecommendLevel?,
13+
val emotionMarbleType: EmotionMarbleType?,
14+
) {
1815
val shouldShowEmotionButton: Boolean
1916
get() = selectedCategory == RecommendCategory.PERSONALIZED && emotionMarbleType == null
17+
18+
companion object {
19+
val INIT = RecommendRoutineState(
20+
isLoading = false,
21+
currentRoutines = emptyList(),
22+
selectedCategory = RecommendCategory.PERSONALIZED,
23+
recommendLevelBottomSheetVisible = false,
24+
selectedRecommendLevel = null,
25+
emotionMarbleType = null,
26+
)
27+
}
2028
}

0 commit comments

Comments
 (0)