From cf8a14b4e2835f966a1005410d1aef73b64ca013 Mon Sep 17 00:00:00 2001 From: youjin09222 Date: Fri, 7 Jun 2024 08:24:41 +0900 Subject: [PATCH] =?UTF-8?q?refactor/#9=20:=20HomeFragment=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EB=B7=B0=EB=AA=A8=EB=8D=B8=201=EA=B0=9C=EB=A7=8C?= =?UTF-8?q?=20=ED=98=B8=EC=B6=9C=20=EB=B0=8F=20UiState=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../now/test/presentation/HomeFragment.kt | 88 ------------------ .../test/presentation/home/HomeFragment.kt | 92 +++++++++++++++++++ 2 files changed, 92 insertions(+), 88 deletions(-) delete mode 100644 app/src/main/java/com/sopt/now/test/presentation/HomeFragment.kt create mode 100644 app/src/main/java/com/sopt/now/test/presentation/home/HomeFragment.kt diff --git a/app/src/main/java/com/sopt/now/test/presentation/HomeFragment.kt b/app/src/main/java/com/sopt/now/test/presentation/HomeFragment.kt deleted file mode 100644 index 7536af9..0000000 --- a/app/src/main/java/com/sopt/now/test/presentation/HomeFragment.kt +++ /dev/null @@ -1,88 +0,0 @@ -package com.sopt.now.test.presentation - -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.Toast -import androidx.fragment.app.Fragment -import androidx.fragment.app.viewModels -import com.sopt.now.R -import com.sopt.now.databinding.FragmentHomeBinding -import com.sopt.now.test.data.Profile -import com.sopt.now.test.friend.FriendAdapter - -class HomeFragment: Fragment() { - private var _binding: FragmentHomeBinding? = null - - private val binding: FragmentHomeBinding - get() = requireNotNull(_binding) { "바인딩 객체 좀 생성해주세요 제발!!" } - - private val friendViewModel by viewModels() - private val userViewModel by viewModels() - var userList = mutableListOf() - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - _binding = FragmentHomeBinding.inflate(inflater, container, false) - return binding.root - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - - setFriendProfile() - setUserProfile() - } - - // 사용자 프로필 설정 - private fun setUserProfile() { - userViewModel.userInfoLiveData.observe(requireActivity()) { userInfo -> - userInfo?.let { - val userProfile = Profile( - userImage = R.drawable.iv_user_profile.toString(), - userName = userInfo.data.nickname, - userInfo = userInfo.data.phone, - ) - userList.add(0, userProfile) - } - } - } - - // 친구 프로필 설정 - private fun setFriendProfile() { - friendViewModel.friendInfoLiveData.observe(requireActivity()) { friendInfo -> - friendInfo?.let { - friendInfo.data.forEach { friendData -> - val friend = Profile( - userImage = friendData.avatar, - userName = friendData.firstName, - userInfo = friendData.email - ) - userList.add(1, friend) - } - setRecyclerView() - } - } - } - - // FriendAdapter 연결 - private fun setRecyclerView(){ - try { - val friendAdapter = FriendAdapter(userList) - binding.rvFriends.run { - setAdapter(friendAdapter) - } - } catch (e: IllegalArgumentException) { - Toast.makeText(requireContext(), "Error: ${e.message}", Toast.LENGTH_SHORT).show() - } - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } -} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/test/presentation/home/HomeFragment.kt b/app/src/main/java/com/sopt/now/test/presentation/home/HomeFragment.kt new file mode 100644 index 0000000..bfcbf68 --- /dev/null +++ b/app/src/main/java/com/sopt/now/test/presentation/home/HomeFragment.kt @@ -0,0 +1,92 @@ +package com.sopt.now.test.presentation.home + +import android.os.Bundle +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.R +import com.sopt.now.databinding.FragmentHomeBinding +import com.sopt.now.test.core.util.fragment.showToast +import com.sopt.now.test.core.view.UiState +import com.sopt.now.test.data.Profile +import timber.log.Timber + +class HomeFragment : Fragment() { + private var _binding: FragmentHomeBinding? = null + private val binding: FragmentHomeBinding + get() = requireNotNull(_binding) { "바인딩 객체 좀 생성해주세요 제발!!" } + + private val userProfileViewModel by viewModels() + private val userList = mutableListOf() + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentHomeBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + initGetUserObserver() + initGetFriendObserver() + } + + // 사용자 프로필 설정 + private fun initGetUserObserver() { + userProfileViewModel.getUserInfoProfileLiveData.observe(viewLifecycleOwner) { + when (it) { + is UiState.Success -> { + val userProfile = Profile( + userImage = R.drawable.iv_user_profile.toString(), + userName = it.data.data.nickname, + userInfo = it.data.data.phone, + ) + userList.add(0, userProfile) + setRecyclerView() + } + + is UiState.Failure -> showToast(it.errorMessage) + is UiState.Loading -> Timber.d(getString(R.string.message_loading)) + } + } + } + + // 친구 프로필 설정 + private fun initGetFriendObserver() { + userProfileViewModel.getFriendProfileLiveData.observe(viewLifecycleOwner) { + when (it) { + is UiState.Success -> { + val friendProfiles = it.data.data.map { friend -> + Profile( + userImage = friend.avatar, + userName = friend.firstName, + userInfo = friend.email + ) + } + // 2번째부터 넣음 + userList.addAll(1, friendProfiles) + setRecyclerView() + } + + is UiState.Failure -> showToast(it.errorMessage) + is UiState.Loading -> Timber.d(getString(R.string.message_loading)) + } + } + } + + // UserProfileAdapter 연결 + private fun setRecyclerView() { + val friendAdapter = UserProfileAdapter(userList) + binding.rvFriends.adapter = friendAdapter + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} \ No newline at end of file