From 5e6bd979a5931c55cc8ed9c580fbb85f19b4bc1b Mon Sep 17 00:00:00 2001
From: "Moch. Sofyan Firdaus" <13521083@std.stei.itb.ac.id>
Date: Tue, 2 Apr 2024 02:52:14 +0700
Subject: [PATCH] feat: make pie entries non-nullable

---
 .../bondoman/ui/graph/GraphFragment.kt        | 23 +++++++------------
 1 file changed, 8 insertions(+), 15 deletions(-)

diff --git a/app/src/main/java/com/onionsquad/bondoman/ui/graph/GraphFragment.kt b/app/src/main/java/com/onionsquad/bondoman/ui/graph/GraphFragment.kt
index 29090db..fb0bc19 100644
--- a/app/src/main/java/com/onionsquad/bondoman/ui/graph/GraphFragment.kt
+++ b/app/src/main/java/com/onionsquad/bondoman/ui/graph/GraphFragment.kt
@@ -8,6 +8,7 @@ import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
 import androidx.fragment.app.Fragment
+import androidx.lifecycle.Observer
 import androidx.lifecycle.ViewModelProvider
 import com.github.mikephil.charting.components.Legend
 import com.github.mikephil.charting.data.PieData
@@ -22,8 +23,8 @@ class GraphFragment : Fragment() {
 
     private var _binding: FragmentGraphBinding? = null
     private val binding get() = _binding!!
-    private var incomeEntry: PieEntry? = null
-    private var outcomeEntry: PieEntry? = null
+    private var incomeEntry = PieEntry(0f, "Income")
+    private var outcomeEntry = PieEntry(0f, "Outcome")
 
     override fun onCreateView(
         inflater: LayoutInflater,
@@ -35,21 +36,13 @@ class GraphFragment : Fragment() {
 
         ViewModelProvider(this)[GraphViewModel::class.java].apply {
             incomes.observe(viewLifecycleOwner) {
-                if (it.isNotEmpty()) {
-                    incomeEntry = PieEntry(it.sumOf { e -> e.amount }.toFloat(), "Income")
-                    Log.d("DATA", "$it")
-                } else {
-                    incomeEntry = null
-                }
+                incomeEntry = PieEntry(it.sumOf { e -> e.amount }.toFloat(), "Income")
+                Log.d("DATA", "$it")
                 refreshPieChart()
             }
             outcomes.observe(viewLifecycleOwner) {
-                if (it.isNotEmpty()) {
-                    outcomeEntry = PieEntry(it.sumOf { e -> e.amount }.toFloat(), "Outcome")
-                    Log.d("DATA", "$it")
-                } else {
-                    outcomeEntry = null
-                }
+                outcomeEntry = PieEntry(it.sumOf { e -> e.amount }.toFloat(), "Outcome")
+                Log.d("DATA", "$it")
                 refreshPieChart()
             }
         }
@@ -76,7 +69,7 @@ class GraphFragment : Fragment() {
 
     private fun refreshPieChart() {
         binding.apply {
-            if (incomeEntry == null || outcomeEntry == null) {
+            if (incomeEntry.value == 0f && outcomeEntry.value == 0f) {
                 textGraph.visibility = View.VISIBLE
                 pieChart.visibility = View.GONE
             } else {
-- 
GitLab