From 9d8c5c862ccccde779e81ab795853863c911ab61 Mon Sep 17 00:00:00 2001 From: mangbaam Date: Sun, 6 Oct 2024 21:46:13 +0900 Subject: [PATCH] =?UTF-8?q?Boolti-301=20refactor:=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=ED=95=84=20=EB=A1=9C=EC=A7=81=20=EB=A6=AC=ED=8C=A9=ED=84=B0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../screen/profile/ProfileViewModel.kt | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/presentation/src/main/java/com/nexters/boolti/presentation/screen/profile/ProfileViewModel.kt b/presentation/src/main/java/com/nexters/boolti/presentation/screen/profile/ProfileViewModel.kt index 77179767..b96ea052 100644 --- a/presentation/src/main/java/com/nexters/boolti/presentation/screen/profile/ProfileViewModel.kt +++ b/presentation/src/main/java/com/nexters/boolti/presentation/screen/profile/ProfileViewModel.kt @@ -24,8 +24,8 @@ import javax.inject.Inject class ProfileViewModel @Inject constructor( savedStateHandle: SavedStateHandle, getUserUsecase: GetUserUsecase, - memberRepository: MemberRepository, - authRepository: AuthRepository, + private val memberRepository: MemberRepository, + private val authRepository: AuthRepository, ) : BaseViewModel() { private val _userCode: String? = savedStateHandle[userCode] private val myProfile: User.My? = getUserUsecase() @@ -42,24 +42,34 @@ class ProfileViewModel @Inject constructor( val event = _event.receiveAsFlow() init { - _userCode?.let { userCode -> - viewModelScope.launch { - memberRepository.getMember(userCode).onSuccess { user -> - _uiState.update { it.copy(user = user) } - }.onFailure { - it.printStackTrace() - event(ProfileEvent.Invalid) - } - } - } ?: authRepository.cachedUser + if (_userCode != null) { + fetchOthersProfile(_userCode) + } else { + collectMyProfile() + } + } + + private fun event(event: ProfileEvent) { + viewModelScope.launch { + _event.send(event) + } + } + + private fun collectMyProfile() { + authRepository.cachedUser .filterNotNull() .onEach { user -> _uiState.update { it.copy(user = user) } } .launchIn(viewModelScope) } - private fun event(event: ProfileEvent) { + private fun fetchOthersProfile(userCode: String) { viewModelScope.launch { - _event.send(event) + memberRepository.getMember(userCode).onSuccess { user -> + _uiState.update { it.copy(user = user) } + }.onFailure { + it.printStackTrace() + event(ProfileEvent.Invalid) + } } } }