diff --git a/app/src/main/java/com/sopt/now/test/presentation/MyPageFragment.kt b/app/src/main/java/com/sopt/now/test/presentation/MyPageFragment.kt index 2c6e3c9..73ce543 100644 --- a/app/src/main/java/com/sopt/now/test/presentation/MyPageFragment.kt +++ b/app/src/main/java/com/sopt/now/test/presentation/MyPageFragment.kt @@ -1,11 +1,14 @@ 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() { @@ -13,6 +16,7 @@ class MyPageFragment : Fragment() { private val binding: FragmentMypageBinding get() = requireNotNull(_binding) { "바인딩 객체 좀 생성해주세요 제발!!" } + private val viewModel by viewModels() private lateinit var userPreference: UserPreference override fun onCreateView( @@ -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 적용 diff --git a/app/src/main/java/com/sopt/now/test/presentation/UserInfoViewModel.kt b/app/src/main/java/com/sopt/now/test/presentation/UserInfoViewModel.kt new file mode 100644 index 0000000..ce78cdc --- /dev/null +++ b/app/src/main/java/com/sopt/now/test/presentation/UserInfoViewModel.kt @@ -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() + val userInfoLiveData = MutableLiveData() + + fun userInfo() { + userService.userInfo().enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response, + ) { + 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, t: Throwable) { + liveData.value = BaseState( + isSuccess = false, + message = "서버 에러" + ) + } + }) + } +} \ No newline at end of file