diff --git a/app/src/main/java/com/example/nerbos/fragments/statistic/StatisticFragment.kt b/app/src/main/java/com/example/nerbos/fragments/statistic/StatisticFragment.kt index bad2441b3231e25f2747f56e5793d286cd2cbc7d..df203cb4033c734c10f9e352516adedef5014e70 100644 --- a/app/src/main/java/com/example/nerbos/fragments/statistic/StatisticFragment.kt +++ b/app/src/main/java/com/example/nerbos/fragments/statistic/StatisticFragment.kt @@ -3,13 +3,14 @@ package com.example.nerbos.fragments.statistic import android.graphics.Color import android.graphics.Typeface import android.os.Bundle -import android.util.Log import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.lifecycle.MediatorLiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.lifecycleScope import com.example.nerbos.R import com.example.nerbos.databinding.FragmentStatisticBinding import com.example.nerbos.model.TransactionCategory @@ -22,14 +23,10 @@ import com.github.mikephil.charting.data.PieDataSet import com.github.mikephil.charting.data.PieEntry import com.github.mikephil.charting.formatter.PercentFormatter import com.github.mikephil.charting.utils.MPPointF +import kotlinx.coroutines.launch import java.text.NumberFormat import java.util.Locale -/** - * A simple [Fragment] subclass. - * Use the [StatisticFragment.newInstance] factory method to - * create an instance of this fragment. - */ class StatisticFragment : Fragment() { private lateinit var transactionViewModel: TransactionViewModel @@ -59,21 +56,9 @@ class StatisticFragment : Fragment() { authentication = Authentication(requireContext()) // Set the view model for the transaction and set the user id transactionViewModel = ViewModelProvider(this)[TransactionViewModel::class.java] - transactionViewModel.setReadDataUserId(authentication.getNim()) - - // log all the transactions - transactionViewModel.readAllData!!.observe(viewLifecycleOwner) { transactions -> - transactions.forEach { - Log.d("Transaction", it.toString()) - if (it.category == TransactionCategory.INCOME) { - sumIncome += it.nominal - } else { - sumOutcome += it.nominal - } - } - // Notify the data is ready - liveDataReady.postValue(true) - } + + // Get the data from the database + getData() // Set up the pie chart pieChartSetup() @@ -84,6 +69,30 @@ class StatisticFragment : Fragment() { return binding.root } + private fun getData() { + lifecycleScope.launch { + // Get the sum of income and outcome + val nim = authentication.getNim() + val income = transactionViewModel.getSums(nim, TransactionCategory.INCOME) + val outcome = transactionViewModel.getSums(nim, TransactionCategory.OUTCOME) + + // Use MediatorLiveData to observe both income and outcome + val result = MediatorLiveData<Float>().apply { + addSource(income) { value -> setValue(value) } + addSource(outcome) { value -> setValue(value) } + } + + // Observing the result + result.observe(viewLifecycleOwner) { + if (income.value != null && outcome.value != null) { + sumIncome = income.value!! + sumOutcome = outcome.value!! + liveDataReady.postValue(true) + } + } + } + } + // Display a pie chart of the income and outcome private fun pieChartSetup() { // Set the pie chart: percentage, description, extra offsets @@ -103,20 +112,21 @@ class StatisticFragment : Fragment() { pieChart.transparentCircleRadius = 48f pieChart.setDrawCenterText(true) - // Set the pie chart: rotation angle, rotation enabled, tap, animation - pieChart.setRotationAngle(0f) - pieChart.isRotationEnabled = true - pieChart.isHighlightPerTapEnabled = true - pieChart.animateY(1400, Easing.EaseInOutQuad) - // Set the pie chart: legend and entry label pieChart.legend.isEnabled = false pieChart.setEntryLabelColor(Color.BLACK) pieChart.setEntryLabelTextSize(12f) + // Set the pie chart: rotation and highlight + pieChart.setRotationAngle(0f) + pieChart.isRotationEnabled = true + pieChart.isHighlightPerTapEnabled = true } private fun setPieData(){ + // Set the pie chart: animation + pieChart.animateY(1400, Easing.EaseInOutQuad) + // Set the pie chart: data val entries: ArrayList<PieEntry> = ArrayList() entries.add(PieEntry(sumIncome, resources.getString(R.string.income)))