You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.7 KiB
Kotlin
47 lines
1.7 KiB
Kotlin
package com.cyb3rko.techniklogger.recycler
|
|
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import android.widget.TextView
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
import androidx.recyclerview.widget.ListAdapter
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
import com.cyb3rko.techniklogger.R
|
|
import com.cyb3rko.techniklogger.data.objects.Participation
|
|
|
|
internal class ParticipationAdapter(
|
|
val action: (participation: Participation) -> Unit
|
|
) : ListAdapter<Participation, ParticipationAdapter.ViewHolder>(ParticipationDiffCallback) {
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
|
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_member, parent, false)
|
|
return ViewHolder(view)
|
|
}
|
|
|
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
|
val entry = getItem(position)
|
|
val title = "${entry.name}, ${entry.duration} h"
|
|
holder.titleView.text = title
|
|
holder.titleView.setOnClickListener {
|
|
action(entry)
|
|
}
|
|
}
|
|
|
|
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
|
val titleView: TextView = view.findViewById(R.id.title)
|
|
}
|
|
|
|
object ParticipationDiffCallback : DiffUtil.ItemCallback<Participation>() {
|
|
override fun areItemsTheSame(oldItem: Participation, newItem: Participation): Boolean {
|
|
return oldItem.objectId == newItem.objectId
|
|
}
|
|
|
|
override fun areContentsTheSame(oldItem: Participation, newItem: Participation): Boolean {
|
|
return oldItem.duration == newItem.duration &&
|
|
oldItem.name == newItem.name &&
|
|
oldItem.time == newItem.time
|
|
}
|
|
}
|
|
}
|