Skip to content

Commit e5c743e

Browse files
committed
Refactor: Home, MyPage, OnBoarding ViewModel에서 유저 정보를 Flow로 구독하도록 변경
1 parent 58e9b3c commit e5c743e

3 files changed

Lines changed: 40 additions & 37 deletions

File tree

presentation/src/main/java/com/threegap/bitnagil/presentation/screen/home/HomeViewModel.kt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import com.threegap.bitnagil.domain.routine.usecase.FetchWeeklyRoutinesUseCase
1111
import com.threegap.bitnagil.domain.routine.usecase.GetWriteRoutineEventFlowUseCase
1212
import com.threegap.bitnagil.domain.routine.usecase.RoutineCompletionUseCase
1313
import com.threegap.bitnagil.domain.routine.usecase.ToggleRoutineUseCase
14-
import com.threegap.bitnagil.domain.user.usecase.FetchUserProfileUseCase
14+
import com.threegap.bitnagil.domain.user.usecase.ObserveUserProfileUseCase
1515
import com.threegap.bitnagil.presentation.screen.home.contract.HomeSideEffect
1616
import com.threegap.bitnagil.presentation.screen.home.contract.HomeState
1717
import com.threegap.bitnagil.presentation.screen.home.model.ToggleStrategy
@@ -35,7 +35,7 @@ import javax.inject.Inject
3535
@HiltViewModel
3636
class HomeViewModel @Inject constructor(
3737
private val fetchWeeklyRoutinesUseCase: FetchWeeklyRoutinesUseCase,
38-
private val fetchUserProfileUseCase: FetchUserProfileUseCase,
38+
private val observeUserProfileUseCase: ObserveUserProfileUseCase,
3939
private val fetchDailyEmotionUseCase: FetchDailyEmotionUseCase,
4040
private val routineCompletionUseCase: RoutineCompletionUseCase,
4141
private val getWriteRoutineEventFlowUseCase: GetWriteRoutineEventFlowUseCase,
@@ -185,7 +185,7 @@ class HomeViewModel @Inject constructor(
185185
private fun initialize() {
186186
intent {
187187
coroutineScope {
188-
launch { fetchUserProfile() }
188+
launch { observeUserProfile() }
189189
launch { fetchDailyEmotion() }
190190
launch { fetchWeeklyRoutines(state.currentWeeks) }
191191
launch { observeWriteRoutineEvent() }
@@ -246,18 +246,22 @@ class HomeViewModel @Inject constructor(
246246
}
247247
}
248248

249-
private suspend fun fetchUserProfile() {
250-
subIntent {
251-
reduce { state.copy(loadingCount = state.loadingCount + 1) }
252-
fetchUserProfileUseCase().fold(
253-
onSuccess = {
254-
reduce { state.copy(userNickname = it.nickname, loadingCount = state.loadingCount - 1) }
255-
},
256-
onFailure = {
257-
Log.e("HomeViewModel", "유저 정보 가져오기 실패: ${it.message}")
258-
reduce { state.copy(loadingCount = state.loadingCount - 1) }
259-
},
260-
)
249+
private fun observeUserProfile() {
250+
intent {
251+
repeatOnSubscription {
252+
reduce { state.copy(loadingCount = state.loadingCount + 1) }
253+
observeUserProfileUseCase().collect { result ->
254+
result.fold(
255+
onSuccess = {
256+
reduce { state.copy(userNickname = it.nickname, loadingCount = state.loadingCount - 1) }
257+
},
258+
onFailure = {
259+
Log.e("HomeViewModel", "유저 정보 가져오기 실패: ${it.message}")
260+
reduce { state.copy(loadingCount = state.loadingCount - 1) }
261+
},
262+
)
263+
}
264+
}
261265
}
262266
}
263267

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.threegap.bitnagil.presentation.screen.mypage
22

33
import androidx.lifecycle.ViewModel
4-
import com.threegap.bitnagil.domain.user.usecase.FetchUserProfileUseCase
4+
import com.threegap.bitnagil.domain.user.usecase.ObserveUserProfileUseCase
55
import com.threegap.bitnagil.presentation.screen.mypage.contract.MyPageSideEffect
66
import com.threegap.bitnagil.presentation.screen.mypage.contract.MyPageState
77
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -12,26 +12,24 @@ import javax.inject.Inject
1212

1313
@HiltViewModel
1414
class MyPageViewModel @Inject constructor(
15-
private val fetchUserProfileUseCase: FetchUserProfileUseCase,
15+
private val observeUserProfileUseCase: ObserveUserProfileUseCase,
1616
) : ContainerHost<MyPageState, MyPageSideEffect>, ViewModel() {
17-
override val container: Container<MyPageState, MyPageSideEffect> = container(initialState = MyPageState.INIT)
1817

19-
init {
20-
loadMyPageInfo()
21-
}
22-
23-
private fun loadMyPageInfo() = intent {
24-
fetchUserProfileUseCase().fold(
25-
onSuccess = {
26-
reduce {
27-
state.copy(
28-
name = it.nickname,
29-
profileUrl = "profileUrl",
30-
)
18+
override val container: Container<MyPageState, MyPageSideEffect> =
19+
container(
20+
initialState = MyPageState.INIT,
21+
buildSettings = { repeatOnSubscribedStopTimeout = 5_000L },
22+
onCreate = {
23+
repeatOnSubscription {
24+
observeUserProfileUseCase().collect { result ->
25+
result.fold(
26+
onSuccess = {
27+
reduce { state.copy(name = it.nickname, profileUrl = "profileUrl") }
28+
},
29+
onFailure = {},
30+
)
31+
}
3132
}
3233
},
33-
onFailure = {
34-
},
3534
)
36-
}
3735
}

presentation/src/main/java/com/threegap/bitnagil/presentation/screen/onboarding/OnBoardingViewModel.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.threegap.bitnagil.domain.onboarding.usecase.GetOnBoardingsUseCase
88
import com.threegap.bitnagil.domain.onboarding.usecase.GetRecommendOnBoardingRoutinesUseCase
99
import com.threegap.bitnagil.domain.onboarding.usecase.GetUserOnBoardingUseCase
1010
import com.threegap.bitnagil.domain.onboarding.usecase.RegisterRecommendOnBoardingRoutinesUseCase
11-
import com.threegap.bitnagil.domain.user.usecase.FetchUserProfileUseCase
11+
import com.threegap.bitnagil.domain.user.usecase.ObserveUserProfileUseCase
1212
import com.threegap.bitnagil.presentation.screen.onboarding.contract.OnBoardingSideEffect
1313
import com.threegap.bitnagil.presentation.screen.onboarding.contract.OnBoardingState
1414
import com.threegap.bitnagil.presentation.screen.onboarding.model.OnBoardingItemUiModel
@@ -22,6 +22,7 @@ import dagger.assisted.AssistedInject
2222
import dagger.hilt.android.lifecycle.HiltViewModel
2323
import kotlinx.coroutines.Job
2424
import kotlinx.coroutines.async
25+
import kotlinx.coroutines.flow.first
2526
import kotlinx.coroutines.isActive
2627
import org.orbitmvi.orbit.Container
2728
import org.orbitmvi.orbit.ContainerHost
@@ -34,7 +35,7 @@ class OnBoardingViewModel @AssistedInject constructor(
3435
private val getRecommendOnBoardingRoutinesUseCase: GetRecommendOnBoardingRoutinesUseCase,
3536
private val getOnBoardingAbstractUseCase: GetOnBoardingAbstractUseCase,
3637
private val registerRecommendOnBoardingRoutinesUseCase: RegisterRecommendOnBoardingRoutinesUseCase,
37-
private val fetchUserProfileUseCase: FetchUserProfileUseCase,
38+
private val observeUserProfileUseCase: ObserveUserProfileUseCase,
3839
private val getUserOnBoardingUseCase: GetUserOnBoardingUseCase,
3940
@Assisted private val onBoardingArg: OnBoardingScreenArg,
4041
) : ContainerHost<OnBoardingState, OnBoardingSideEffect>, ViewModel() {
@@ -71,7 +72,7 @@ class OnBoardingViewModel @AssistedInject constructor(
7172
}
7273

7374
private fun loadIntro() = intent {
74-
val userName = fetchUserProfileUseCase().getOrNull()?.nickname ?: "-"
75+
val userName = observeUserProfileUseCase().first().getOrNull()?.nickname ?: "-"
7576

7677
reduce {
7778
OnBoardingState.Idle(
@@ -86,7 +87,7 @@ class OnBoardingViewModel @AssistedInject constructor(
8687
}
8788

8889
private fun loadUserOnBoarding() = intent {
89-
val userName = fetchUserProfileUseCase().getOrNull()?.nickname ?: "-"
90+
val userName = observeUserProfileUseCase().first().getOrNull()?.nickname ?: "-"
9091
val userOnBoarding = getUserOnBoardingUseCase().fold(
9192
onSuccess = { it },
9293
onFailure = {

0 commit comments

Comments
 (0)