56 lines
2.0 KiB
Kotlin
56 lines
2.0 KiB
Kotlin
package com.danilafe.fencelessgrazing.ui.components
|
|
|
|
import android.view.LayoutInflater
|
|
import android.view.ViewGroup
|
|
import androidx.recyclerview.widget.ListAdapter
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
import com.danilafe.fencelessgrazing.R
|
|
import com.danilafe.fencelessgrazing.model.CollarSummary
|
|
import com.danilafe.fencelessgrazing.requests.authenticated.CollarRequest
|
|
|
|
/**
|
|
* A [ListAdapter] subclass to display and manage a list of [CollarSummary] items
|
|
* collected from a [CollarRequest].
|
|
*
|
|
* @param items the list of items that are contained in this adapter.
|
|
* @param collarClickListener the action to be executed when an item in the list is clicked.
|
|
*/
|
|
class CollarSummaryAdapter(
|
|
private val items: List<CollarSummary>,
|
|
private val collarClickListener: CollarClickListener
|
|
) : ListAdapter<CollarSummary, CollarViewHolder>(
|
|
DiffCallback()
|
|
) {
|
|
|
|
/**
|
|
* [DiffUtil.ItemCallback] used for the [ListAdapter]. Compares items using
|
|
* the standard equality method generated for data classes.
|
|
*/
|
|
internal class DiffCallback : DiffUtil.ItemCallback<CollarSummary>() {
|
|
override fun areItemsTheSame(oldItem: CollarSummary, newItem: CollarSummary): Boolean
|
|
= oldItem.id == newItem.id
|
|
|
|
override fun areContentsTheSame(oldItem: CollarSummary, newItem: CollarSummary): Boolean
|
|
= oldItem == newItem
|
|
}
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CollarViewHolder {
|
|
val layout = LayoutInflater.from(parent.context).inflate(viewType, parent, false)
|
|
return CollarViewHolder(
|
|
layout
|
|
)
|
|
}
|
|
|
|
override fun onBindViewHolder(holder: CollarViewHolder, position: Int) {
|
|
holder.bindData(getItem(position), collarClickListener)
|
|
}
|
|
|
|
override fun getItemViewType(position: Int): Int =
|
|
R.layout.collar_summary_layout
|
|
|
|
override fun getItem(position: Int): CollarSummary = items[position]
|
|
|
|
override fun getItemCount(): Int = items.size
|
|
|
|
|
|
} |