From 0731ec153eb7f0b3d82fb4030a701c5305d1cf67 Mon Sep 17 00:00:00 2001 From: Prince Raj Date: Fri, 22 Dec 2023 17:31:15 +0530 Subject: [PATCH] refactor: cleaned up codebase --- .../ui/adapter/CombinedChartAdapter.kt | 6 +--- .../cyclofit/ui/adapter/LineChartAdapter.kt | 11 +++--- .../cyclofit/ui/adapter/PieChartAdapter.kt | 6 +--- .../fragment/DistanceCombineChartFragment.kt | 32 ++++++++--------- .../ui/fragment/KcalCombinedChartFragment.kt | 35 +++++++++---------- .../cyclofit/ui/fragment/KcalFragment.kt | 2 +- .../ui/fragment/KcalPeiChartFragment.kt | 2 +- .../ui/fragment/TimeCombinedChartFragment.kt | 31 ++++++++-------- .../example/cyclofit/ui/utils/ChartUtils.kt | 18 +++++++--- .../example/cyclofit/ui/utils/Constants.kt | 2 ++ app/src/main/res/menu/health_top.xml | 2 +- 11 files changed, 72 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/com/example/cyclofit/ui/adapter/CombinedChartAdapter.kt b/app/src/main/java/com/example/cyclofit/ui/adapter/CombinedChartAdapter.kt index b933ea4..b95e2b3 100644 --- a/app/src/main/java/com/example/cyclofit/ui/adapter/CombinedChartAdapter.kt +++ b/app/src/main/java/com/example/cyclofit/ui/adapter/CombinedChartAdapter.kt @@ -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) { diff --git a/app/src/main/java/com/example/cyclofit/ui/adapter/LineChartAdapter.kt b/app/src/main/java/com/example/cyclofit/ui/adapter/LineChartAdapter.kt index a769508..bbc3838 100644 --- a/app/src/main/java/com/example/cyclofit/ui/adapter/LineChartAdapter.kt +++ b/app/src/main/java/com/example/cyclofit/ui/adapter/LineChartAdapter.kt @@ -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() } } } \ No newline at end of file diff --git a/app/src/main/java/com/example/cyclofit/ui/adapter/PieChartAdapter.kt b/app/src/main/java/com/example/cyclofit/ui/adapter/PieChartAdapter.kt index 7f3bbb9..02ce726 100644 --- a/app/src/main/java/com/example/cyclofit/ui/adapter/PieChartAdapter.kt +++ b/app/src/main/java/com/example/cyclofit/ui/adapter/PieChartAdapter.kt @@ -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) { diff --git a/app/src/main/java/com/example/cyclofit/ui/fragment/DistanceCombineChartFragment.kt b/app/src/main/java/com/example/cyclofit/ui/fragment/DistanceCombineChartFragment.kt index 726910d..1217a10 100644 --- a/app/src/main/java/com/example/cyclofit/ui/fragment/DistanceCombineChartFragment.kt +++ b/app/src/main/java/com/example/cyclofit/ui/fragment/DistanceCombineChartFragment.kt @@ -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 @@ -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 { @@ -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): LineData { val lineData = LineData() val lineDataSet = LineDataSet(dataList, "Line DataSet") @@ -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 diff --git a/app/src/main/java/com/example/cyclofit/ui/fragment/KcalCombinedChartFragment.kt b/app/src/main/java/com/example/cyclofit/ui/fragment/KcalCombinedChartFragment.kt index 06abea3..d275c1a 100644 --- a/app/src/main/java/com/example/cyclofit/ui/fragment/KcalCombinedChartFragment.kt +++ b/app/src/main/java/com/example/cyclofit/ui/fragment/KcalCombinedChartFragment.kt @@ -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 @@ -78,7 +79,6 @@ class KcalCombinedChartFragment : Fragment() { val barArrayList = mutableListOf() 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 @@ -91,7 +91,6 @@ class KcalCombinedChartFragment : Fragment() { val barArrayList = mutableListOf() 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 @@ -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 { @@ -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") @@ -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 diff --git a/app/src/main/java/com/example/cyclofit/ui/fragment/KcalFragment.kt b/app/src/main/java/com/example/cyclofit/ui/fragment/KcalFragment.kt index 7b83860..9dbd02f 100644 --- a/app/src/main/java/com/example/cyclofit/ui/fragment/KcalFragment.kt +++ b/app/src/main/java/com/example/cyclofit/ui/fragment/KcalFragment.kt @@ -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 @@ -63,7 +64,6 @@ class KcalFragment : Fragment() { val barArrayList = mutableListOf() 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) diff --git a/app/src/main/java/com/example/cyclofit/ui/fragment/KcalPeiChartFragment.kt b/app/src/main/java/com/example/cyclofit/ui/fragment/KcalPeiChartFragment.kt index c52f3fa..29c5329 100644 --- a/app/src/main/java/com/example/cyclofit/ui/fragment/KcalPeiChartFragment.kt +++ b/app/src/main/java/com/example/cyclofit/ui/fragment/KcalPeiChartFragment.kt @@ -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 @@ -69,7 +70,6 @@ class KcalPeiChartFragment : Fragment(), OnChartValueSelectedListener { val barArrayList = mutableListOf() 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 diff --git a/app/src/main/java/com/example/cyclofit/ui/fragment/TimeCombinedChartFragment.kt b/app/src/main/java/com/example/cyclofit/ui/fragment/TimeCombinedChartFragment.kt index d12342c..553ccd4 100644 --- a/app/src/main/java/com/example/cyclofit/ui/fragment/TimeCombinedChartFragment.kt +++ b/app/src/main/java/com/example/cyclofit/ui/fragment/TimeCombinedChartFragment.kt @@ -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 @@ -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 { @@ -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): BarData { val dataSet = BarDataSet(dataList, "Bar") @@ -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 diff --git a/app/src/main/java/com/example/cyclofit/ui/utils/ChartUtils.kt b/app/src/main/java/com/example/cyclofit/ui/utils/ChartUtils.kt index 901e58b..f81f913 100644 --- a/app/src/main/java/com/example/cyclofit/ui/utils/ChartUtils.kt +++ b/app/src/main/java/com/example/cyclofit/ui/utils/ChartUtils.kt @@ -4,7 +4,6 @@ import com.github.mikephil.charting.utils.ColorTemplate object ChartUtils { - // add a lot of colors val colors = ArrayList().apply { for (c in ColorTemplate.VORDIPLOM_COLORS) add(c) for (c in ColorTemplate.JOYFUL_COLORS) add(c) @@ -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 + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/cyclofit/ui/utils/Constants.kt b/app/src/main/java/com/example/cyclofit/ui/utils/Constants.kt index b1d5424..33421d3 100644 --- a/app/src/main/java/com/example/cyclofit/ui/utils/Constants.kt +++ b/app/src/main/java/com/example/cyclofit/ui/utils/Constants.kt @@ -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) { diff --git a/app/src/main/res/menu/health_top.xml b/app/src/main/res/menu/health_top.xml index 1d10346..ed01e57 100644 --- a/app/src/main/res/menu/health_top.xml +++ b/app/src/main/res/menu/health_top.xml @@ -5,7 +5,7 @@ android:title="Line Chart" /> + android:title="Pie Chart" />