feat(gift): 선물함 목록 어댑터를 추가한다
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package kr.co.vividnext.sodalive.v2.gift.ui
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.viewbinding.ViewBinding
|
||||
import coil.load
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.databinding.ItemGiftInboxAddressBannerBinding
|
||||
import kr.co.vividnext.sodalive.databinding.ItemGiftInboxEmptyReceivedBinding
|
||||
import kr.co.vividnext.sodalive.databinding.ItemGiftInboxFooterBinding
|
||||
import kr.co.vividnext.sodalive.databinding.ItemGiftInboxNoticeBannerBinding
|
||||
import kr.co.vividnext.sodalive.databinding.ItemGiftInboxReceivedBinding
|
||||
import kr.co.vividnext.sodalive.databinding.ItemGiftInboxSentBinding
|
||||
import kr.co.vividnext.sodalive.v2.gift.GiftInboxCardUiModel
|
||||
import kr.co.vividnext.sodalive.v2.gift.GiftInboxRow
|
||||
import kr.co.vividnext.sodalive.v2.gift.GiftInboxRowType
|
||||
|
||||
class GiftInboxAdapter(
|
||||
private val onCardClick: (GiftInboxCardUiModel) -> Unit,
|
||||
private val onInquiryClick: () -> Unit,
|
||||
private val onRetryClick: () -> Unit
|
||||
) : ListAdapter<GiftInboxRow, GiftInboxAdapter.Holder>(RowDiff) {
|
||||
class Holder(val binding: ViewBinding) : RecyclerView.ViewHolder(binding.root)
|
||||
|
||||
override fun getItemViewType(position: Int) = getItem(position).type.ordinal
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
|
||||
val inflater = LayoutInflater.from(parent.context)
|
||||
val binding = when (GiftInboxRowType.entries[viewType]) {
|
||||
GiftInboxRowType.NOTICE -> ItemGiftInboxNoticeBannerBinding.inflate(inflater, parent, false)
|
||||
GiftInboxRowType.ADDRESS -> ItemGiftInboxAddressBannerBinding.inflate(inflater, parent, false)
|
||||
GiftInboxRowType.SENT -> ItemGiftInboxSentBinding.inflate(inflater, parent, false)
|
||||
GiftInboxRowType.RECEIVED -> ItemGiftInboxReceivedBinding.inflate(inflater, parent, false)
|
||||
GiftInboxRowType.EMPTY_RECEIVED -> ItemGiftInboxEmptyReceivedBinding.inflate(inflater, parent, false).also {
|
||||
it.root.minimumHeight = parent.height - parent.paddingTop - parent.paddingBottom
|
||||
}
|
||||
GiftInboxRowType.INQUIRY, GiftInboxRowType.LOADING, GiftInboxRowType.RETRY ->
|
||||
ItemGiftInboxFooterBinding.inflate(inflater, parent, false)
|
||||
}
|
||||
return Holder(binding)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: Holder, position: Int) {
|
||||
val row = getItem(position)
|
||||
val binding = holder.binding
|
||||
val context = binding.root.context
|
||||
when (binding) {
|
||||
is ItemGiftInboxNoticeBannerBinding -> {
|
||||
val received = (row as GiftInboxRow.Notice).received
|
||||
binding.title.setText(
|
||||
if (received) R.string.gift_inbox_received_notice_title else R.string.gift_inbox_sent_notice_title
|
||||
)
|
||||
binding.body.setText(
|
||||
if (received) R.string.gift_inbox_received_notice_body else R.string.gift_inbox_sent_notice_body
|
||||
)
|
||||
}
|
||||
is ItemGiftInboxAddressBannerBinding -> {
|
||||
binding.root.setOnClickListener { onCardClick((row as GiftInboxRow.Address).card) }
|
||||
}
|
||||
is ItemGiftInboxSentBinding -> {
|
||||
val card = (row as GiftInboxRow.Card).card
|
||||
binding.counterpartNickname.text = card.counterpartNickname
|
||||
binding.counterpartProfileImage.load(card.counterpartProfileImageUrl ?: R.drawable.ic_placeholder_profile) {
|
||||
placeholder(R.drawable.ic_placeholder_profile)
|
||||
fallback(R.drawable.ic_placeholder_profile)
|
||||
error(R.drawable.ic_placeholder_profile)
|
||||
}
|
||||
binding.status.text = card.status.resId?.let(context::getString) ?: card.status.message
|
||||
binding.date.isVisible = card.createdDate.isNotEmpty()
|
||||
binding.date.text = context.getString(card.dateLabelRes, card.createdDate)
|
||||
binding.details.text = context.getString(R.string.gift_inbox_details, card.sizeName, card.categoryName)
|
||||
binding.root.setOnClickListener { onCardClick(card) }
|
||||
}
|
||||
is ItemGiftInboxReceivedBinding -> {
|
||||
val card = (row as GiftInboxRow.Card).card
|
||||
binding.counterpartNickname.text = card.counterpartNickname
|
||||
binding.counterpartProfileImage.load(card.counterpartProfileImageUrl ?: R.drawable.ic_placeholder_profile) {
|
||||
placeholder(R.drawable.ic_placeholder_profile)
|
||||
fallback(R.drawable.ic_placeholder_profile)
|
||||
error(R.drawable.ic_placeholder_profile)
|
||||
}
|
||||
binding.status.text = card.status.resId?.let(context::getString) ?: card.status.message
|
||||
binding.date.isVisible = card.createdDate.isNotEmpty()
|
||||
binding.date.text = context.getString(card.dateLabelRes, card.createdDate)
|
||||
binding.subtitle.isVisible = card.subtitleRes != null
|
||||
binding.subtitle.text = card.subtitleRes?.let(context::getString)
|
||||
binding.root.setOnClickListener { onCardClick(card) }
|
||||
}
|
||||
is ItemGiftInboxEmptyReceivedBinding -> binding.feedback.setOnClickListener { onInquiryClick() }
|
||||
is ItemGiftInboxFooterBinding -> {
|
||||
binding.progress.isVisible = row == GiftInboxRow.Loading
|
||||
binding.action.isVisible = row != GiftInboxRow.Loading
|
||||
binding.action.setText(if (row == GiftInboxRow.Retry) R.string.gift_inbox_retry else R.string.gift_inbox_inquiry)
|
||||
binding.action.setOnClickListener {
|
||||
if (row == GiftInboxRow.Retry) onRetryClick() else onInquiryClick()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private object RowDiff : DiffUtil.ItemCallback<GiftInboxRow>() {
|
||||
override fun areItemsTheSame(oldItem: GiftInboxRow, newItem: GiftInboxRow): Boolean {
|
||||
if (oldItem.type != newItem.type) return false
|
||||
return when {
|
||||
oldItem is GiftInboxRow.Card && newItem is GiftInboxRow.Card ->
|
||||
oldItem.card.applicationNo == newItem.card.applicationNo
|
||||
oldItem is GiftInboxRow.Notice && newItem is GiftInboxRow.Notice -> oldItem.received == newItem.received
|
||||
oldItem is GiftInboxRow.Address && newItem is GiftInboxRow.Address ->
|
||||
oldItem.card.applicationNo == newItem.card.applicationNo
|
||||
else -> true
|
||||
}
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: GiftInboxRow, newItem: GiftInboxRow) = oldItem == newItem
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user