Skip to content

Commit

Permalink
refactor/#9: initGetUserInfoObserver 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
youjin09222 committed May 24, 2024
1 parent 9873cc6 commit 54bbdf7
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions app/src/main/java/com/sopt/now/test/presentation/MyPageFragment.kt
Original file line number Diff line number Diff line change
@@ -1,23 +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
import com.sopt.now.test.core.view.UiState
import com.sopt.now.test.data.dto.response.ResponseUserInfoDto
import timber.log.Timber

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
private val userViewModel by viewModels<UserInfoViewModel>()

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -30,35 +29,26 @@ class MyPageFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
userPreference = UserPreference(requireContext())
initObserver()
initGetUserInfoObserver()
}

private fun initObserver() {
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()}")
// 사용자 프로필 설정
private fun initGetUserInfoObserver() {
userViewModel.getUserInfoLiveData.observe(requireActivity()) {
when (it) {
is UiState.Success -> initMyProfile(it.data)
is UiState.Failure -> Timber.d("실패 : $it")
is UiState.Loading -> Timber.d("로딩 중")
}
}
}

// 받아온 UserData 적용
private fun setupUserData() {
val userData = userPreference.getUserData()
if (userData != null) {
with(binding) {
tvMyId.text = userData.userId
tvMyName.text = userData.userName
tvMyPhone.text = userData.userPhone
}
private fun initMyProfile(userData : ResponseUserInfoDto) {
with(binding) {
tvMyId.text = userData.data.authenticationId
tvMyName.text = userData.data.nickname
tvMyPhone.text = userData.data.phone
}
}

Expand Down

0 comments on commit 54bbdf7

Please sign in to comment.