Document UI components that are not activities or fragments.

This commit is contained in:
Danila Fedorin 2020-05-13 15:57:49 -07:00
parent ce0096d94f
commit b48317c0c0
5 changed files with 44 additions and 3 deletions

View File

@ -2,6 +2,16 @@ package com.danilafe.fencelessgrazing
import com.danilafe.fencelessgrazing.model.CollarSummary
/**
* Simple interface used for the [CollarSummaryAdapter] to
* allow items in the collar list to respond to click events.
*/
interface CollarClickListener {
/**
* Method called when a collar is clicked in the [CollarSummaryAdapter].
*
* @param collar the collar that was clicked.
*/
fun onCollarClick(collar: CollarSummary?)
}

View File

@ -11,12 +11,23 @@ import com.android.volley.toolbox.Volley
import com.danilafe.fencelessgrazing.model.CollarSummary
import com.danilafe.fencelessgrazing.requests.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()) {
class DiffCallback : DiffUtil.ItemCallback<CollarSummary>() {
/**
* [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

View File

@ -5,13 +5,30 @@ import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.danilafe.fencelessgrazing.model.CollarSummary
/**
* A [RecyclerView.ViewHolder] used to render [CollarSummary] item entries.
*/
class CollarViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
/**
* The [TextView] that holds the name of a collar.
*/
private val nameView: TextView = itemView.findViewById(R.id.collarSummaryName)
/**
* The [TextView] that holds the position of the collar.
*/
private val positionView: TextView = itemView.findViewById(R.id.collarSummaryPos)
/**
* Called by the [CollarSummaryAdapter] to update the contents of the view this
* class holds.
*
* @param summary the collar summary whose data should be extracted and placed into the views.
* @param collarClickListener the action that should be called when a summary is clicked.
*/
fun bindData(summary: CollarSummary, collarClickListener: CollarClickListener) {
nameView.text = summary.name
positionView.text = nameView.resources.getString(R.string.collarSummaryLocation,
positionView.text = itemView.resources.getString(R.string.collarSummaryLocation,
summary.pos.longitude.toDouble(), summary.pos.latitude.toDouble())
itemView.setOnClickListener {
collarClickListener.onCollarClick(summary)

View File

@ -4,6 +4,9 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
/**
* Adapter for the [ViewPager2][androidx.viewpager2.widget.ViewPager2] class.
*/
class StatisticsGraphAdapter(activity : FragmentActivity) : FragmentStateAdapter(activity) {
override fun getItemCount(): Int = 1
override fun createFragment(position: Int): Fragment = DistanceTraveledGraph()

View File

@ -4,7 +4,7 @@ import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
/**
* General request aimed at a protected endpoint of the API. The [token] can be retrieved
* General request aimed at a protected endpoint of the API. The token can be retrieved
* via a [LoginRequest], or, if you've made it past the login screen, via Shared Preferences.
*
* The [AuthenticatedRequest] expects to receive a string from the API endpoint; how this string