From dd6fc1fdc55a80d348d8186b639f18c29e41f6fb Mon Sep 17 00:00:00 2001
From: bewe <93899302+bernarduswillson@users.noreply.github.com>
Date: Sat, 30 Mar 2024 10:36:10 +0700
Subject: [PATCH] feat: buggy filter and buggy landscape

---
 .../ui/screen/mainmenu/fragment/Statistics.kt | 173 +++++++++++------
 .../main/res/layout-land/activity_main.xml    | 181 ++++++++++++++++++
 .../main/res/layout/fragment_statistics.xml   |  92 +++++++--
 3 files changed, 371 insertions(+), 75 deletions(-)
 create mode 100644 app/src/main/res/layout-land/activity_main.xml

diff --git a/app/src/main/java/com/example/transactionapp/ui/screen/mainmenu/fragment/Statistics.kt b/app/src/main/java/com/example/transactionapp/ui/screen/mainmenu/fragment/Statistics.kt
index e05873b..1fb7171 100644
--- a/app/src/main/java/com/example/transactionapp/ui/screen/mainmenu/fragment/Statistics.kt
+++ b/app/src/main/java/com/example/transactionapp/ui/screen/mainmenu/fragment/Statistics.kt
@@ -1,10 +1,14 @@
 package com.example.transactionapp.ui.screen.mainmenu.fragment
 
+import android.content.pm.ActivityInfo
 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 android.widget.AdapterView
+import android.widget.ArrayAdapter
 import androidx.core.content.ContextCompat
 import androidx.fragment.app.activityViewModels
 import com.example.transactionapp.R
@@ -14,6 +18,8 @@ import com.github.mikephil.charting.animation.Easing
 import com.github.mikephil.charting.data.Entry
 import com.github.mikephil.charting.data.LineData
 import com.github.mikephil.charting.data.LineDataSet
+import java.util.Arrays
+import java.util.Calendar
 import java.util.Date
 
 class Statistics : Fragment() {
@@ -24,6 +30,11 @@ class Statistics : Fragment() {
     private val expenseValues = ArrayList<Entry>()
     private val savingValues = ArrayList<Entry>()
 
+    var incomeFetched = false
+    var expenseFetched = false
+    var savingFetched = false
+
+
     private val db: TransactionViewModel by activityViewModels()
 
     override fun onCreateView(
@@ -32,42 +43,125 @@ class Statistics : Fragment() {
     ): View? {
         binding = FragmentStatisticsBinding.inflate(inflater)
 
+        val months = arrayOf(
+            "January", "February", "March", "April", "May", "June",
+            "July", "August", "September", "October", "November", "December"
+        )
+
+        val arrayAdp = ArrayAdapter(requireActivity(), R.layout.selected_dropdown_item, months)
+        binding.monthInput.adapter = arrayAdp
+
+        val calendar = Calendar.getInstance()
+        val currentMonthIndex = calendar.get(Calendar.MONTH)
+        binding.monthInput.setSelection(currentMonthIndex)
+
         dataListing()
 
+        binding.monthInput.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
+            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
+                val selectedMonth = position + 1
+                val selectedYear = binding.yearInput.selectedItem.toString().toInt()
+                db.getStatisticByMonth(getDateForMonth(selectedMonth, selectedYear))
+
+                renderCharts()
+            }
+
+            override fun onNothingSelected(parent: AdapterView<*>?) {
+            }
+        }
+
+        binding.yearInput.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
+            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
+                val selectedYear = binding.yearInput.selectedItem.toString().toInt()
+                val selectedMonth = binding.monthInput.selectedItemPosition + 1
+                db.getStatisticByMonth(getDateForMonth(selectedMonth, selectedYear))
+
+                renderCharts()
+            }
+
+            override fun onNothingSelected(parent: AdapterView<*>?) {
+            }
+        }
+
         return binding.root
     }
 
