2020-05-12 22:07:08 -07:00
|
|
|
package com.danilafe.fencelessgrazing
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.Toast
|
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import com.android.volley.RequestQueue
|
|
|
|
import com.android.volley.Response
|
|
|
|
import com.android.volley.toolbox.Volley
|
2020-05-13 16:47:16 -07:00
|
|
|
import com.danilafe.fencelessgrazing.model.CollarDistance
|
2020-05-12 22:07:08 -07:00
|
|
|
import com.danilafe.fencelessgrazing.requests.DistanceTraveledRequest
|
|
|
|
import com.github.mikephil.charting.charts.BarChart
|
|
|
|
import com.github.mikephil.charting.data.BarData
|
|
|
|
import com.github.mikephil.charting.data.BarDataSet
|
|
|
|
import com.github.mikephil.charting.data.BarEntry
|
|
|
|
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
|
|
|
|
import com.github.mikephil.charting.utils.ColorTemplate
|
|
|
|
|
2020-05-13 16:47:16 -07:00
|
|
|
/**
|
|
|
|
* One of the tabs from the [StatisticsActivity]. Contains the graph
|
|
|
|
* of the distance traveled by each animal, retrieved via the [DistanceTraveledRequest]
|
|
|
|
* from the API.
|
|
|
|
*/
|
2020-05-12 22:07:08 -07:00
|
|
|
class DistanceTraveledGraph() : Fragment() {
|
|
|
|
|
2020-05-13 16:47:16 -07:00
|
|
|
/**
|
|
|
|
* The BarChart used to display the distance data.
|
|
|
|
*/
|
2020-05-12 22:07:08 -07:00
|
|
|
private lateinit var distanceTraveledChart: BarChart
|
|
|
|
|
2020-05-13 16:47:16 -07:00
|
|
|
/**
|
|
|
|
* The Volley queue used for making API requests.
|
|
|
|
*/
|
2020-05-12 22:07:08 -07:00
|
|
|
private lateinit var queue : RequestQueue
|
|
|
|
|
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
): View? {
|
|
|
|
val view = inflater.inflate(R.layout.distance_traveled_layoyt, container, false)
|
|
|
|
distanceTraveledChart = view.findViewById(R.id.distanceTraveledChart)
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
queue = Volley.newRequestQueue(requireActivity().applicationContext)
|
|
|
|
|
2020-05-13 16:47:16 -07:00
|
|
|
setupChart()
|
2020-05-12 22:07:08 -07:00
|
|
|
triggerRefresh()
|
|
|
|
}
|
|
|
|
|
2020-05-13 16:47:16 -07:00
|
|
|
/**
|
|
|
|
* Send the API request to retrieve updated distance data.
|
|
|
|
*/
|
2020-05-12 22:07:08 -07:00
|
|
|
private fun triggerRefresh() {
|
|
|
|
val activity = requireActivity()
|
|
|
|
val sharedPrefs = activity.getSharedPreferences("FencelessGrazing", 0)
|
|
|
|
val token = sharedPrefs.getString("token", null)!!
|
|
|
|
val request = DistanceTraveledRequest(activity.getString(R.string.apiUrl), token,
|
|
|
|
Response.Listener {
|
2020-05-13 16:47:16 -07:00
|
|
|
updateChartData(it)
|
2020-05-12 22:07:08 -07:00
|
|
|
},
|
|
|
|
Response.ErrorListener {
|
|
|
|
Toast.makeText(activity, "Failed to retrieve distance traveled!", Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
queue.add(request)
|
|
|
|
}
|
2020-05-13 16:47:16 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the [distanceTraveledChart] with newly retrieved distance data.
|
|
|
|
*/
|
|
|
|
private fun updateChartData(distances: List<CollarDistance>) {
|
|
|
|
val entries = mutableListOf<BarEntry>()
|
|
|
|
val labels = distances.map { c -> c.name }
|
|
|
|
distances.forEachIndexed { i, v ->
|
|
|
|
entries.add(BarEntry(i.toFloat(), v.distance))
|
|
|
|
}
|
|
|
|
val dataSet = BarDataSet(entries, "Distance Traveled")
|
|
|
|
dataSet.colors = ColorTemplate.PASTEL_COLORS.toList()
|
|
|
|
val data = BarData(dataSet)
|
|
|
|
data.barWidth = 0.9f
|
|
|
|
distanceTraveledChart.data = data
|
|
|
|
distanceTraveledChart.xAxis.valueFormatter = IndexAxisValueFormatter(labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure the [distanceTraveledChart] with the proper visual settings.
|
|
|
|
*/
|
|
|
|
private fun setupChart() {
|
|
|
|
distanceTraveledChart.legend.textSize = 20.0f
|
|
|
|
distanceTraveledChart.setFitBars(true)
|
|
|
|
distanceTraveledChart.xAxis.granularity = 1.0f
|
|
|
|
distanceTraveledChart.xAxis.isGranularityEnabled = true
|
|
|
|
distanceTraveledChart.invalidate()
|
|
|
|
}
|
2020-05-12 22:07:08 -07:00
|
|
|
}
|