Skip to content

Commit

Permalink
refactor: cleaned up codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
princeraj-pr committed Dec 22, 2023
1 parent 9ebb85d commit 0731ec1
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ package com.example.cyclofit.ui.adapter
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.cyclofit.ui.fragment.DistanceCombineChartFragment
import com.example.cyclofit.ui.fragment.KcalCombinedChartFragment
import com.example.cyclofit.ui.fragment.TimeCombinedChartFragment

class CombinedChartAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
FragmentStateAdapter(fragmentManager, lifecycle) {
override fun getItemCount(): Int {
return 3
}
LineChartAdapter(fragmentManager, lifecycle) {

override fun createFragment(position: Int): Fragment {
return when (position) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import com.example.cyclofit.ui.fragment.DistanceFragment
import com.example.cyclofit.ui.fragment.KcalFragment
import com.example.cyclofit.ui.fragment.TimeFragment

class LineChartAdapter(fragmentManager: FragmentManager, lifecycle : Lifecycle) : FragmentStateAdapter(fragmentManager,lifecycle) {
open class LineChartAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
FragmentStateAdapter(fragmentManager, lifecycle) {
override fun getItemCount(): Int {
return 3
}

override fun createFragment(position: Int): Fragment {
when(position){
0 -> return KcalFragment()
1 -> return TimeFragment()
else -> return DistanceFragment()
return when (position) {
0 -> KcalFragment()
1 -> TimeFragment()
else -> DistanceFragment()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ package com.example.cyclofit.ui.adapter
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.cyclofit.ui.fragment.DistancePieChartFragment
import com.example.cyclofit.ui.fragment.KcalPeiChartFragment
import com.example.cyclofit.ui.fragment.TimePieCharFragment

class PieChartAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
FragmentStateAdapter(fragmentManager, lifecycle) {
override fun getItemCount(): Int {
return 3
}
LineChartAdapter(fragmentManager, lifecycle) {

override fun createFragment(position: Int): Fragment {
return when (position) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import androidx.fragment.app.Fragment
import com.example.cyclofit.R
import com.example.cyclofit.databinding.FragmentDistanceCombineChartBinding
import com.example.cyclofit.model.Shared
import com.example.cyclofit.ui.utils.ChartUtils.months
import com.example.cyclofit.ui.utils.ChartUtils
import com.example.cyclofit.ui.utils.Constants
import com.github.mikephil.charting.charts.CombinedChart
import com.github.mikephil.charting.components.AxisBase
Expand Down Expand Up @@ -95,22 +95,12 @@ class DistanceCombineChartFragment : Fragment() {
orientation = Legend.LegendOrientation.HORIZONTAL
setDrawInside(false)
}
axisRight.apply {
setDrawGridLines(false)
setDrawAxisLine(false)
setDrawLabels(false)
axisMinimum = 0f
}
axisLeft.apply {
setDrawGridLines(false)
setDrawAxisLine(false)
setDrawLabels(false)
axisMinimum = 0f
}
customizeYAxis(axisRight)
customizeYAxis(axisLeft)
val formatter = object : ValueFormatter() {
override fun getAxisLabel(value: Float, axis: AxisBase?): String {
val index = value.toInt() % months.size
return months[index]
val index = value.toInt() % ChartUtils.Months.values().size
return ChartUtils.Months.values()[index].name
}
}
xAxis.apply {
Expand All @@ -125,6 +115,15 @@ class DistanceCombineChartFragment : Fragment() {
}
}

private fun customizeYAxis(axis: YAxis) {
axis.apply {
setDrawGridLines(false)
setDrawAxisLine(false)
setDrawLabels(false)
axisMinimum = 0f
}
}

private fun generateLineData(dataList: MutableList<Entry>): LineData {
val lineData = LineData()
val lineDataSet = LineDataSet(dataList, "Line DataSet")
Expand All @@ -149,8 +148,7 @@ class DistanceCombineChartFragment : Fragment() {
dataSet.valueTextSize = 10f
axisDependency = YAxis.AxisDependency.LEFT
}
val barWidth = 0.45f // x2 dataset
// (0.45 + 0.02) * 2 + 0.06 = 1.00 -> interval per "group"
val barWidth = 0.45f
val barData = BarData(dataSet)
barData.barWidth = barWidth
return barData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import com.example.cyclofit.R
import com.example.cyclofit.databinding.FragmentKcalCombinedChartBinding
import com.example.cyclofit.model.Shared
import com.example.cyclofit.model.User
import com.example.cyclofit.ui.utils.ChartUtils.months
import com.example.cyclofit.ui.utils.ChartUtils
import com.example.cyclofit.ui.utils.Constants
import com.example.cyclofit.ui.utils.Constants.met
import com.github.mikephil.charting.charts.CombinedChart.DrawOrder
import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.components.Legend
Expand Down Expand Up @@ -78,7 +79,6 @@ class KcalCombinedChartFragment : Fragment() {
val barArrayList = mutableListOf<Entry>()
for ((x, i) in sp.withIndex()) {
val y = i.time.toInt()
val met = 12 // for bicycles
val wt = userWeight.toFloat() // in kg
var totalCalsBurnt: Float = y * (met * 3.5f * wt)
totalCalsBurnt /= 200 * 1000
Expand All @@ -91,7 +91,6 @@ class KcalCombinedChartFragment : Fragment() {
val barArrayList = mutableListOf<BarEntry>()
for ((x, i) in sp.withIndex()) {
val y = i.time.toInt()
val met = 12 // for bicycles
val wt = userWeight.toFloat() // in kg
var totalCalsBurnt: Float = y * (met * 3.5f * wt)
totalCalsBurnt /= 200 * 1000
Expand Down Expand Up @@ -120,22 +119,12 @@ class KcalCombinedChartFragment : Fragment() {
orientation = Legend.LegendOrientation.HORIZONTAL
setDrawInside(false)
}
axisRight.apply {
setDrawGridLines(false)
setDrawAxisLine(false)
setDrawLabels(false)
axisMinimum = 0f
}
axisLeft.apply {
setDrawGridLines(false)
setDrawAxisLine(false)
setDrawLabels(false)
axisMinimum = 0f
}
customizeYAxis(axisRight)
customizeYAxis(axisLeft)
val formatter = object : ValueFormatter() {
override fun getAxisLabel(value: Float, axis: AxisBase?): String {
val index = value.toInt() % months.size
return months[index]
val index = value.toInt() % ChartUtils.Months.values().size
return ChartUtils.Months.values()[index].name
}
}
xAxis.apply {
Expand All @@ -150,6 +139,15 @@ class KcalCombinedChartFragment : Fragment() {
}
}

private fun customizeYAxis(axis: YAxis) {
axis.apply {
setDrawGridLines(false)
setDrawAxisLine(false)
setDrawLabels(false)
axisMinimum = 0f
}
}

private fun generateLineData(): LineData {
val lineData = LineData()
val lineDataSet = LineDataSet(getCaloriesForLineChart(userWeight), "Line DataSet")
Expand All @@ -174,8 +172,7 @@ class KcalCombinedChartFragment : Fragment() {
dataSet.valueTextSize = 10f
axisDependency = YAxis.AxisDependency.LEFT
}
val barWidth = 0.45f // x2 dataset
// (0.45 + 0.02) * 2 + 0.06 = 1.00 -> interval per "group"
val barWidth = 0.45f
val barData = BarData(dataSet)
barData.barWidth = barWidth
return barData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.example.cyclofit.databinding.FragmentKcalBinding
import com.example.cyclofit.model.Shared
import com.example.cyclofit.model.User
import com.example.cyclofit.ui.utils.Constants
import com.example.cyclofit.ui.utils.Constants.met
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
Expand Down Expand Up @@ -63,7 +64,6 @@ class KcalFragment : Fragment() {
val barArrayList = mutableListOf<Entry>()
for ((x, i) in sp.withIndex()) {
val y = i.time.toInt()
val met = 12 // for bicycles
val wt = userWeight.toDouble() // in kg
var totalCalsBurnt = 0.0;
totalCalsBurnt = y * (met * 3.5 * wt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.example.cyclofit.model.Shared
import com.example.cyclofit.model.User
import com.example.cyclofit.ui.utils.ChartUtils
import com.example.cyclofit.ui.utils.Constants
import com.example.cyclofit.ui.utils.Constants.met
import com.github.mikephil.charting.animation.Easing
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.data.Entry
Expand Down Expand Up @@ -69,7 +70,6 @@ class KcalPeiChartFragment : Fragment(), OnChartValueSelectedListener {
val barArrayList = mutableListOf<PieEntry>()
for (i in sp) {
val y = i.time.toInt()
val met = 12 // for bicycles
val wt = userWeight.toDouble() // in kg
var totalCalsBurnt: Double = y * (met * 3.5 * wt)
totalCalsBurnt /= 200 * 1000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import androidx.fragment.app.Fragment
import com.example.cyclofit.R
import com.example.cyclofit.databinding.FragmentTimeCombinedChartBinding
import com.example.cyclofit.model.Shared
import com.example.cyclofit.ui.utils.ChartUtils.months
import com.example.cyclofit.ui.utils.ChartUtils
import com.example.cyclofit.ui.utils.Constants
import com.github.mikephil.charting.charts.CombinedChart
import com.github.mikephil.charting.components.AxisBase
Expand Down Expand Up @@ -93,22 +93,12 @@ class TimeCombinedChartFragment : Fragment() {
orientation = Legend.LegendOrientation.HORIZONTAL
setDrawInside(false)
}
axisRight.apply {
setDrawGridLines(false)
setDrawAxisLine(false)
setDrawLabels(false)
axisMinimum = 0f
}
axisLeft.apply {
setDrawGridLines(false)
setDrawAxisLine(false)
setDrawLabels(false)
axisMinimum = 0f
}
customizeYAxis(axisRight)
customizeYAxis(axisLeft)
val formatter = object : ValueFormatter() {
override fun getAxisLabel(value: Float, axis: AxisBase?): String {
val index = value.toInt() % months.size
return months[index]
val index = value.toInt() % ChartUtils.Months.values().size
return ChartUtils.Months.values()[index].name
}
}
xAxis.apply {
Expand Down Expand Up @@ -138,6 +128,14 @@ class TimeCombinedChartFragment : Fragment() {
lineData.addDataSet(lineDataSet)
return lineData
}
private fun customizeYAxis(axis: YAxis) {
axis.apply {
setDrawGridLines(false)
setDrawAxisLine(false)
setDrawLabels(false)
axisMinimum = 0f
}
}

private fun generateBarData(dataList: MutableList<BarEntry>): BarData {
val dataSet = BarDataSet(dataList, "Bar")
Expand All @@ -147,8 +145,7 @@ class TimeCombinedChartFragment : Fragment() {
dataSet.valueTextSize = 10f
axisDependency = YAxis.AxisDependency.LEFT
}
val barWidth = 0.45f // x2 dataset
// (0.45 + 0.02) * 2 + 0.06 = 1.00 -> interval per "group"
val barWidth = 0.45f
val barData = BarData(dataSet)
barData.barWidth = barWidth
return barData
Expand Down
18 changes: 14 additions & 4 deletions app/src/main/java/com/example/cyclofit/ui/utils/ChartUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.github.mikephil.charting.utils.ColorTemplate

object ChartUtils {

// add a lot of colors
val colors = ArrayList<Int>().apply {
for (c in ColorTemplate.VORDIPLOM_COLORS) add(c)
for (c in ColorTemplate.JOYFUL_COLORS) add(c)
Expand All @@ -14,7 +13,18 @@ object ChartUtils {
add(ColorTemplate.getHoloBlue())
}

val months = arrayOf(
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
)
enum class Months {
Jan,
Feb,
Mar,
Apr,
May,
Jun,
Jul,
Aug,
Sep,
Oct,
Nov,
Dec
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/example/cyclofit/ui/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ object Constants{
const val PUT_EXTRA = "put"
const val PROFILE_COMPLETED = "profile"

const val met = 12 // for bicycles


fun showImageChooser(activity : Activity)
{
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/menu/health_top.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:title="Line Chart" />
<item
android:id="@+id/pie_chart"
android:title="Pic Chart" />
android:title="Pie Chart" />
<item
android:id="@+id/combine_chart"
android:title="Combine Chart" />
Expand Down

0 comments on commit 0731ec1

Please sign in to comment.