Skip to content

Commit

Permalink
Boolti-301 refactor: 프로필 로직 리팩터링
Browse files Browse the repository at this point in the history
  • Loading branch information
mangbaam committed Oct 6, 2024
1 parent a270b19 commit 9d8c5c8
Showing 1 changed file with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
}
}
}

0 comments on commit 9d8c5c8

Please sign in to comment.