Skip to content

Commit

Permalink
added dining marker and interactive dining graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
trinif committed Oct 22, 2023
1 parent 5e8604f commit 14b997c
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -178,9 +181,39 @@ class DiningInsightsCardAdapter(private var cells: ArrayList<DiningInsightCell>)
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) {
Expand Down Expand Up @@ -233,6 +266,7 @@ class DiningInsightsCardAdapter(private var cells: ArrayList<DiningInsightCell>)
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) {
Expand All @@ -246,6 +280,7 @@ class DiningInsightsCardAdapter(private var cells: ArrayList<DiningInsightCell>)
predictionSet.color = mContext.getColor(R.color.gray)
predictionSet.lineWidth = 4f
predictionSet.enableDashedLine(10f, 10f, 0f)
predictionSet.setDrawHighlightIndicators(false)
val dataSets: ArrayList<ILineDataSet> = ArrayList()
dataSets.add(actualValues)
dataSets.add(predictionSet)
Expand Down
Original file line number Diff line number Diff line change
@@ -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())
}
}
24 changes: 24 additions & 0 deletions PennMobile/src/main/res/layout/dining_marker_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp">

<ImageView
android:id="@+id/dining_marker_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/dining_insights_circle"
/>

<TextView
android:id="@+id/dining_marker_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/gilroy_bold"
android:text="@string/dining_graph_point"
android:textColor="@android:color/black"
android:textSize="14sp" />

</LinearLayout>
1 change: 1 addition & 0 deletions PennMobile/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<string name="out_of_dining_swipes_message">You have no swipes left for this semester.</string>
<string name="out_of_dining_swipes_prediction_message">Based on your current balance and past behavior, we project you\'ll run out on this date.</string>
<string name="extra_dining_swipes_message">Based on your past behavior, we project you\'ll end the semester with swipes to spare.</string>
<string name="dining_graph_point">Date Filler Text \n Swipes Filler Text</string>

<!-- LaundryRoom -->
<string name="laundry_washer">Washers</string>
Expand Down

0 comments on commit 14b997c

Please sign in to comment.