From 8abce2d195364e720ae0a6480421c103e434b5c3 Mon Sep 17 00:00:00 2001 From: kimdowoo Date: Sun, 9 Apr 2023 17:48:17 +0900 Subject: [PATCH] =?UTF-8?q?[BUD-50]=20fix=20:=20Buddy=20=EC=9D=B4=EB=AF=B8?= =?UTF-8?q?=EC=A7=80=20api=20=EA=B3=A0=ED=99=94=EC=A7=88=20=EC=9D=B4?= =?UTF-8?q?=EB=AF=B8=EC=A7=80=20=EC=97=85=EB=A1=9C=EB=93=9C=20=EC=95=88?= =?UTF-8?q?=EB=90=98=EB=8D=98=20=EB=B2=84=EA=B7=B8=20=ED=94=BD=EC=8A=A4(#3?= =?UTF-8?q?4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../buddyvet/buddy/BuddyListAdapter.kt | 31 ++++++++++++ .../buddy/HomeBuddyProfileListAdapter.kt | 50 +++++++++++++++++++ .../buddyvet/buddy/domain/entity/BuddyData.kt | 2 +- .../buddyvet/buddy/ui/BuddyViewModel.kt | 14 ++++-- .../buddy/ui/CompleteRegistrationFragment.kt | 9 ++-- .../buddyvet/buddy/ui/MyBuddyFragment.kt | 4 +- .../src/main/res/layout/item_buddy_detail.xml | 16 ++++-- .../res/layout/item_buddy_profile_home.xml | 48 ++++++++++++++++++ .../src/main/res/drawable/img_more_vert.xml | 5 ++ .../src/main/res/menu/common_menu.xml | 5 +- .../community/ui/CommunityViewModel.kt | 1 - .../community/ui/DetailPostFragment.kt | 6 +-- .../diary/ui/DetailDiaryFragment.kt | 14 ++++-- diary/src/main/res/menu/detail_diary_menu.xml | 15 ------ .../com/nocapstone/home/ui/HomeFragment.kt | 5 +- home/src/main/res/layout/fragment_home.xml | 2 +- 16 files changed, 183 insertions(+), 44 deletions(-) create mode 100644 buddy/src/main/java/com/nocapstone/buddyvet/buddy/HomeBuddyProfileListAdapter.kt create mode 100644 buddy/src/main/res/layout/item_buddy_profile_home.xml create mode 100644 common-ui/src/main/res/drawable/img_more_vert.xml rename community/src/main/res/menu/detail_community_menu.xml => common-ui/src/main/res/menu/common_menu.xml (82%) delete mode 100644 diary/src/main/res/menu/detail_diary_menu.xml diff --git a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/BuddyListAdapter.kt b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/BuddyListAdapter.kt index 43330e2..7e53f42 100644 --- a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/BuddyListAdapter.kt +++ b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/BuddyListAdapter.kt @@ -1,14 +1,23 @@ package com.nocapstone.buddyvet.buddy import android.view.LayoutInflater +import android.view.MenuItem +import android.view.View import android.view.ViewGroup +import androidx.annotation.MenuRes +import androidx.appcompat.widget.PopupMenu +import androidx.fragment.app.Fragment +import androidx.navigation.fragment.findNavController import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView import com.nocapstone.buddyvet.buddy.databinding.ItemBuddyDetailBinding import com.nocapstone.buddyvet.buddy.domain.entity.BuddyData +import com.nocapstone.buddyvet.buddy.ui.MyBuddyFragment +import com.nocapstone.common_ui.R.menu.common_menu class BuddyListAdapter( + val fragment: Fragment ) : ListAdapter(BuddyListDiffCallback()) { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { return BuddyListViewHolder( @@ -32,8 +41,30 @@ class BuddyListAdapter( fun bind(data: BuddyData, position: Int) { binding.apply { buddyData = data + more.setOnClickListener { + val id = getItem(position).id + showMenu(it, common_menu,id) + } } } + + private fun showMenu(v: View, @MenuRes menuRes: Int, id: Long) { + val popup = PopupMenu(fragment.requireContext(), v) + popup.menuInflater.inflate(menuRes, popup.menu) + + popup.setOnMenuItemClickListener { menuItem: MenuItem -> + when(menuItem.itemId) { + com.nocapstone.common_ui.R.id.delete -> { + (fragment as MyBuddyFragment).buddyViewModel.deleteBuddy(id) + } + com.nocapstone.common_ui.R.id.put -> { + } + } + true + } + // Show the popup menu. + popup.show() + } } } diff --git a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/HomeBuddyProfileListAdapter.kt b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/HomeBuddyProfileListAdapter.kt new file mode 100644 index 0000000..63cac97 --- /dev/null +++ b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/HomeBuddyProfileListAdapter.kt @@ -0,0 +1,50 @@ +package com.nocapstone.buddyvet.buddy + +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.DiffUtil +import androidx.recyclerview.widget.ListAdapter +import androidx.recyclerview.widget.RecyclerView +import com.nocapstone.buddyvet.buddy.databinding.ItemBuddyProfileBinding +import com.nocapstone.buddyvet.buddy.databinding.ItemBuddyProfileHomeBinding +import com.nocapstone.buddyvet.buddy.domain.entity.BuddyData + +class HomeBuddyProfileListAdapter() : + ListAdapter(HomeBuddyProfileListDiffCallback()) { + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { + return BuddyListViewHolder( + ItemBuddyProfileHomeBinding.inflate( + LayoutInflater.from(parent.context), + parent, + false + ) + ) + } + + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + if (holder is BuddyListViewHolder) { + val buddyData = getItem(position) + holder.bind(buddyData, position) + } + } + + inner class BuddyListViewHolder(private val binding: ItemBuddyProfileHomeBinding) : + RecyclerView.ViewHolder(binding.root) { + fun bind(data: BuddyData, position: Int) { + + binding.apply { + buddyData = data + } + } + } +} + +private class HomeBuddyProfileListDiffCallback : DiffUtil.ItemCallback() { + override fun areItemsTheSame(oldItem: BuddyData, newItem: BuddyData): Boolean { + return oldItem === newItem + } + + override fun areContentsTheSame(oldItem: BuddyData, newItem: BuddyData): Boolean { + return oldItem == newItem + } +} \ No newline at end of file diff --git a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/domain/entity/BuddyData.kt b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/domain/entity/BuddyData.kt index c2a0b1a..0cf1bb6 100644 --- a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/domain/entity/BuddyData.kt +++ b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/domain/entity/BuddyData.kt @@ -5,7 +5,7 @@ data class BuddyListResponse( ) data class BuddyData( - val id: Int, + val id: Long, val kind: String, val name: String, val gender: String, diff --git a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/BuddyViewModel.kt b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/BuddyViewModel.kt index 88e6104..c7d52f0 100644 --- a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/BuddyViewModel.kt +++ b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/BuddyViewModel.kt @@ -13,7 +13,9 @@ import com.nocapstone.buddyvet.buddy.domain.usecase.BuddyUseCase import com.nocapstone.common.domain.usecase.DataStoreUseCase import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.qualifiers.ApplicationContext +import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.first @@ -92,14 +94,15 @@ class BuddyViewModel @Inject constructor( } } - fun createBuddy() { - viewModelScope.launch { + fun createBuddy(callback: () -> Unit) { + viewModelScope.launch(Dispatchers.IO) { try { val jwt = dataStoreUseCase.bearerJsonWebToken.first()!! val buddyId = buddyUseCase.createBuddy(jwt, _newBuddy.value!!) if (_selectImgUri.value != null) { - uploadBuddyImg(jwt, buddyId) + uploadBuddyImg(jwt, buddyId).await() } + callback.invoke() } catch (e: Exception) { Log.d("createBuddy", e.message.toString()) } @@ -117,11 +120,12 @@ class BuddyViewModel @Inject constructor( viewModelScope.launch { val jwt = dataStoreUseCase.bearerJsonWebToken.first()!! buddyUseCase.deleteBuddy(jwt, buddyId) + readBuddyList() } } - private fun uploadBuddyImg(token: String, buddyId: Long) { - viewModelScope.launch { + private fun uploadBuddyImg(token: String, buddyId: Long) : Deferred { + return viewModelScope.async(Dispatchers.IO) { val imgUri = _selectImgUri.value!! val inputStream = context.contentResolver.openInputStream(imgUri) val byteArray = inputStream!!.readBytes() diff --git a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/CompleteRegistrationFragment.kt b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/CompleteRegistrationFragment.kt index 009059b..95cadc2 100644 --- a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/CompleteRegistrationFragment.kt +++ b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/CompleteRegistrationFragment.kt @@ -6,9 +6,11 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.viewModels +import androidx.lifecycle.lifecycleScope import com.nocapstone.buddyvet.buddy.databinding.FragmentCompleteRegistrationBinding import com.nocapstone.common.util.LoginUtil import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.launch import javax.inject.Inject @AndroidEntryPoint @@ -39,12 +41,13 @@ class CompleteRegistrationFragment : Fragment() { } binding.next.setOnClickListener { - buddyViewModel.createBuddy() - LoginUtil.startMainActivity(requireActivity(), mainActivityClass) + buddyViewModel.createBuddy(){ + LoginUtil.startMainActivity(requireActivity(), mainActivityClass) + } } - } + override fun onDestroyView() { super.onDestroyView() _binding = null diff --git a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/MyBuddyFragment.kt b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/MyBuddyFragment.kt index f4d4c18..a54bc19 100644 --- a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/MyBuddyFragment.kt +++ b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/MyBuddyFragment.kt @@ -20,7 +20,7 @@ class MyBuddyFragment : Fragment() { private var _binding: FragmentMyBuddyBinding? = null private val binding get() = _binding!! - private val buddyViewModel: BuddyViewModel by viewModels({ requireActivity() }) + val buddyViewModel: BuddyViewModel by viewModels({ requireActivity() }) override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -46,7 +46,7 @@ class MyBuddyFragment : Fragment() { binding.apply { lifecycleOwner = viewLifecycleOwner viewModel = buddyViewModel - adapter = BuddyListAdapter() + adapter = BuddyListAdapter(this@MyBuddyFragment) } } diff --git a/buddy/src/main/res/layout/item_buddy_detail.xml b/buddy/src/main/res/layout/item_buddy_detail.xml index 911c80b..486c680 100644 --- a/buddy/src/main/res/layout/item_buddy_detail.xml +++ b/buddy/src/main/res/layout/item_buddy_detail.xml @@ -11,11 +11,11 @@ + app:cardElevation="1dp"> + + diff --git a/buddy/src/main/res/layout/item_buddy_profile_home.xml b/buddy/src/main/res/layout/item_buddy_profile_home.xml new file mode 100644 index 0000000..83e0071 --- /dev/null +++ b/buddy/src/main/res/layout/item_buddy_profile_home.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/common-ui/src/main/res/drawable/img_more_vert.xml b/common-ui/src/main/res/drawable/img_more_vert.xml new file mode 100644 index 0000000..1983df1 --- /dev/null +++ b/common-ui/src/main/res/drawable/img_more_vert.xml @@ -0,0 +1,5 @@ + + + diff --git a/community/src/main/res/menu/detail_community_menu.xml b/common-ui/src/main/res/menu/common_menu.xml similarity index 82% rename from community/src/main/res/menu/detail_community_menu.xml rename to common-ui/src/main/res/menu/common_menu.xml index db4fffe..161a46d 100644 --- a/community/src/main/res/menu/detail_community_menu.xml +++ b/common-ui/src/main/res/menu/common_menu.xml @@ -1,14 +1,13 @@ - diff --git a/community/src/main/java/com/nocapstone/community/ui/CommunityViewModel.kt b/community/src/main/java/com/nocapstone/community/ui/CommunityViewModel.kt index 63eba93..1739076 100644 --- a/community/src/main/java/com/nocapstone/community/ui/CommunityViewModel.kt +++ b/community/src/main/java/com/nocapstone/community/ui/CommunityViewModel.kt @@ -52,7 +52,6 @@ class CommunityViewModel @Inject constructor( communityUseCase.readPostList(dataStoreUseCase.bearerJsonWebToken.first()!!).let { _postList.value = it.toMutableList() } - val tmp = mutableListOf() } catch (e: Exception) { } diff --git a/community/src/main/java/com/nocapstone/community/ui/DetailPostFragment.kt b/community/src/main/java/com/nocapstone/community/ui/DetailPostFragment.kt index 3e5ebc4..bd7c44f 100644 --- a/community/src/main/java/com/nocapstone/community/ui/DetailPostFragment.kt +++ b/community/src/main/java/com/nocapstone/community/ui/DetailPostFragment.kt @@ -73,7 +73,7 @@ class DetailPostFragment : Fragment() { menuHost.addMenuProvider(object : MenuProvider { override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { - menuInflater.inflate(R.menu.detail_community_menu, menu) + menuInflater.inflate(com.nocapstone.common_ui.R.menu.common_menu, menu) } override fun onMenuItemSelected(menuItem: MenuItem): Boolean { @@ -81,10 +81,10 @@ class DetailPostFragment : Fragment() { android.R.id.home -> { findNavController().popBackStack() } - R.id.put_post -> { + com.nocapstone.common_ui.R.id.put -> { } - R.id.delete_post -> { + com.nocapstone.common_ui.R.id.delete -> { MaterialAlertDialogBuilder(requireContext()) .setTitle("글 삭제") .setMessage("정말 삭제하시겠습니까") diff --git a/diary/src/main/java/com/nocapstone/diary/ui/DetailDiaryFragment.kt b/diary/src/main/java/com/nocapstone/diary/ui/DetailDiaryFragment.kt index a7dfe77..ee95349 100644 --- a/diary/src/main/java/com/nocapstone/diary/ui/DetailDiaryFragment.kt +++ b/diary/src/main/java/com/nocapstone/diary/ui/DetailDiaryFragment.kt @@ -17,6 +17,7 @@ import com.nocapstone.common_ui.MainActivityUtil import com.nocapstone.common_ui.ToastSet import com.nocapstone.common_ui.ToastType import dagger.hilt.android.AndroidEntryPoint +import com.nocapstone.common_ui.R.menu.common_menu @AndroidEntryPoint @@ -65,7 +66,7 @@ class DetailDiaryFragment : Fragment() { menuHost.addMenuProvider(object : MenuProvider { override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { - menuInflater.inflate(R.menu.detail_diary_menu, menu) + menuInflater.inflate(common_menu, menu) } override fun onMenuItemSelected(menuItem: MenuItem): Boolean { @@ -73,14 +74,14 @@ class DetailDiaryFragment : Fragment() { android.R.id.home -> { findNavController().popBackStack() } - R.id.put_diary -> { + com.nocapstone.common_ui.R.id.put -> { findNavController().navigate( DetailDiaryFragmentDirections.putDiaryFragment( diaryId ) ) } - R.id.delete_diary -> { + com.nocapstone.common_ui.R.id.delete -> { MaterialAlertDialogBuilder(requireContext()) .setTitle("일기 삭제") .setMessage("정말 삭제하시겠습니까?") @@ -88,7 +89,12 @@ class DetailDiaryFragment : Fragment() { dialog.dismiss() }.setPositiveButton("삭제") { dialog, which -> diaryViewModel.deleteDiary(diaryId) - diaryViewModel.setToastMessage(ToastSet("일기 삭제 완료",ToastType.SUCCESS)) + diaryViewModel.setToastMessage( + ToastSet( + "일기 삭제 완료", + ToastType.SUCCESS + ) + ) dialog.dismiss() findNavController().popBackStack() }.show() diff --git a/diary/src/main/res/menu/detail_diary_menu.xml b/diary/src/main/res/menu/detail_diary_menu.xml deleted file mode 100644 index 490de32..0000000 --- a/diary/src/main/res/menu/detail_diary_menu.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/home/src/main/java/com/nocapstone/home/ui/HomeFragment.kt b/home/src/main/java/com/nocapstone/home/ui/HomeFragment.kt index 3dac574..6bfb05f 100644 --- a/home/src/main/java/com/nocapstone/home/ui/HomeFragment.kt +++ b/home/src/main/java/com/nocapstone/home/ui/HomeFragment.kt @@ -8,6 +8,7 @@ import android.view.ViewGroup import androidx.fragment.app.viewModels import androidx.navigation.fragment.findNavController import com.nocapstone.buddyvet.buddy.BuddyProfileListAdapter +import com.nocapstone.buddyvet.buddy.HomeBuddyProfileListAdapter import com.nocapstone.buddyvet.buddy.ui.BuddyViewModel import com.nocapstone.common_ui.MainActivityUtil import com.nocapstone.eye_check.ui.EyeCheckViewModel @@ -32,9 +33,7 @@ class HomeFragment : Fragment() { binding.apply { lifecycleOwner = viewLifecycleOwner - adapter = BuddyProfileListAdapter(false) { - buddyViewModel.setSelectCheckBuddy(it) - } + adapter = HomeBuddyProfileListAdapter() viewModel = buddyViewModel buddySetting.setOnClickListener { findNavController().navigate(R.id.toBuddy) diff --git a/home/src/main/res/layout/fragment_home.xml b/home/src/main/res/layout/fragment_home.xml index d0235a3..434cfe2 100644 --- a/home/src/main/res/layout/fragment_home.xml +++ b/home/src/main/res/layout/fragment_home.xml @@ -12,7 +12,7 @@ + type="com.nocapstone.buddyvet.buddy.HomeBuddyProfileListAdapter" />