generated from NOW-SOPT-ANDROID/now-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1938b2e
commit 18d2d22
Showing
2 changed files
with
103 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
app/src/main/java/com/sopt/now/compose/presentation/UserInfoViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.sopt.now.compose.presentation | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import com.google.gson.Gson | ||
import com.sopt.now.compose.data.BaseState | ||
import com.sopt.now.compose.data.ServicePool | ||
import com.sopt.now.compose.data.dto.response.ResponseAuthDto | ||
import com.sopt.now.compose.data.dto.response.ResponseUserInfoDto | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
|
||
class UserInfoViewModel : ViewModel() { | ||
private val userService by lazy { ServicePool.userService } | ||
val liveData = MutableLiveData<BaseState>() | ||
val userInfoLiveData = MutableLiveData<ResponseUserInfoDto?>() | ||
|
||
fun userInfo() { | ||
userService.userInfo().enqueue(object : Callback<ResponseUserInfoDto> { | ||
override fun onResponse( | ||
call: Call<ResponseUserInfoDto>, | ||
response: Response<ResponseUserInfoDto>, | ||
) { | ||
if (response.isSuccessful) { | ||
val data: ResponseUserInfoDto? = response.body() | ||
val userId = response.headers()["location"] | ||
userInfoLiveData.value = data | ||
|
||
liveData.value = BaseState( | ||
isSuccess = true, | ||
message = response.message() | ||
) | ||
Log.d("UserInfo", "data: $data, userId: $userId") | ||
} else { | ||
val error = response.errorBody()?.string() | ||
val gson = Gson() | ||
try { | ||
val errorResponse = gson.fromJson(error, ResponseAuthDto::class.java) | ||
liveData.value = BaseState( | ||
isSuccess = false, | ||
message = "회원 정보 조회 실패: ${errorResponse.message}" // 에러 메시지 사용 | ||
) | ||
} catch (e: Exception) { | ||
liveData.value = BaseState( | ||
isSuccess = false, | ||
message = "회원 정보 조회 실패: 에러 메시지 파싱 실패" | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<ResponseUserInfoDto>, t: Throwable) { | ||
liveData.value = BaseState( | ||
isSuccess = false, | ||
message = "서버 에러" | ||
) | ||
} | ||
}) | ||
} | ||
} |