Skip to content
Snippets Groups Projects
Commit 3241ac32 authored by Wilson Tansil's avatar Wilson Tansil Committed by Wilson Tansil
Browse files

fix: graph auto change

parent 8d527d87
No related merge requests found
......@@ -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)
}
......
......@@ -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
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment