diff --git a/app/src/main/java/com/example/bondoman/ui/graph/GraphFragment.kt b/app/src/main/java/com/example/bondoman/ui/graph/GraphFragment.kt index d83c5bba4e50e4a23cc9f8bc8f04d3d4922f8030..371c343f8fa313ab63564944e36fa356aa05b55c 100644 --- a/app/src/main/java/com/example/bondoman/ui/graph/GraphFragment.kt +++ b/app/src/main/java/com/example/bondoman/ui/graph/GraphFragment.kt @@ -7,12 +7,14 @@ import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.lifecycleScope import com.anychart.AnyChart import com.anychart.AnyChartView import com.anychart.chart.common.dataentry.DataEntry +import com.anychart.chart.common.dataentry.ValueDataEntry import com.anychart.charts.Pie -import com.example.bondoman.R import com.example.bondoman.databinding.FragmentGraphBinding +import kotlinx.coroutines.launch class GraphFragment: Fragment() { @@ -20,6 +22,10 @@ class GraphFragment: Fragment() { private var _binding: FragmentGraphBinding? = null + private lateinit var graphViewModel: GraphViewModel + + private var pie: Pie? = null + private val binding get() = _binding!! override fun onCreateView( @@ -27,24 +33,37 @@ class GraphFragment: Fragment() { container: ViewGroup?, savedInstanceState: Bundle? ): View { - val graphViewModel = ViewModelProvider(this).get(GraphViewModel::class.java) - graphViewModel.getCountTypeData() + graphViewModel = ViewModelProvider(this).get(GraphViewModel::class.java) _binding = FragmentGraphBinding.inflate(inflater, container, false) val root: View = binding.root anyChartView = binding.graphChartFragment - graphViewModel.countData.observe(viewLifecycleOwner) { dataEntries -> - setupChartView(dataEntries) - } + pie= AnyChart.pie() + return root } - private fun setupChartView(dataEntries: List<DataEntry>) { - val pie: Pie = AnyChart.pie() - pie.data(dataEntries) + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + lifecycleScope.launch { + val (pemasukan, pembelian) = graphViewModel.getCountTypeData() + Log.d("Graph Fragment", "$pemasukan, $pembelian") + updateChart(pemasukan, pembelian) + } + + } + + private fun updateChart(pemasukanCount: Int, pembelianCount: Int){ + Log.d("Graph Fragment", "$pemasukanCount and $pembelianCount") + val dataEntries: List<DataEntry> = listOf( + ValueDataEntry("Pemasukan", pemasukanCount), + ValueDataEntry("Pembelian", pembelianCount) + ) + + pie!!.data(dataEntries) anyChartView.setChart(pie) } diff --git a/app/src/main/java/com/example/bondoman/ui/graph/GraphViewModel.kt b/app/src/main/java/com/example/bondoman/ui/graph/GraphViewModel.kt index 68cc255f4ba7f2c22a1b2059a1d181ee602e47c2..7782fd7af20537d5c9dbe49e6f403ffa9a6e73b0 100644 --- a/app/src/main/java/com/example/bondoman/ui/graph/GraphViewModel.kt +++ b/app/src/main/java/com/example/bondoman/ui/graph/GraphViewModel.kt @@ -2,11 +2,6 @@ package com.example.bondoman.ui.graph import android.app.Application import androidx.lifecycle.AndroidViewModel -import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.ViewModel -import com.anychart.chart.common.dataentry.DataEntry -import com.anychart.chart.common.dataentry.ValueDataEntry import com.example.bondoman.core.repository.TransactionRepository import com.example.bondoman.core.usecase.AddTransaction import com.example.bondoman.core.usecase.GetAllTransaction @@ -16,19 +11,11 @@ import com.example.bondoman.core.usecase.RemoveTransaction import com.example.bondoman.framework.RoomTransactionDataSource import com.example.bondoman.framework.UseCases import com.example.bondoman.lib.transaction.TRANSACTION_TYPE -import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch import kotlinx.coroutines.withContext class GraphViewModel(application: Application):AndroidViewModel(application) { - private val _countData = MutableLiveData<List<DataEntry>>() - - val countData: LiveData<List<DataEntry>> = _countData - - private val coroutineScope = CoroutineScope(Dispatchers.IO) - val repository = TransactionRepository(RoomTransactionDataSource(application)) val useCases = UseCases( @@ -39,18 +26,11 @@ class GraphViewModel(application: Application):AndroidViewModel(application) { GetTransactionTypeCount(repository) ) - fun getCountTypeData(){ - coroutineScope.launch { - val pemasukanCount:Int = useCases.getTransactionTypeCount(TRANSACTION_TYPE.PEMASUKAN) - val pembelianCount:Int = useCases.getTransactionTypeCount(TRANSACTION_TYPE.PEMBELIAN) - - val dataEntries:List<DataEntry> = listOf( - ValueDataEntry("Pemasukan", pemasukanCount), - ValueDataEntry("Pembelian", pembelianCount)) - - withContext(Dispatchers.Main) { - _countData.value = dataEntries - } + suspend fun getCountTypeData(): Pair<Int, Int>{ + return withContext(Dispatchers.Default) { + val pemasukanCount = useCases.getTransactionTypeCount(TRANSACTION_TYPE.PEMASUKAN) + val pembelianCount = useCases.getTransactionTypeCount(TRANSACTION_TYPE.PEMBELIAN) + Pair(pemasukanCount, pembelianCount) } } } \ No newline at end of file