Skip to content

Commit

Permalink
[fix/#138] 관심사 추가/삭제 시 변경된 currentList를 콜백으로 받아와 외부에서 참조 가능
Browse files Browse the repository at this point in the history
  • Loading branch information
unam98 committed Feb 12, 2024
1 parent a949250 commit 0c5a36e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.teumteum.teumteum.util.callback

interface OnCurrentListChangedListener<T> {
fun onCurrentListChanged(previousList: List<T>, currentList: List<T>)
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import androidx.cardview.widget.CardView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.teumteum.teumteum.R
import com.teumteum.teumteum.util.callback.OnDeletedInterests
import com.teumteum.teumteum.util.callback.OnCurrentListChangedListener
import com.teumteum.teumteum.util.custom.itemdecoration.FlexboxItemDecoration
import com.teumteum.teumteum.util.custom.view.adapter.InterestAdapter
import com.teumteum.teumteum.util.custom.view.model.BackCard
Expand All @@ -31,7 +32,7 @@ import timber.log.Timber
*
* xml, compose 모든 환경에서 뷰를 재활용 할 수 있게 커스텀뷰로 제작
*/
class BackCardView : CardView, OnDeletedInterests {
class BackCardView : CardView, OnCurrentListChangedListener<Interest> {
private val layoutParent = ConstraintLayout.LayoutParams.PARENT_ID
private var backCard = BackCard()

Expand All @@ -55,7 +56,7 @@ class BackCardView : CardView, OnDeletedInterests {
ivEditGoalContent.visibility = if (value) View.VISIBLE else View.INVISIBLE
}

var currentListAfterDelete = mutableListOf<Interest>()
var currentList = MutableLiveData<MutableList<Interest>>()

// isModifyDetail 값을 설정하고 어댑터에 UI 갱신을 알리는 함수
@SuppressLint("NotifyDataSetChanged")
Expand Down Expand Up @@ -422,8 +423,10 @@ class BackCardView : CardView, OnDeletedInterests {
this.addView(rvInterests)
}

override fun deletedInterests(deletedInterests: MutableList<Interest>) {
currentListAfterDelete = deletedInterests.filterNot { it.interest == "추가하기" }.toMutableList()
Timber.tag("삭제 후 관심사 리스트").d("${currentListAfterDelete}")
override fun onCurrentListChanged(previousList: List<Interest>, currentList: List<Interest>) {
Timber.tag("갱신 리스트 p").d("${previousList}")
Timber.tag("갱신 리스트 c").d("${currentList}")
this.currentList.value = currentList.filterNot { it.interest == "추가하기" }.toMutableList()
Timber.tag("currentList").d("${this.currentList.value}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.teumteum.teumteum.R
import com.teumteum.teumteum.databinding.ItemInterestBinding
import com.teumteum.teumteum.util.callback.OnDeletedInterests
import com.teumteum.teumteum.util.callback.OnCurrentListChangedListener
import com.teumteum.teumteum.util.custom.view.model.Interest
import timber.log.Timber

class InterestAdapter(val context: Context, val onDeletedInterests: OnDeletedInterests) : ListAdapter<Interest, InterestAdapter.ItemViewHolder>(
class InterestAdapter(
val context: Context,
var onCurrentListChangedListener: OnCurrentListChangedListener<Interest>,
) : ListAdapter<Interest, InterestAdapter.ItemViewHolder>(
ItemListDiffCallback
) {
var isModifyDetail: Boolean = false
var onAddItemClick: (() -> Unit)? = null

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
val binding = ItemInterestBinding.inflate(LayoutInflater.from(parent.context), parent, false)
val binding =
ItemInterestBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ItemViewHolder(binding, isModifyDetail, onAddItemClick) { position ->
if (currentList.size > 2) {
removeItem(position)
Expand All @@ -37,31 +40,49 @@ class InterestAdapter(val context: Context, val onDeletedInterests: OnDeletedInt
holder.bind(item)
}

override fun onCurrentListChanged(
previousList: MutableList<Interest>,
currentList: MutableList<Interest>,
) {
super.onCurrentListChanged(previousList, currentList)
onCurrentListChangedListener?.onCurrentListChanged(previousList, currentList)
}

private fun removeItem(position: Int) {
val newList = currentList.toMutableList().apply {
removeAt(position)
}
onDeletedInterests.deletedInterests(newList)
submitList(newList)
}

class ItemViewHolder(
private val binding: ItemInterestBinding,
private val isModifyDetail: Boolean,
private val onAddItemClick: (() -> Unit)?,
private val onRemoveItem: (position: Int) -> Unit
private val onRemoveItem: (position: Int) -> Unit,
) : RecyclerView.ViewHolder(binding.root) {

fun bind(item: Interest) {
with(binding){
with(binding) {
if (item.interest == "추가하기") {
tvInterest.text = item.interest
tvInterest.setTextColor(ContextCompat.getColor(itemView.context, com.teumteum.base.R.color.text_button_primary_default))
tvInterest.setTextColor(
ContextCompat.getColor(
itemView.context,
com.teumteum.base.R.color.text_button_primary_default
)
)
clInterest.setBackgroundResource(R.drawable.shape_rect4_color_outline_level01_active)
ivDelete.setImageDrawable(ContextCompat.getDrawable(itemView.context,R.drawable.ic_plus_fill))
ivDelete.setImageDrawable(
ContextCompat.getDrawable(
itemView.context,
R.drawable.ic_plus_fill
)
)
root.setOnClickListener { onAddItemClick?.invoke() }
} else {
tvInterest.text = itemView.context.getString(R.string.item_interest, item.interest)
tvInterest.text =
itemView.context.getString(R.string.item_interest, item.interest)
clInterest.setBackgroundResource(R.drawable.shape_rect4_background)
root.setOnClickListener(null)
ivDelete.setOnClickListener { onRemoveItem(absoluteAdapterPosition) }
Expand Down

0 comments on commit 0c5a36e

Please sign in to comment.