Skip to content

Commit

Permalink
feat/#9: 회원 정보 조회 서버 통신 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
youjin09222 committed May 3, 2024
1 parent ad0d55f commit 1e0e58d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.sopt.now.test.presentation

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.sopt.now.databinding.FragmentMypageBinding
import com.sopt.now.test.data.UserData
import com.sopt.now.test.data.UserPreference

class MyPageFragment : Fragment() {
private var _binding: FragmentMypageBinding? = null
private val binding: FragmentMypageBinding
get() = requireNotNull(_binding) { "바인딩 객체 좀 생성해주세요 제발!!" }

private val viewModel by viewModels<UserInfoViewModel>()
private lateinit var userPreference: UserPreference

override fun onCreateView(
Expand All @@ -27,7 +31,26 @@ class MyPageFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
userPreference = UserPreference(requireContext())
setupUserData()
initObserver()
}

private fun initObserver() {

viewModel.userInfo()

viewModel.userInfoLiveData.observe(requireActivity()) { userData ->
Log.d("userPreference before", "${userPreference.getUserData()}")
userData?.let {
val userData = UserData(
userId = userData.data.authenticationId,
userName = userData.data.nickname,
userPhone = userData.data.phone
)
userPreference.saveUserData(userData)
setupUserData()
Log.d("userPreference after", "${userPreference.getUserData()}")
}
}
}

// 받아온 UserData 적용
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.sopt.now.test.presentation

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.google.gson.Gson
import com.sopt.now.test.data.ServicePool
import com.sopt.now.test.data.BaseState
import com.sopt.now.test.data.dto.response.ResponseAuthDto
import com.sopt.now.test.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 = "서버 에러"
)
}
})
}
}

0 comments on commit 1e0e58d

Please sign in to comment.