diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/DiningInsightsCardAdapter.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/DiningInsightsCardAdapter.kt index 84c09940..9c4e1ba5 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/DiningInsightsCardAdapter.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/DiningInsightsCardAdapter.kt @@ -20,9 +20,12 @@ import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.LineData import com.github.mikephil.charting.data.LineDataSet import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.highlight.Highlight import com.github.mikephil.charting.interfaces.datasets.ILineDataSet +import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.pennapps.labs.pennmobile.* import com.pennapps.labs.pennmobile.classes.DiningInsightCell +import com.pennapps.labs.pennmobile.classes.DiningMarkerView import java.text.ParseException import java.text.SimpleDateFormat import java.util.* @@ -178,9 +181,39 @@ class DiningInsightsCardAdapter(private var cells: ArrayList) yAxis.setDrawZeroLine(false) yAxis.setDrawLimitLinesBehindData(false) setData(amounts, predictionChart, holder, typeId) - predictionChart.setTouchEnabled(false) xAxis.removeAllLimitLines() xAxis.addLimitLine(endOfTermLine) + + // Enable touch gestures and marker + predictionChart.setTouchEnabled(true) + predictionChart.setPinchZoom(false) + predictionChart.setScaleEnabled(false) + + val markerView = DiningMarkerView(mContext, R.layout.dining_marker_view) + predictionChart.marker = markerView + + // Set an event listener to display marker content + predictionChart.setOnChartValueSelectedListener(object : + OnChartValueSelectedListener { + override fun onValueSelected(e: Entry?, h: Highlight?) { + if (e != null) { + if (h != null) { + markerView.setGraphType(typeId) + markerView.refreshContent(e, h) + } + predictionChart.invalidate() + } + } + + override fun onNothingSelected() { + // Hide the marker when nothing is selected + predictionChart.marker = null + } + }) + // Don't think this is necessary, add just in case + predictionChart.invalidate() + + } private fun bindDiningBalanceCells(holder: ViewHolder, cell: DiningInsightCell) { @@ -233,6 +266,7 @@ class DiningInsightsCardAdapter(private var cells: ArrayList) actualValues.setDrawValues(false) actualValues.setDrawCircles(false) actualValues.setDrawFilled(false) + actualValues.setDrawHighlightIndicators(false) if(typeId == DINING_DOLLARS_PREDICTIONS) { actualValues.color = mContext.getColor(R.color.diningGreen) } else if(typeId == DINING_SWIPES_PREDICTIONS) { @@ -246,6 +280,7 @@ class DiningInsightsCardAdapter(private var cells: ArrayList) predictionSet.color = mContext.getColor(R.color.gray) predictionSet.lineWidth = 4f predictionSet.enableDashedLine(10f, 10f, 0f) + predictionSet.setDrawHighlightIndicators(false) val dataSets: ArrayList = ArrayList() dataSets.add(actualValues) dataSets.add(predictionSet) diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/DiningMarkerView.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/DiningMarkerView.kt new file mode 100644 index 00000000..ddb190ae --- /dev/null +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/DiningMarkerView.kt @@ -0,0 +1,69 @@ +package com.pennapps.labs.pennmobile.classes + +import android.content.Context +import android.graphics.Canvas +import android.util.Log +import android.widget.ImageView +import android.widget.TextView +import com.github.mikephil.charting.components.MarkerView +import com.pennapps.labs.pennmobile.R +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import com.pennapps.labs.pennmobile.adapters.DiningInsightsCardAdapter +import kotlinx.android.synthetic.main.dining_spent_card.view.view +import java.text.SimpleDateFormat +import java.util.Date +import kotlin.math.roundToInt + +class DiningMarkerView(context: Context, layoutResource: Int) : MarkerView(context, layoutResource) { + + companion object { + private const val DINING_DOLLARS_PREDICTIONS = 2 + private const val DINING_SWIPES_PREDICTIONS = 3 + } + private val textView: TextView = findViewById(R.id.dining_marker_text) + private val point: ImageView = findViewById(R.id.dining_marker_point) + private var xValue: Float = 0.0F + private var yValue: Float = 0.0F + private var typeId: Int = 0 + + // Callback method to update the marker content + override fun refreshContent(entry: Entry, highlight: Highlight) { + xValue = entry.x + yValue = entry.y + //convert entry.x to date + val daysFromStart = xValue.roundToInt() + val sdf = SimpleDateFormat("MMM. dd") + val date = DiningInsightsCardAdapter.Utils.addDaysToDateMMMdd(DiningInsightsCardAdapter.START_DAY_OF_SEMESTER, daysFromStart) + + var diningData = String.format("%.2f", yValue) + if (typeId == DINING_SWIPES_PREDICTIONS) { + diningData = yValue.toInt().toString() + } + if (typeId == DINING_DOLLARS_PREDICTIONS) { + diningData = "\$" + diningData + } + textView.text = buildString { + append(date) + append("\n") + append(diningData) + } + if(typeId == DINING_SWIPES_PREDICTIONS) { + textView.setTextColor(context.getColor(R.color.diningBlue)) + point.setColorFilter(context.getColor(R.color.diningBlue)) + } + else { + textView.setTextColor(context.getColor(R.color.diningGreen)) + point.setColorFilter(context.getColor(R.color.diningGreen)) + } + } + + fun setGraphType(typeId: Int) { + this.typeId = typeId + } + // This is used to reposition the marker + override fun getOffset(): MPPointF { + return MPPointF(0.0F, (-height / 6).toFloat()) + } +} diff --git a/PennMobile/src/main/res/layout/dining_marker_view.xml b/PennMobile/src/main/res/layout/dining_marker_view.xml new file mode 100644 index 00000000..623d7a18 --- /dev/null +++ b/PennMobile/src/main/res/layout/dining_marker_view.xml @@ -0,0 +1,24 @@ + + + + + + + + \ No newline at end of file diff --git a/PennMobile/src/main/res/values/strings.xml b/PennMobile/src/main/res/values/strings.xml index e44a6c9a..5a62b35e 100644 --- a/PennMobile/src/main/res/values/strings.xml +++ b/PennMobile/src/main/res/values/strings.xml @@ -67,6 +67,7 @@ You have no swipes left for this semester. Based on your current balance and past behavior, we project you\'ll run out on this date. Based on your past behavior, we project you\'ll end the semester with swipes to spare. + Date Filler Text \n Swipes Filler Text Washers