diff --git a/app/src/main/java/com/sopt/now/ui/mypage/MypageViewModel.kt b/app/src/main/java/com/sopt/now/ui/mypage/MypageViewModel.kt index cd98de5..d08ccf3 100644 --- a/app/src/main/java/com/sopt/now/ui/mypage/MypageViewModel.kt +++ b/app/src/main/java/com/sopt/now/ui/mypage/MypageViewModel.kt @@ -2,40 +2,55 @@ package com.sopt.now.ui.mypage import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.sopt.now.data.UserRepository import com.sopt.now.network.response.ResponseInfoDto import com.sopt.now.network.response.UserInfo import com.sopt.now.network.service.ServicePool import com.sopt.now.ui.AuthState -import retrofit2.Call -import retrofit2.Callback +import kotlinx.coroutines.launch import retrofit2.Response -class MypageViewModel(private val userRepository: UserRepository): ViewModel() { +class MypageViewModel(private val userRepository: UserRepository) : ViewModel() { private val authService by lazy { ServicePool.authService } - val userInfoStatus = MutableLiveData() + private val userInfoStatus = MutableLiveData() val userLiveData = MutableLiveData() fun info() { - authService.info().enqueue(object : Callback { - override fun onResponse(call: Call, response: Response) { - if (response.isSuccessful) { - val userInfo = response.body()?.data - if (userInfo != null) { - userLiveData.postValue(userInfo) - userInfoStatus.value = AuthState(isSuccess = true, message = "회원 정보 조회 성공") - } else { - userInfoStatus.value = AuthState(isSuccess = false, message = "회원 정보 없음") - } - } else { - userInfoStatus.value = AuthState(isSuccess = false, message = "회원 정보 조회 실패") - } + viewModelScope.launch { + runCatching { + authService.info() + }.onSuccess { response -> + handleSuccess(response) + }.onFailure { + userInfoStatus.value = AuthState( + isSuccess = false, + message = "서버 에러" + ) } + } + } - override fun onFailure(call: Call, t: Throwable) { - userInfoStatus.value = AuthState(isSuccess = false, message = "서버 에러") - } - }) + private fun handleSuccess(response: Response) { + if (response.isSuccessful) { + successResponse(response) + } else { + failResponse() + } + } + + private fun successResponse(response: Response) { + val userInfo = response.body()?.data + if (userInfo != null) { + userLiveData.postValue(userInfo) + userInfoStatus.value = AuthState(isSuccess = true, message = "회원 정보 조회 성공") + } else { + userInfoStatus.value = AuthState(isSuccess = false, message = "회원 정보 없음") + } + } + + private fun failResponse() { + userInfoStatus.value = AuthState(isSuccess = false, message = "회원 정보 조회 실패") } fun logout() {