+    private fun getDateForMonth(month: Int, year: Int): Date {
+        val calendar = Calendar.getInstance()
+        calendar.set(Calendar.YEAR, year)
+        calendar.set(Calendar.MONTH, month - 1)
+        return calendar.time
+    }
+
+
     private fun dataListing() {
-        db.sumOfIncome.observe(viewLifecycleOwner) {
-            val sum = it.toString().reversed().chunked(3).joinToString(".").reversed()
-            binding.incomeSum.text = sum
-        }
         db.listOfIncome.observe(viewLifecycleOwner) {
-            for (i in it.indices){
+            incomeValues.clear()
+            for (i in it.indices) {
                 incomeValues.add(Entry(i.toFloat(), it[i].toFloat()))
             }
-            setChart("income")
+            incomeFetched = true
+            renderCharts()
         }
 
-        db.sumOfExpense.observe(viewLifecycleOwner) {
-            val sum = it.toString().reversed().chunked(3).joinToString(".").reversed()
-            binding.expenseSum.text = sum
-        }
         db.listOfExpense.observe(viewLifecycleOwner) {
-            for (i in it.indices){
+            expenseValues.clear()
+            for (i in it.indices) {
                 expenseValues.add(Entry(i.toFloat(), it[i].toFloat()))
             }
-            setChart("expense")
+            expenseFetched = true
+            renderCharts()
+        }
+
+        db.listOfSaving.observe(viewLifecycleOwner) {
+            savingValues.clear()
+            for (i in it.indices) {
+                savingValues.add(Entry(i.toFloat(), it[i].toFloat()))
+            }
+            savingFetched = true
+            renderCharts()
+        }
+
+
+        db.sumOfIncome.observe(viewLifecycleOwner) {
+            val sum = it.toString().reversed().chunked(3).joinToString(".").reversed()
+            binding.incomeSum.text = sum
+        }
+
+        db.sumOfExpense.observe(viewLifecycleOwner) {
+            val sum = it.toString().reversed().chunked(3).joinToString(".").reversed()
+            binding.expenseSum.text = sum
         }
 
         db.sumOfSaving.observe(viewLifecycleOwner) {
             val sum = it.toString().reversed().chunked(3).joinToString(".").reversed()
             binding.savingSum.text = sum
         }
-        db.listOfSaving.observe(viewLifecycleOwner) {
-            for (i in it.indices){
-                savingValues.add(Entry(i.toFloat(), it[i].toFloat()))
-            }
+
+        db.dateAll.observe(viewLifecycleOwner) { transactionDateList ->
+            val yearsList = transactionDateList
+                .map { transactionDate ->
+                    transactionDate.date.split(" ")[2]
+                }
+                .distinct()
+
+            val years = yearsList.toTypedArray()
+            Log.d("Years", "onCreateView: ${Arrays.toString(years)}")
+
+            val arrayAdpYear = ArrayAdapter(requireActivity(), R.layout.selected_dropdown_item, years)
+            binding.yearInput.adapter = arrayAdpYear
+
+            val calendar = Calendar.getInstance()
+            val currentYear = calendar.get(Calendar.YEAR).toString()
+            val currentYearIndex = years.indexOf(currentYear)
+            binding.yearInput.setSelection(currentYearIndex)
+        }
+    }
+
+    private fun renderCharts() {
+        if (incomeFetched && expenseFetched && savingFetched) {
+            setChart("income")
+            setChart("expense")
             setChart("saving")
         }
     }
@@ -132,51 +226,4 @@ class Statistics : Fragment() {
             else -> emptyList()
         }
     }
-
-
-//    private fun setChart(type: String) {
-//        val dataSet: LineDataSet
-//
-//        if (binding.incomeChart.data != null && binding.incomeChart.data.dataSetCount > 0) {
-//            dataSet = binding.incomeChart.data.getDataSetByIndex(0) as LineDataSet
-//            dataSet.values = incomeValues
-//            binding.incomeChart.data.notifyDataChanged()
-//            binding.incomeChart.notifyDataSetChanged()
-//        } else {
-//            dataSet = LineDataSet(incomeValues, "Income")
-//            dataSet.setColors(ContextCompat.getColor(requireContext(), R.color.N1))
-//            dataSet.setCircleColors(ContextCompat.getColor(requireContext(), R.color.N1))
-//            dataSet.lineWidth = 2f
-//            dataSet.circleRadius = 1f
-//            dataSet.setDrawValues(false)
-//
-//            binding.incomeChart.apply {
-//                description.isEnabled = false
-//                legend.isEnabled = false
-//                axisLeft.apply {
-//                    setDrawLabels(false)
-//                    setDrawGridLines(false)
-//                    setDrawAxisLine(false)
-//                }
-//                axisRight.apply {
-//                    setDrawLabels(false)
-//                    setDrawGridLines(false)
-//                    setDrawAxisLine(false)
-//                }
-//                xAxis.apply {
-//                    setDrawLabels(false)
-//                    setDrawGridLines(false)
-//                    setDrawAxisLine(false)
-//                }
-//
-//                setViewPortOffsets(0f, 0f, 0f, 0f)
-//
-//                animateXY(700, 1000, Easing.EaseInOutQuad)
-//            }
-//
-//            val data = LineData(dataSet)
-//            binding.incomeChart.data = data
-//            binding.incomeChart.invalidate()
-//        }
-//    }
 }
\ No newline at end of file
diff --git a/app/src/main/res/layout-land/activity_main.xml b/app/src/main/res/layout-land/activity_main.xml
new file mode 100644
index 0000000..effb04a
--- /dev/null
+++ b/app/src/main/res/layout-land/activity_main.xml
@@ -0,0 +1,181 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".ui.screen.mainmenu.MainActivity">
+
+    <!-- Toolbar -->
+    <!--    <ImageButton-->
+    <!--        android:id="@+id/BtnBack"-->
+    <!--        android:layout_width="wrap_content"-->
+    <!--        android:layout_height="wrap_content"-->
+    <!--        app:layout_constraintStart_toStartOf="parent"-->
+    <!--        app:layout_constraintTop_toTopOf="@+id/TvToolbarTitle"-->
+    <!--        app:layout_constraintBottom_toBottomOf="@+id/TvToolbarTitle"-->
+    <!--        android:layout_marginStart="14dp"-->
+    <!--        android:src="@drawable/back_dark_ic"-->
+    <!--        android:background="@android:color/transparent"-->
+    <!--        android:contentDescription="@string/alt_back_button"/>-->
+
+    <!--    <TextView-->
+    <!--        android:id="@+id/TvToolbarTitle"-->
+    <!--        android:layout_width="wrap_content"-->
+    <!--        android:layout_height="wrap_content"-->
+    <!--        android:layout_marginTop="20dp"-->
+    <!--        android:fontFamily="@font/playfairdisplay_extrabold"-->
+    <!--        android:lineSpacingExtra="3sp"-->
+    <!--        android:text="@string/title_transactions"-->
+    <!--        android:textAlignment="center"-->
+    <!--        android:textColor="?attr/colorSecondary"-->
+    <!--        android:textSize="21sp"-->
+    <!--        app:layout_constraintEnd_toEndOf="parent"-->
+    <!--        app:layout_constraintStart_toStartOf="parent"-->
+    <!--        app:layout_constraintTop_toTopOf="parent" />-->
+
+    <!-- Fragment Placeholder -->
+    <!--    <fragment-->
+    <!--        android:id="@+id/nav_host_fragment"-->
+    <!--        android:name="androidx.navigation.fragment.NavHostFragment"-->
+    <!--        android:layout_width="match_parent"-->
+    <!--        android:layout_height="match_parent"-->
+    <!--        android:layout_marginTop="70dp"-->
+    <!--        android:layout_marginBottom="92dp"-->
+    <!--        app:layout_constraintTop_toBottomOf="@id/TvToolbarTitle"-->
+    <!--        app:defaultNavHost="true"-->
+    <!--        app:navGraph="@navigation/navigation" />-->
+
+    <FrameLayout
+        android:id="@+id/navHostFragment"
+        android:layout_width="0dp"
+        android:layout_height="0dp"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toEndOf="@+id/coordinateLayout"
+        app:layout_constraintTop_toTopOf="parent" />
+
+    <androidx.coordinatorlayout.widget.CoordinatorLayout
+        android:id="@+id/coordinateLayout"
+        android:layout_width="80dp"
+        android:layout_height="0dp"
+        app:fabCradleMargin="20dp"
+        android:background="@android:color/transparent"
+        app:fabCradleVerticalOffset="10dp"
+        app:fabCradleRoundedCornerRadius="20dp"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintTop_toTopOf="parent"
+        app:layout_constraintStart_toStartOf="parent">
+        <com.google.android.material.bottomappbar.BottomAppBar
+            android:id="@+id/bottomAppBar"
+            android:layout_width="match_parent"
+            android:background="@android:color/transparent"
+            style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
+            android:layout_height="match_parent">
+            <com.google.android.material.bottomnavigation.BottomNavigationView
+                android:id="@+id/bottomNavigationView"
+                android:layout_width="match_parent"
+                android:background="@android:color/transparent"
+                android:layout_gravity="center"
+                android:layout_height="match_parent"
+                app:itemHorizontalTranslationEnabled="true"
+
+                app:itemIconSize="25dp"
+                app:itemIconTint="?attr/colorSecondary"
+                app:itemTextColor="?attr/colorSecondary"
+                app:menu="@menu/button_menu" />
+        </com.google.android.material.bottomappbar.BottomAppBar>
+
+        <com.google.android.material.floatingactionbutton.FloatingActionButton
+            android:id="@+id/fabAddTransaction"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_gravity="center"
+            android:background="@android:color/transparent"
+            android:src="@drawable/add_ic"
+            app:maxImageSize="50dp"
+            app:fabCustomSize="60dp"
+            app:fabSize="auto"
+            />
+    </androidx.coordinatorlayout.widget.CoordinatorLayout>
+
+    <!-- Navigation Bar -->
+
+    <!--    <androidx.constraintlayout.widget.ConstraintLayout-->
+    <!--        android:id="@+id/constraintLayout2"-->
+    <!--        android:layout_width="match_parent"-->
+    <!--        android:layout_height="92dp"-->
+    <!--        android:background="?attr/colorPrimaryVariant"-->
+    <!--        app:layout_constraintBottom_toBottomOf="parent"-->
+    <!--        app:layout_constraintEnd_toEndOf="parent"-->
+    <!--        app:layout_constraintStart_toStartOf="parent">-->
+
+    <!--        <ImageButton-->
+    <!--            android:id="@+id/IbTransactionBtn"-->
+    <!--            android:layout_width="36dp"-->
+    <!--            android:layout_height="36dp"-->
+    <!--            android:background="@android:color/transparent"-->
+    <!--            android:clickable="true"-->
+    <!--            android:contentDescription="@string/transaction_button"-->
+    <!--            android:scaleType="fitXY"-->
+    <!--            android:src="@drawable/transaction_ic"-->
+    <!--            app:layout_constraintBottom_toBottomOf="parent"-->
+    <!--            app:layout_constraintEnd_toStartOf="@+id/IbScanBtn"-->
+    <!--            app:layout_constraintStart_toStartOf="parent"-->
+    <!--            app:layout_constraintTop_toTopOf="parent" />-->
+
+    <!--        <ImageButton-->
+    <!--            android:id="@+id/IbScanBtn"-->
+    <!--            android:layout_width="36dp"-->
+    <!--            android:layout_height="36dp"-->
+    <!--            android:background="@android:color/transparent"-->
+    <!--            android:contentDescription="@string/scan_button"-->
+    <!--            android:scaleType="fitXY"-->
+    <!--            android:src="@drawable/ic_scan_button"-->
+    <!--            app:layout_constraintBottom_toBottomOf="parent"-->
+    <!--            app:layout_constraintEnd_toStartOf="@+id/IbAddBtn"-->
+    <!--            app:layout_constraintStart_toEndOf="@id/IbTransactionBtn"-->
+    <!--            app:layout_constraintTop_toTopOf="parent" />-->
+
+    <!--        <ImageButton-->
+    <!--            android:id="@+id/IbAddBtn"-->
+    <!--            android:layout_width="68dp"-->
+    <!--            android:layout_height="68dp"-->
+    <!--            android:background="@android:color/transparent"-->
+    <!--            android:contentDescription="@string/add_transaction_button"-->
+    <!--            android:scaleType="fitXY"-->
+    <!--            android:src="@drawable/add_ic"-->
+    <!--            app:layout_constraintBottom_toBottomOf="parent"-->
+    <!--            app:layout_constraintEnd_toStartOf="@+id/IbStatisticsBtn"-->
+    <!--            app:layout_constraintStart_toEndOf="@id/IbScanBtn"-->
+    <!--            app:layout_constraintTop_toTopOf="parent" />-->
+
+    <!--        <ImageButton-->
+    <!--            android:id="@+id/IbStatisticsBtn"-->
+    <!--            android:layout_width="36dp"-->
+    <!--            android:layout_height="36dp"-->
+    <!--            android:background="@android:color/transparent"-->
+    <!--            android:contentDescription="@string/statistics_button"-->
+    <!--            android:scaleType="fitXY"-->
+    <!--            android:src="@drawable/ic_statistics_button"-->
+    <!--            app:layout_constraintBottom_toBottomOf="parent"-->
+    <!--            app:layout_constraintEnd_toStartOf="@+id/IbSettingsBtn"-->
+    <!--            app:layout_constraintStart_toEndOf="@id/IbAddBtn"-->
+    <!--            app:layout_constraintTop_toTopOf="parent" />-->
+
+    <!--        <ImageButton-->
+    <!--            android:id="@+id/IbSettingsBtn"-->
+    <!--            android:layout_width="36dp"-->
+    <!--            android:layout_height="36dp"-->
+    <!--            android:background="@android:color/transparent"-->
+    <!--            android:contentDescription="@string/settings_button"-->
+    <!--            android:scaleType="fitXY"-->
+    <!--            android:src="@drawable/ic_settings_button"-->
+    <!--            app:layout_constraintBottom_toBottomOf="parent"-->
+    <!--            app:layout_constraintEnd_toEndOf="parent"-->
+    <!--            app:layout_constraintStart_toEndOf="@id/IbStatisticsBtn"-->
+    <!--            app:layout_constraintTop_toTopOf="parent" />-->
+    <!--    </androidx.constraintlayout.widget.ConstraintLayout>-->
+
+
+</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
diff --git a/app/src/main/res/layout/fragment_statistics.xml b/app/src/main/res/layout/fragment_statistics.xml
index dd95de8..c0df1d1 100644
--- a/app/src/main/res/layout/fragment_statistics.xml
+++ b/app/src/main/res/layout/fragment_statistics.xml
@@ -25,25 +25,94 @@
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintEnd_toEndOf="parent" />
 
-        <ScrollView
-            android:layout_width="match_parent"
-            android:layout_height="0dp"
-            app:layout_constraintBottom_toBottomOf="parent"
-            android:layout_marginHorizontal="10dp"
-            android:scrollbarSize="0dp"
-            app:layout_constraintTop_toBottomOf="@+id/transactionText">
             <androidx.constraintlayout.widget.ConstraintLayout
                 android:layout_width="match_parent"
-                android:layout_height="match_parent"
+                android:layout_height="0dp"
+                android:layout_marginHorizontal="15dp"
                 app:layout_constraintStart_toStartOf="parent"
                 app:layout_constraintEnd_toEndOf="parent"
+                app:layout_constraintTop_toBottomOf="@id/transactionText"
                 app:layout_constraintBottom_toBottomOf="parent">
 
+                <androidx.constraintlayout.widget.ConstraintLayout
+                    android:id="@+id/searchLayout"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content"
+                    app:layout_constraintTop_toTopOf="parent">
+
+                    <TextView
+                        android:id="@+id/monthLabel"
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        app:layout_constraintTop_toTopOf="parent"
+                        app:layout_constraintStart_toStartOf="parent"
+                        android:text="Month"
+                        android:textSize="15sp"
+                        android:fontFamily="@font/playfairdisplay_semibold"
+                        android:textColor="@color/N6"/>
+
+                    <Spinner
+                        android:id="@+id/monthInput"
+                        android:layout_width="0dp"
+                        android:layout_height="wrap_content"
+                        android:paddingVertical="6dp"
+                        android:paddingHorizontal="7dp"
+                        android:text="Choose category"
+                        android:layout_marginTop="5dp"
+                        android:fontFamily="@font/poppins_regular"
+                        app:layout_constraintTop_toBottomOf="@id/monthLabel"
+                        app:layout_constraintStart_toStartOf="parent"
+                        android:background="@drawable/spinner_input_field"
+                        android:contentDescription="transaction category input"
+                        app:layout_constraintWidth_percent="0.55"/>
+
+                    <TextView
+                        android:id="@+id/yearLabel"
+                        android:layout_width="0dp"
+                        android:layout_height="wrap_content"
+                        app:layout_constraintTop_toTopOf="parent"
+                        app:layout_constraintEnd_toEndOf="parent"
+                        android:layout_marginBottom="5dp"
+                        android:text="Year"
+                        android:textSize="15sp"
+                        android:fontFamily="@font/playfairdisplay_semibold"
+                        android:textColor="@color/N6"
+                        app:layout_constraintWidth_percent="0.4"/>
+
+                    <Spinner
+                        android:id="@+id/yearInput"
+                        android:layout_width="0dp"
+                        android:layout_height="wrap_content"
+                        android:paddingVertical="6dp"
+                        android:paddingHorizontal="7dp"
+                        android:text="Choose category"
+                        android:layout_marginTop="5dp"
+                        android:fontFamily="@font/poppins_regular"
+                        app:layout_constraintTop_toBottomOf="@id/yearLabel"
+                        app:layout_constraintEnd_toEndOf="parent"
+                        android:background="@drawable/spinner_input_field"
+                        android:contentDescription="transaction category input"
+                        app:layout_constraintWidth_percent="0.4"/>
+
+                </androidx.constraintlayout.widget.ConstraintLayout>
+
+                <ScrollView
+                    android:layout_width="match_parent"
+                    android:layout_height="0dp"
+                    android:layout_marginTop="20dp"
+                    android:scrollbarSize="0dp"
+                    app:layout_constraintBottom_toBottomOf="parent"
+                    app:layout_constraintTop_toBottomOf="@+id/searchLayout">
+
+                    <LinearLayout
+                        android:layout_width="match_parent"
+                        android:layout_height="wrap_content"
+                        android:orientation="vertical">
+
                 <LinearLayout
                     android:id="@+id/incomeCard"
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
-                    android:layout_marginTop="20dp"
                     app:layout_constraintTop_toTopOf="parent"
                     app:layout_constraintStart_toStartOf="parent"
                     app:layout_constraintEnd_toEndOf="parent"
@@ -198,10 +267,9 @@
                         android:background="@drawable/statistic_card_saving" />
 
                 </LinearLayout>
-
-
+                    </LinearLayout>
+                </ScrollView>
             </androidx.constraintlayout.widget.ConstraintLayout>
-        </ScrollView>
 
     </androidx.constraintlayout.widget.ConstraintLayout>
 
-- 
GitLab