Skip to content

Commit

Permalink
Fix: 향후계획 시간표 과목 삭제 구현 중...
Browse files Browse the repository at this point in the history
  • Loading branch information
dkyuuum committed Feb 28, 2024
1 parent c40a805 commit 9304eb2
Showing 1 changed file with 81 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,87 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import umc.com.mobile.project.data.model.plan.TimeInfoResponse
import umc.com.mobile.project.data.model.plan.timetable.UpTimeResult
import umc.com.mobile.project.databinding.ItemPlanTimeBinding
import umc.com.mobile.project.ui.plan.viewmodel.PlanViewModel

class PlanTimeAdapter(private var addnewtime: List<Any> = ArrayList()): ListAdapter<TimeInfoResponse, PlanTimeAdapter.ViewHolder>(
UpTimeResultDiffCallback()
) {

class ViewHolder(private val binding: ItemPlanTimeBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: TimeInfoResponse) {
binding.timeKindTitle.text = item.type
binding.timeClassTitle.text = item.name
// 학점을 문자열로 변환하여 표시
binding.timeGradeTitle.text = item.credit.toString()
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = ItemPlanTimeBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = getItem(position)
holder.bind(item)
}

fun updateTimeList(addnewtime: ArrayList<TimeInfoResponse>) {
submitList(addnewtime)
notifyDataSetChanged()
}

class UpTimeResultDiffCallback : DiffUtil.ItemCallback<TimeInfoResponse>() {
override fun areItemsTheSame(oldItem: TimeInfoResponse, newItem: TimeInfoResponse): Boolean {
// subjectId를 비교하여 동일한 아이템인지 판별
return oldItem.name== newItem.name
}

override fun areContentsTheSame(oldItem: TimeInfoResponse, newItem: TimeInfoResponse): Boolean {
// 내용이 동일한지 비교
return oldItem == newItem
}
}
class PlanTimeAdapter(private val viewModel: PlanViewModel) :
ListAdapter<UpTimeResult, PlanTimeAdapter.ViewHolder>(
UpTimeResultDiffCallback()
) {
private var onItemLongClickListener: ((UpTimeResult) -> Unit)? = null
private var onItemClickListener: ((UpTimeResult) -> Unit)? = null

class ViewHolder(private val binding: ItemPlanTimeBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(item: UpTimeResult) {
binding.timeKindTitle.text = item.type
binding.timeClassTitle.text = item.name
binding.timeGradeTitle.text = item.credit.toString()
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding =
ItemPlanTimeBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = getItem(position)
holder.bind(item)

holder.itemView.setOnLongClickListener {
onItemLongClickListener?.invoke(item)
val position = holder.adapterPosition
if (position != RecyclerView.NO_POSITION) {
val clickedItem = getItem(position)
viewModel.subjectId.value?.let { it1 ->
viewModel.deleteTimeTable(
viewModel.grade.value!!,
viewModel.semester.value!!,
it1
)
}
}
true
}

holder.itemView.setOnClickListener {
onItemClickListener?.invoke(item)
}
}

fun updateTimeList(addnewtime: ArrayList<UpTimeResult>) {
submitList(addnewtime)
notifyDataSetChanged()
}

/*
fun setOnItemLongClickListener(listener: (UpTimeResult) -> Unit) {
this.onItemLongClickListener = listener
}
fun setOnItemClickListener(listener: (UpTimeResult) -> Unit) {
this.onItemClickListener = listener
}
*/

class UpTimeResultDiffCallback : DiffUtil.ItemCallback<UpTimeResult>() {
override fun areItemsTheSame(
oldItem: UpTimeResult,
newItem: UpTimeResult
): Boolean {
// subjectId를 비교하여 동일한 아이템인지 판별
return oldItem.name == newItem.name
}

override fun areContentsTheSame(
oldItem: UpTimeResult,
newItem: UpTimeResult
): Boolean {
// 내용이 동일한지 비교
return oldItem == newItem
}
}
}

0 comments on commit 9304eb2

Please sign in to comment.