Skip to content

Commit

Permalink
[BUD-50] fix : Buddy 이미지 api 고화질 이미지 업로드 안되던 버그 픽스(#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
KDW03 committed Apr 9, 2023
1 parent 7801eeb commit 8abce2d
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -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<BuddyData, RecyclerView.ViewHolder>(BuddyListDiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return BuddyListViewHolder(
Expand All @@ -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()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<BuddyData, RecyclerView.ViewHolder>(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<BuddyData>() {
override fun areItemsTheSame(oldItem: BuddyData, newItem: BuddyData): Boolean {
return oldItem === newItem
}

override fun areContentsTheSame(oldItem: BuddyData, newItem: BuddyData): Boolean {
return oldItem == newItem
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
Expand All @@ -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<Unit> {
return viewModelScope.async(Dispatchers.IO) {
val imgUri = _selectImgUri.value!!
val inputStream = context.contentResolver.openInputStream(imgUri)
val byteArray = inputStream!!.readBytes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand All @@ -46,7 +46,7 @@ class MyBuddyFragment : Fragment() {
binding.apply {
lifecycleOwner = viewLifecycleOwner
viewModel = buddyViewModel
adapter = BuddyListAdapter()
adapter = BuddyListAdapter(this@MyBuddyFragment)
}

}
Expand Down
16 changes: 13 additions & 3 deletions buddy/src/main/res/layout/item_buddy_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
</data>

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
app:cardCornerRadius="16dp"
android:layout_width="match_parent"
app:cardElevation="1dp"
android:layout_height="wrap_content">
app:cardElevation="1dp">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
Expand Down Expand Up @@ -62,6 +62,16 @@
app:layout_constraintStart_toStartOf="@id/kind"
app:layout_constraintTop_toBottomOf="@id/kind"
tools:text="여자 / 1살" />

<ImageView
android:id="@+id/more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:src="@drawable/img_more_vert"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

Expand Down
48 changes: 48 additions & 0 deletions buddy/src/main/res/layout/item_buddy_profile_home.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>

<import type="android.view.View" />

<variable
name="buddyData"
type="com.nocapstone.buddyvet.buddy.domain.entity.BuddyData" />

</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_marginHorizontal="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">


<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/buddy_profile"
photoUrl="@{buddyData.profile}"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
app:civ_border_overlay="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="8dp"
android:fontFamily="@font/cafe24_ssurround_air"
android:text="@{buddyData.name}"
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="@id/buddy_profile"
app:layout_constraintStart_toStartOf="@id/buddy_profile"
app:layout_constraintTop_toBottomOf="@id/buddy_profile"
tools:text="주주" />


</androidx.constraintlayout.widget.ConstraintLayout>

</layout>
5 changes: 5 additions & 0 deletions common-ui/src/main/res/drawable/img_more_vert.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="32dp" android:tint="#969696"
android:viewportHeight="24" android:viewportWidth="24"
android:width="32dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,8c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2zM12,10c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2zM12,16c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/put_post"
android:id="@+id/put"
android:title="수정"
app:showAsAction="never"
/>
<item
android:id="@+id/delete_post"
android:id="@+id/delete"
android:title="삭제"
app:showAsAction="never"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class CommunityViewModel @Inject constructor(
communityUseCase.readPostList(dataStoreUseCase.bearerJsonWebToken.first()!!).let {
_postList.value = it.toMutableList()
}
val tmp = mutableListOf<Post>()
} catch (e: Exception) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ 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 {
when (menuItem.itemId) {
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("정말 삭제하시겠습니까")
Expand Down
14 changes: 10 additions & 4 deletions diary/src/main/java/com/nocapstone/diary/ui/DetailDiaryFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,30 +66,35 @@ 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 {
when (menuItem.itemId) {
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("정말 삭제하시겠습니까?")
.setNegativeButton("취소") { dialog, which ->
dialog.dismiss()
}.setPositiveButton("삭제") { dialog, which ->
diaryViewModel.deleteDiary(diaryId)
diaryViewModel.setToastMessage(ToastSet("일기 삭제 완료",ToastType.SUCCESS))
diaryViewModel.setToastMessage(
ToastSet(
"일기 삭제 완료",
ToastType.SUCCESS
)
)
dialog.dismiss()
findNavController().popBackStack()
}.show()
Expand Down
Loading

0 comments on commit 8abce2d

Please sign in to comment.