Skip to content
Snippets Groups Projects
Commit f9fc7b04 authored by rayhanp1402's avatar rayhanp1402
Browse files

feat: Graph functionality

parent 045d5a83
3 merge requests!13Release,!11proper save for gmail,!10Graph
......@@ -9,7 +9,11 @@ import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import com.example.bondoyap.R
import com.example.bondoyap.ui.transactions.TransactionsApplication
import com.example.bondoyap.ui.transactions.TransactionsViewModel
import com.example.bondoyap.ui.transactions.TransactionsViewModelFactory
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.data.PieData
......@@ -23,7 +27,11 @@ class GraphFragment : Fragment() {
fun newInstance() = GraphFragment()
}
private val viewModel: GraphViewModel by viewModels()
private val transactionsViewModel: TransactionsViewModel by viewModels {
TransactionsViewModelFactory((requireContext().applicationContext as TransactionsApplication).repository)
}
private lateinit var pieChart: PieChart
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -36,40 +44,48 @@ class GraphFragment : Fragment() {
savedInstanceState: Bundle?
): View {
val root = inflater.inflate(R.layout.fragment_graph, container, false)
val pieChart: PieChart = root.findViewById(R.id.pieChart)
val entries = listOf(
PieEntry(1f, "Entry 1"),
PieEntry(2f, "Entry 2"),
PieEntry(3f, "Entry 3")
)
val pieDataSet = PieDataSet(entries, "")
pieDataSet.colors = ColorTemplate.COLORFUL_COLORS.toList()
val pieData = PieData(pieDataSet)
pieChart.data = pieData
pieChart.description.isEnabled = false
val legend = pieChart.legend
val orientation = resources.configuration.orientation
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
legend.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
legend.verticalAlignment = Legend.LegendVerticalAlignment.CENTER
} else {
legend.horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER
legend.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM
}
legend.setDrawInside(false)
if (isDarkTheme()) {
legend.textColor = ContextCompat.getColor(requireContext(), R.color.white)
} else {
legend.textColor = ContextCompat.getColor(requireContext(), R.color.black)
}
pieChart.invalidate()
pieChart = root.findViewById(R.id.pieChart)
transactionsViewModel.pemasukanCount.observe(viewLifecycleOwner, Observer { pemasukanCount ->
transactionsViewModel.pengeluaranCount.observe(viewLifecycleOwner, Observer { pengeluaranCount ->
val entries = listOf(
PieEntry(pemasukanCount.toFloat(), "Pemasukan"),
PieEntry(pengeluaranCount.toFloat(), "Pengeluaran")
)
val colors = mutableListOf<Int>()
colors.add(ContextCompat.getColor(requireContext(), R.color.green))
colors.add(ContextCompat.getColor(requireContext(), R.color.red))
val pieDataSet = PieDataSet(entries, "")
pieDataSet.colors = colors
val pieData = PieData(pieDataSet)
pieChart.data = pieData
pieChart.description.isEnabled = false
val legend = pieChart.legend
val orientation = resources.configuration.orientation
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
legend.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
legend.verticalAlignment = Legend.LegendVerticalAlignment.CENTER
} else {
legend.horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER
legend.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM
}
legend.setDrawInside(false)
if (isDarkTheme()) {
legend.textColor = ContextCompat.getColor(requireContext(), R.color.white)
} else {
legend.textColor = ContextCompat.getColor(requireContext(), R.color.black)
}
pieChart.invalidate()
})
})
return root
}
......
......@@ -15,6 +15,8 @@ class TransactionsViewModel(
private val repository: TransactionsRepository
): ViewModel() {
val allTransactions: LiveData<List<Transactions>> = repository.allTransactions.asLiveData()
val pemasukanCount: LiveData<Int> = repository.getPemasukanCount().asLiveData()
val pengeluaranCount: LiveData<Int> = repository.getPengeluaranCount().asLiveData()
fun upsert(transactions: Transactions) = viewModelScope.launch {
repository.upsert(transactions)
......
......@@ -26,4 +26,10 @@ interface TransactionsDao {
@Query("DELETE FROM transactions")
suspend fun deleteAllTransactions()
@Query("SELECT COUNT(transactions.id) FROM transactions WHERE transactions.is_pemasukan = 1")
fun getPemasukanCount(): Flow<Int>
@Query("SELECT COUNT(transactions.id) FROM transactions WHERE transactions.is_pemasukan = 0")
fun getPengeluaranCount(): Flow<Int>
}
\ No newline at end of file
......@@ -28,4 +28,12 @@ class TransactionsRepository(private val transactionsDao: TransactionsDao) {
suspend fun deleteAllTransactions() {
transactionsDao.deleteAllTransactions()
}
fun getPemasukanCount(): Flow<Int> {
return transactionsDao.getPemasukanCount()
}
fun getPengeluaranCount(): Flow<Int> {
return transactionsDao.getPengeluaranCount()
}
}
\ No newline at end of file
......@@ -8,5 +8,6 @@
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="red">#FF0000</color>
<color name="green">#50C878</color>
<color name="ic_launcher_background">#f6bc2b</color>
</resources>
\ 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